1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | exports.writeWorkspace = exports.readWorkspace = exports._test_removeWorkspaceFile = exports._test_addWorkspaceFile = exports.WorkspaceFormat = void 0;
|
11 | const virtual_fs_1 = require("../virtual-fs");
|
12 | const reader_1 = require("./json/reader");
|
13 | const writer_1 = require("./json/writer");
|
14 | const formatLookup = new WeakMap();
|
15 |
|
16 |
|
17 |
|
18 | var WorkspaceFormat;
|
19 | (function (WorkspaceFormat) {
|
20 | WorkspaceFormat[WorkspaceFormat["JSON"] = 0] = "JSON";
|
21 | })(WorkspaceFormat = exports.WorkspaceFormat || (exports.WorkspaceFormat = {}));
|
22 |
|
23 |
|
24 |
|
25 | function _test_addWorkspaceFile(name, format) {
|
26 | workspaceFiles[name] = format;
|
27 | }
|
28 | exports._test_addWorkspaceFile = _test_addWorkspaceFile;
|
29 |
|
30 |
|
31 |
|
32 | function _test_removeWorkspaceFile(name) {
|
33 | delete workspaceFiles[name];
|
34 | }
|
35 | exports._test_removeWorkspaceFile = _test_removeWorkspaceFile;
|
36 |
|
37 | const workspaceFiles = {
|
38 | 'angular.json': WorkspaceFormat.JSON,
|
39 | '.angular.json': WorkspaceFormat.JSON,
|
40 | };
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | async function readWorkspace(path, host, format) {
|
58 | if (await host.isDirectory(path)) {
|
59 |
|
60 | const directory = (0, virtual_fs_1.normalize)(path);
|
61 | let found = false;
|
62 | for (const [name, nameFormat] of Object.entries(workspaceFiles)) {
|
63 | if (format !== undefined && format !== nameFormat) {
|
64 | continue;
|
65 | }
|
66 | const potential = (0, virtual_fs_1.getSystemPath)((0, virtual_fs_1.join)(directory, name));
|
67 | if (await host.isFile(potential)) {
|
68 | path = potential;
|
69 | format = nameFormat;
|
70 | found = true;
|
71 | break;
|
72 | }
|
73 | }
|
74 | if (!found) {
|
75 | throw new Error('Unable to locate a workspace file for workspace path. Are you missing an `angular.json`' +
|
76 | ' or `.angular.json` file?');
|
77 | }
|
78 | }
|
79 | else if (format === undefined) {
|
80 | const filename = (0, virtual_fs_1.basename)((0, virtual_fs_1.normalize)(path));
|
81 | if (filename in workspaceFiles) {
|
82 | format = workspaceFiles[filename];
|
83 | }
|
84 | }
|
85 | if (format === undefined) {
|
86 | throw new Error('Unable to determine format for workspace path.');
|
87 | }
|
88 | let workspace;
|
89 | switch (format) {
|
90 | case WorkspaceFormat.JSON:
|
91 | workspace = await (0, reader_1.readJsonWorkspace)(path, host);
|
92 | break;
|
93 | default:
|
94 | throw new Error('Unsupported workspace format.');
|
95 | }
|
96 | formatLookup.set(workspace, WorkspaceFormat.JSON);
|
97 | return { workspace };
|
98 | }
|
99 | exports.readWorkspace = readWorkspace;
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | async function writeWorkspace(workspace, host, path, format) {
|
119 | if (format === undefined) {
|
120 | format = formatLookup.get(workspace);
|
121 | if (format === undefined) {
|
122 | throw new Error('A format is required for custom workspace objects.');
|
123 | }
|
124 | }
|
125 | switch (format) {
|
126 | case WorkspaceFormat.JSON:
|
127 | return (0, writer_1.writeJsonWorkspace)(workspace, host, path);
|
128 | default:
|
129 | throw new Error('Unsupported workspace format.');
|
130 | }
|
131 | }
|
132 | exports.writeWorkspace = writeWorkspace;
|