mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-15 01:54:04 +08:00
docs(@angular-devkit/core): add preliminary overview of workspace API
This commit is contained in:
parent
8a34a85881
commit
8214be43a6
@ -56,4 +56,110 @@ export class CoreSchemaRegistry implements SchemaRegistry {
|
||||
|
||||
# Utils
|
||||
|
||||
# Virtual FS
|
||||
# Virtual FS
|
||||
|
||||
# Workspaces
|
||||
|
||||
The `workspaces` namespace provides an API for interacting with the workspace file formats.
|
||||
It provides an abstraction of the underlying storage format of the workspace and provides
|
||||
support for both reading and writing. Currently, the only supported format is the JSON-based
|
||||
format used by the Angular CLI. For this format, the API provides internal change tracking of values which
|
||||
enables fine-grained updates to the underlying storage of the workspace. This allows for the
|
||||
retention of existing formatting and comments.
|
||||
|
||||
A workspace is defined via the following object model. Definition collection objects are specialized
|
||||
Javascript `Map` objects with an additional `add` method to simplify addition and provide more localized
|
||||
error checking of the newly added values.
|
||||
|
||||
```ts
|
||||
export interface WorkspaceDefinition {
|
||||
readonly extensions: Record<string, JsonValue | undefined>;
|
||||
readonly projects: ProjectDefinitionCollection;
|
||||
}
|
||||
|
||||
export interface ProjectDefinition {
|
||||
readonly extensions: Record<string, JsonValue | undefined>;
|
||||
readonly targets: TargetDefinitionCollection;
|
||||
root: string;
|
||||
prefix?: string;
|
||||
sourceRoot?: string;
|
||||
}
|
||||
|
||||
export interface TargetDefinition {
|
||||
options?: Record<string, JsonValue | undefined>;
|
||||
configurations?: Record<string, Record<string, JsonValue | undefined> | undefined>;
|
||||
builder: string;
|
||||
}
|
||||
```
|
||||
|
||||
The API is asynchronous and has two main functions to facilitate reading, creation, and modifying
|
||||
a workspace: `readWorkspace` and `writeWorkspace`.
|
||||
|
||||
```ts
|
||||
export enum WorkspaceFormat {
|
||||
JSON,
|
||||
}
|
||||
```
|
||||
|
||||
```ts
|
||||
export function readWorkspace(
|
||||
path: string,
|
||||
host: WorkspaceHost,
|
||||
format?: WorkspaceFormat,
|
||||
): Promise<{ workspace: WorkspaceDefinition; }>;
|
||||
```
|
||||
|
||||
```ts
|
||||
export function writeWorkspace(
|
||||
workspace: WorkspaceDefinition,
|
||||
host: WorkspaceHost,
|
||||
path?: string,
|
||||
format?: WorkspaceFormat,
|
||||
): Promise<void>;
|
||||
```
|
||||
|
||||
A `WorkspaceHost` abstracts the underlying data access methods from the functions. It provides
|
||||
methods to read, write, and analyze paths. A utility function is provided to create
|
||||
an instance of a `WorkspaceHost` from the Angular DevKit's virtual filesystem host abstraction.
|
||||
|
||||
```ts
|
||||
export interface WorkspaceHost {
|
||||
readFile(path: string): Promise<string>;
|
||||
writeFile(path: string, data: string): Promise<void>;
|
||||
isDirectory(path: string): Promise<boolean>;
|
||||
isFile(path: string): Promise<boolean>;
|
||||
}
|
||||
|
||||
export function createWorkspaceHost(host: virtualFs.Host): WorkspaceHost;
|
||||
```
|
||||
|
||||
## Usage Example
|
||||
|
||||
To demonstrate the usage of the API, the following code will show how to add a option property
|
||||
to a build target for an application.
|
||||
|
||||
```ts
|
||||
import { NodeJsSyncHost } from '@angular-devkit/core/node';
|
||||
import { workspaces } from '@angular-devkit/core';
|
||||
|
||||
async function demonstrate() {
|
||||
const host = workspaces.createWorkspaceHost(new NodeJsSyncHost());
|
||||
const workspace = await workspaces.readWorkspace('path/to/workspace/directory/', host);
|
||||
|
||||
const project = workspace.projects.get('my-app');
|
||||
if (!project) {
|
||||
throw new Error('my-app does not exist');
|
||||
}
|
||||
|
||||
const buildTarget = project.targets.get('build');
|
||||
if (!buildTarget) {
|
||||
throw new Error('build target does not exist');
|
||||
}
|
||||
|
||||
buildTarget.options.optimization = true;
|
||||
|
||||
await workspaces.writeWorkspace(workspace, host);
|
||||
}
|
||||
|
||||
demonstrate();
|
||||
```
|
@ -13,14 +13,23 @@ import { writeJsonWorkspace } from './json/writer';
|
||||
|
||||
const formatLookup = new WeakMap<WorkspaceDefinition, WorkspaceFormat>();
|
||||
|
||||
/**
|
||||
* Supported workspace formats
|
||||
*/
|
||||
export enum WorkspaceFormat {
|
||||
JSON,
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
export function _test_addWorkspaceFile(name: string, format: WorkspaceFormat): void {
|
||||
workspaceFiles[name] = format;
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
export function _test_removeWorkspaceFile(name: string): void {
|
||||
delete workspaceFiles[name];
|
||||
}
|
||||
@ -31,6 +40,22 @@ const workspaceFiles: Record<string, WorkspaceFormat> = {
|
||||
'.angular.json': WorkspaceFormat.JSON,
|
||||
};
|
||||
|
||||
/**
|
||||
* Reads and constructs a `WorkspaceDefinition`. If the function is provided with a path to a
|
||||
* directory instead of a file, a search of the directory's files will commence to attempt to
|
||||
* locate a known workspace file. Currently the following are considered known workspace files:
|
||||
* - `angular.json`
|
||||
* - `.angular.json`
|
||||
*
|
||||
* @param path The path to either a workspace file or a directory containing a workspace file.
|
||||
* @param host The `WorkspaceHost` to use to access the file and directory data.
|
||||
* @param format An optional `WorkspaceFormat` value. Used if the path specifies a non-standard
|
||||
* file name that would prevent automatically discovering the format.
|
||||
*
|
||||
*
|
||||
* @return An `Promise` of the read result object with the `WorkspaceDefinition` contained within
|
||||
* the `workspace` property.
|
||||
*/
|
||||
export async function readWorkspace(
|
||||
path: string,
|
||||
host: WorkspaceHost,
|
||||
@ -82,6 +107,24 @@ export async function readWorkspace(
|
||||
return { workspace };
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a `WorkspaceDefinition` to the underlying storage via the provided `WorkspaceHost`.
|
||||
* If the `WorkspaceDefinition` was created via the `readWorkspace` function, metadata will be
|
||||
* used to determine the path and format of the Workspace. In all other cases, the `path` and
|
||||
* `format` options must be specified as they would be otherwise unknown.
|
||||
*
|
||||
* @param workspace The `WorkspaceDefinition` that will be written.
|
||||
* @param host The `WorkspaceHost` to use to access/write the file and directory data.
|
||||
* @param path The path to a file location for the output. Required if `readWorkspace` was not
|
||||
* used to create the `WorkspaceDefinition`. Optional otherwise; will override the
|
||||
* `WorkspaceDefinition` metadata if provided.
|
||||
* @param format The `WorkspaceFormat` to use for output. Required if `readWorkspace` was not
|
||||
* used to create the `WorkspaceDefinition`. Optional otherwise; will override the
|
||||
* `WorkspaceDefinition` metadata if provided.
|
||||
*
|
||||
*
|
||||
* @return An `Promise` of type `void`.
|
||||
*/
|
||||
export async function writeWorkspace(
|
||||
workspace: WorkspaceDefinition,
|
||||
host: WorkspaceHost,
|
||||
|
Loading…
x
Reference in New Issue
Block a user