UNPKG

5.38 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright Google LLC All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9Object.defineProperty(exports, "__esModule", { value: true });
10exports.writeWorkspace = exports.readWorkspace = exports._test_removeWorkspaceFile = exports._test_addWorkspaceFile = exports.WorkspaceFormat = void 0;
11const virtual_fs_1 = require("../virtual-fs");
12const reader_1 = require("./json/reader");
13const writer_1 = require("./json/writer");
14const formatLookup = new WeakMap();
15/**
16 * Supported workspace formats
17 */
18var WorkspaceFormat;
19(function (WorkspaceFormat) {
20 WorkspaceFormat[WorkspaceFormat["JSON"] = 0] = "JSON";
21})(WorkspaceFormat = exports.WorkspaceFormat || (exports.WorkspaceFormat = {}));
22/**
23 * @private
24 */
25function _test_addWorkspaceFile(name, format) {
26 workspaceFiles[name] = format;
27}
28exports._test_addWorkspaceFile = _test_addWorkspaceFile;
29/**
30 * @private
31 */
32function _test_removeWorkspaceFile(name) {
33 delete workspaceFiles[name];
34}
35exports._test_removeWorkspaceFile = _test_removeWorkspaceFile;
36// NOTE: future additions could also perform content analysis to determine format/version
37const workspaceFiles = {
38 'angular.json': WorkspaceFormat.JSON,
39 '.angular.json': WorkspaceFormat.JSON,
40};
41/**
42 * Reads and constructs a `WorkspaceDefinition`. If the function is provided with a path to a
43 * directory instead of a file, a search of the directory's files will commence to attempt to
44 * locate a known workspace file. Currently the following are considered known workspace files:
45 * - `angular.json`
46 * - `.angular.json`
47 *
48 * @param path The path to either a workspace file or a directory containing a workspace file.
49 * @param host The `WorkspaceHost` to use to access the file and directory data.
50 * @param format An optional `WorkspaceFormat` value. Used if the path specifies a non-standard
51 * file name that would prevent automatically discovering the format.
52 *
53 *
54 * @return An `Promise` of the read result object with the `WorkspaceDefinition` contained within
55 * the `workspace` property.
56 */
57async function readWorkspace(path, host, format) {
58 if (await host.isDirectory(path)) {
59 // TODO: Warn if multiple found (requires diagnostics support)
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}
99exports.readWorkspace = readWorkspace;
100/**
101 * Writes a `WorkspaceDefinition` to the underlying storage via the provided `WorkspaceHost`.
102 * If the `WorkspaceDefinition` was created via the `readWorkspace` function, metadata will be
103 * used to determine the path and format of the Workspace. In all other cases, the `path` and
104 * `format` options must be specified as they would be otherwise unknown.
105 *
106 * @param workspace The `WorkspaceDefinition` that will be written.
107 * @param host The `WorkspaceHost` to use to access/write the file and directory data.
108 * @param path The path to a file location for the output. Required if `readWorkspace` was not
109 * used to create the `WorkspaceDefinition`. Optional otherwise; will override the
110 * `WorkspaceDefinition` metadata if provided.
111 * @param format The `WorkspaceFormat` to use for output. Required if `readWorkspace` was not
112 * used to create the `WorkspaceDefinition`. Optional otherwise; will override the
113 * `WorkspaceDefinition` metadata if provided.
114 *
115 *
116 * @return An `Promise` of type `void`.
117 */
118async 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}
132exports.writeWorkspace = writeWorkspace;