UNPKG

4.3 kBMarkdownView Raw
1# Core
2
3> Shared utilities for Angular DevKit.
4
5# Exception
6
7# Json
8
9## Schema
10
11### SchemaValidatorResult
12
13```
14export interface SchemaValidatorResult {
15 success: boolean;
16 errors?: string[];
17}
18```
19
20### SchemaValidator
21
22```
23export interface SchemaValidator {
24 (data: any): Observable<SchemaValidatorResult>;
25}
26```
27
28### SchemaFormatter
29
30```
31export interface SchemaFormatter {
32 readonly async: boolean;
33 validate(data: any): boolean | Observable<boolean>;
34}
35```
36
37### SchemaRegistry
38
39```
40export 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.
49Constructor accepts object containing `SchemaFormatter` that will be added automatically.
50
51```
52export 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
65The `workspaces` namespace provides an API for interacting with the workspace file formats.
66It provides an abstraction of the underlying storage format of the workspace and provides
67support for both reading and writing. Currently, the only supported format is the JSON-based
68format used by the Angular CLI. For this format, the API provides internal change tracking of values which
69enables fine-grained updates to the underlying storage of the workspace. This allows for the
70retention of existing formatting and comments.
71
72A workspace is defined via the following object model. Definition collection objects are specialized
73Javascript `Map` objects with an additional `add` method to simplify addition and provide more localized
74error checking of the newly added values.
75
76```ts
77export interface WorkspaceDefinition {
78 readonly extensions: Record<string, JsonValue | undefined>;
79 readonly projects: ProjectDefinitionCollection;
80}
81
82export interface ProjectDefinition {
83 readonly extensions: Record<string, JsonValue | undefined>;
84 readonly targets: TargetDefinitionCollection;
85 root: string;
86 prefix?: string;
87 sourceRoot?: string;
88}
89
90export interface TargetDefinition {
91 options?: Record<string, JsonValue | undefined>;
92 configurations?: Record<string, Record<string, JsonValue | undefined> | undefined>;
93 builder: string;
94}
95```
96
97The API is asynchronous and has two main functions to facilitate reading, creation, and modifying
98a workspace: `readWorkspace` and `writeWorkspace`.
99
100```ts
101export enum WorkspaceFormat {
102 JSON,
103}
104```
105
106```ts
107export function readWorkspace(
108 path: string,
109 host: WorkspaceHost,
110 format?: WorkspaceFormat,
111): Promise<{ workspace: WorkspaceDefinition }>;
112```
113
114```ts
115export function writeWorkspace(
116 workspace: WorkspaceDefinition,
117 host: WorkspaceHost,
118 path?: string,
119 format?: WorkspaceFormat,
120): Promise<void>;
121```
122
123A `WorkspaceHost` abstracts the underlying data access methods from the functions. It provides
124methods to read, write, and analyze paths. A utility function is provided to create
125an instance of a `WorkspaceHost` from the Angular DevKit's virtual filesystem host abstraction.
126
127```ts
128export 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
135export function createWorkspaceHost(host: virtualFs.Host): WorkspaceHost;
136```
137
138## Usage Example
139
140To demonstrate the usage of the API, the following code will show how to add a option property
141to a build target for an application.
142
143```ts
144import { NodeJsSyncHost } from '@angular-devkit/core/node';
145import { workspaces } from '@angular-devkit/core';
146
147async 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
166demonstrate();
167```