Skip to content

Commit a1aa8d5

Browse files
clydinhansl
authored andcommitted
feat(@angular-devkit/core): add workspace reader/writer core API
NOTE: No changes have yet been made to the public API of the package. This introduces the core and eventual public API for the stable workspace API. This is not intended to be feature complete but rather represents the initial base infrastucture necessary for pending future feature additions.
1 parent 263231e commit a1aa8d5

File tree

7 files changed

+1195
-0
lines changed

7 files changed

+1195
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import { basename, getSystemPath, join, normalize } from '../virtual-fs';
9+
import { ProjectDefinitionCollection, WorkspaceDefinition } from './definitions';
10+
import { WorkspaceHost } from './host';
11+
12+
const formatLookup = new WeakMap<WorkspaceDefinition, WorkspaceFormat>();
13+
14+
export enum WorkspaceFormat {
15+
JSON,
16+
}
17+
18+
export function _test_addWorkspaceFile(name: string, format: WorkspaceFormat): void {
19+
workspaceFiles[name] = format;
20+
}
21+
22+
export function _test_removeWorkspaceFile(name: string): void {
23+
delete workspaceFiles[name];
24+
}
25+
26+
// NOTE: future additions could also perform content analysis to determine format/version
27+
const workspaceFiles: Record<string, WorkspaceFormat> = {
28+
'angular.json': WorkspaceFormat.JSON,
29+
'.angular.json': WorkspaceFormat.JSON,
30+
};
31+
32+
export async function readWorkspace(
33+
path: string,
34+
host: WorkspaceHost,
35+
format?: WorkspaceFormat,
36+
): Promise<WorkspaceDefinition> {
37+
if (await host.isDirectory(path)) {
38+
// TODO: Warn if multiple found (requires diagnostics support)
39+
const directory = normalize(path);
40+
let found = false;
41+
for (const [name, nameFormat] of Object.entries(workspaceFiles)) {
42+
if (format !== undefined && format !== nameFormat) {
43+
continue;
44+
}
45+
46+
const potential = getSystemPath(join(directory, name));
47+
if (await host.isFile(potential)) {
48+
// TEMP - remove disable when actual reader is used
49+
// tslint:disable-next-line:no-dead-store
50+
path = potential;
51+
format = nameFormat;
52+
found = true;
53+
break;
54+
}
55+
}
56+
if (!found) {
57+
throw new Error('Unable to locate a workspace file for workspace path.');
58+
}
59+
} else if (format === undefined) {
60+
const filename = basename(normalize(path));
61+
if (filename in workspaceFiles) {
62+
format = workspaceFiles[filename];
63+
}
64+
}
65+
66+
if (format === undefined) {
67+
throw new Error('Unable to determine format for workspace path.');
68+
}
69+
70+
let workspace;
71+
switch (format) {
72+
case WorkspaceFormat.JSON:
73+
// TEMP: remove the following two statements when JSON support is introduced
74+
await host.readFile(path);
75+
workspace = {
76+
extensions: {},
77+
projects: new ProjectDefinitionCollection(),
78+
};
79+
break;
80+
default:
81+
throw new Error('Unsupported workspace format.');
82+
}
83+
84+
formatLookup.set(workspace, WorkspaceFormat.JSON);
85+
86+
return workspace;
87+
}
88+
89+
export async function writeWorkspace(
90+
workspace: WorkspaceDefinition,
91+
_host: WorkspaceHost,
92+
_path?: string,
93+
format?: WorkspaceFormat,
94+
): Promise<void> {
95+
if (format === undefined) {
96+
format = formatLookup.get(workspace);
97+
if (format === undefined) {
98+
throw new Error('A format is required for custom workspace objects.');
99+
}
100+
}
101+
102+
switch (format) {
103+
case WorkspaceFormat.JSON:
104+
throw new Error('Not Implemented.');
105+
default:
106+
throw new Error('Unsupported workspace format.');
107+
}
108+
}

0 commit comments

Comments
 (0)