1 | # Core
|
---|
2 |
|
---|
3 | > Shared utilities for Angular DevKit.
|
---|
4 |
|
---|
5 | # Exception
|
---|
6 |
|
---|
7 | # Json
|
---|
8 |
|
---|
9 | ## Schema
|
---|
10 |
|
---|
11 | ### SchemaValidatorResult
|
---|
12 |
|
---|
13 | ```
|
---|
14 | export interface SchemaValidatorResult {
|
---|
15 | success: boolean;
|
---|
16 | errors?: string[];
|
---|
17 | }
|
---|
18 | ```
|
---|
19 |
|
---|
20 | ### SchemaValidator
|
---|
21 |
|
---|
22 | ```
|
---|
23 | export interface SchemaValidator {
|
---|
24 | (data: any): Observable<SchemaValidatorResult>;
|
---|
25 | }
|
---|
26 | ```
|
---|
27 |
|
---|
28 | ### SchemaFormatter
|
---|
29 |
|
---|
30 | ```
|
---|
31 | export interface SchemaFormatter {
|
---|
32 | readonly async: boolean;
|
---|
33 | validate(data: any): boolean | Observable<boolean>;
|
---|
34 | }
|
---|
35 | ```
|
---|
36 |
|
---|
37 | ### SchemaRegistry
|
---|
38 |
|
---|
39 | ```
|
---|
40 | export interface SchemaRegistry {
|
---|
41 | compile(schema: Object): Observable<SchemaValidator>;
|
---|
42 | addFormat(name: string, formatter: SchemaFormatter): void;
|
---|
43 | }
|
---|
44 | ```
|
---|
45 |
|
---|
46 | ### CoreSchemaRegistry
|
---|
47 |
|
---|
48 | `SchemaRegistry` implementation using https://github.com/epoberezkin/ajv.
|
---|
49 | Constructor accepts object containing `SchemaFormatter` that will be added automatically.
|
---|
50 |
|
---|
51 | ```
|
---|
52 | export class CoreSchemaRegistry implements SchemaRegistry {
|
---|
53 | constructor(formats: { [name: string]: SchemaFormatter} = {}) {}
|
---|
54 | }
|
---|
55 | ```
|
---|
56 |
|
---|
57 | # Logger
|
---|
58 |
|
---|
59 | # Utils
|
---|
60 |
|
---|
61 | # Virtual FS
|
---|
62 |
|
---|
63 | # Workspaces
|
---|
64 |
|
---|
65 | The `workspaces` namespace provides an API for interacting with the workspace file formats.
|
---|
66 | It provides an abstraction of the underlying storage format of the workspace and provides
|
---|
67 | support for both reading and writing. Currently, the only supported format is the JSON-based
|
---|
68 | format used by the Angular CLI. For this format, the API provides internal change tracking of values which
|
---|
69 | enables fine-grained updates to the underlying storage of the workspace. This allows for the
|
---|
70 | retention of existing formatting and comments.
|
---|
71 |
|
---|
72 | A workspace is defined via the following object model. Definition collection objects are specialized
|
---|
73 | Javascript `Map` objects with an additional `add` method to simplify addition and provide more localized
|
---|
74 | error checking of the newly added values.
|
---|
75 |
|
---|
76 | ```ts
|
---|
77 | export interface WorkspaceDefinition {
|
---|
78 | readonly extensions: Record<string, JsonValue | undefined>;
|
---|
79 | readonly projects: ProjectDefinitionCollection;
|
---|
80 | }
|
---|
81 |
|
---|
82 | export interface ProjectDefinition {
|
---|
83 | readonly extensions: Record<string, JsonValue | undefined>;
|
---|
84 | readonly targets: TargetDefinitionCollection;
|
---|
85 | root: string;
|
---|
86 | prefix?: string;
|
---|
87 | sourceRoot?: string;
|
---|
88 | }
|
---|
89 |
|
---|
90 | export interface TargetDefinition {
|
---|
91 | options?: Record<string, JsonValue | undefined>;
|
---|
92 | configurations?: Record<string, Record<string, JsonValue | undefined> | undefined>;
|
---|
93 | builder: string;
|
---|
94 | }
|
---|
95 | ```
|
---|
96 |
|
---|
97 | The API is asynchronous and has two main functions to facilitate reading, creation, and modifying
|
---|
98 | a workspace: `readWorkspace` and `writeWorkspace`.
|
---|
99 |
|
---|
100 | ```ts
|
---|
101 | export enum WorkspaceFormat {
|
---|
102 | JSON,
|
---|
103 | }
|
---|
104 | ```
|
---|
105 |
|
---|
106 | ```ts
|
---|
107 | export function readWorkspace(
|
---|
108 | path: string,
|
---|
109 | host: WorkspaceHost,
|
---|
110 | format?: WorkspaceFormat,
|
---|
111 | ): Promise<{ workspace: WorkspaceDefinition }>;
|
---|
112 | ```
|
---|
113 |
|
---|
114 | ```ts
|
---|
115 | export function writeWorkspace(
|
---|
116 | workspace: WorkspaceDefinition,
|
---|
117 | host: WorkspaceHost,
|
---|
118 | path?: string,
|
---|
119 | format?: WorkspaceFormat,
|
---|
120 | ): Promise<void>;
|
---|
121 | ```
|
---|
122 |
|
---|
123 | A `WorkspaceHost` abstracts the underlying data access methods from the functions. It provides
|
---|
124 | methods to read, write, and analyze paths. A utility function is provided to create
|
---|
125 | an instance of a `WorkspaceHost` from the Angular DevKit's virtual filesystem host abstraction.
|
---|
126 |
|
---|
127 | ```ts
|
---|
128 | export interface WorkspaceHost {
|
---|
129 | readFile(path: string): Promise<string>;
|
---|
130 | writeFile(path: string, data: string): Promise<void>;
|
---|
131 | isDirectory(path: string): Promise<boolean>;
|
---|
132 | isFile(path: string): Promise<boolean>;
|
---|
133 | }
|
---|
134 |
|
---|
135 | export function createWorkspaceHost(host: virtualFs.Host): WorkspaceHost;
|
---|
136 | ```
|
---|
137 |
|
---|
138 | ## Usage Example
|
---|
139 |
|
---|
140 | To demonstrate the usage of the API, the following code will show how to add a option property
|
---|
141 | to a build target for an application.
|
---|
142 |
|
---|
143 | ```ts
|
---|
144 | import { NodeJsSyncHost } from '@angular-devkit/core/node';
|
---|
145 | import { workspaces } from '@angular-devkit/core';
|
---|
146 |
|
---|
147 | async function demonstrate() {
|
---|
148 | const host = workspaces.createWorkspaceHost(new NodeJsSyncHost());
|
---|
149 | const { workspace } = await workspaces.readWorkspace('path/to/workspace/directory/', host);
|
---|
150 |
|
---|
151 | const project = workspace.projects.get('my-app');
|
---|
152 | if (!project) {
|
---|
153 | throw new Error('my-app does not exist');
|
---|
154 | }
|
---|
155 |
|
---|
156 | const buildTarget = project.targets.get('build');
|
---|
157 | if (!buildTarget) {
|
---|
158 | throw new Error('build target does not exist');
|
---|
159 | }
|
---|
160 |
|
---|
161 | buildTarget.options.optimization = true;
|
---|
162 |
|
---|
163 | await workspaces.writeWorkspace(workspace, host);
|
---|
164 | }
|
---|
165 |
|
---|
166 | demonstrate();
|
---|
167 | ```
|
---|