[6a3a178] | 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 | */
|
---|
| 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 | * Supported workspace formats
|
---|
| 17 | */
|
---|
| 18 | var WorkspaceFormat;
|
---|
| 19 | (function (WorkspaceFormat) {
|
---|
| 20 | WorkspaceFormat[WorkspaceFormat["JSON"] = 0] = "JSON";
|
---|
| 21 | })(WorkspaceFormat = exports.WorkspaceFormat || (exports.WorkspaceFormat = {}));
|
---|
| 22 | /**
|
---|
| 23 | * @private
|
---|
| 24 | */
|
---|
| 25 | function _test_addWorkspaceFile(name, format) {
|
---|
| 26 | workspaceFiles[name] = format;
|
---|
| 27 | }
|
---|
| 28 | exports._test_addWorkspaceFile = _test_addWorkspaceFile;
|
---|
| 29 | /**
|
---|
| 30 | * @private
|
---|
| 31 | */
|
---|
| 32 | function _test_removeWorkspaceFile(name) {
|
---|
| 33 | delete workspaceFiles[name];
|
---|
| 34 | }
|
---|
| 35 | exports._test_removeWorkspaceFile = _test_removeWorkspaceFile;
|
---|
| 36 | // NOTE: future additions could also perform content analysis to determine format/version
|
---|
| 37 | const 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 | */
|
---|
| 57 | async function readWorkspace(path, host, format) {
|
---|
| 58 | if (await host.isDirectory(path)) {
|
---|
| 59 | // TODO: Warn if multiple found (requires diagnostics support)
|
---|
| 60 | const directory = 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 = virtual_fs_1.getSystemPath(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.');
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | else if (format === undefined) {
|
---|
| 79 | const filename = virtual_fs_1.basename(virtual_fs_1.normalize(path));
|
---|
| 80 | if (filename in workspaceFiles) {
|
---|
| 81 | format = workspaceFiles[filename];
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | if (format === undefined) {
|
---|
| 85 | throw new Error('Unable to determine format for workspace path.');
|
---|
| 86 | }
|
---|
| 87 | let workspace;
|
---|
| 88 | switch (format) {
|
---|
| 89 | case WorkspaceFormat.JSON:
|
---|
| 90 | workspace = await reader_1.readJsonWorkspace(path, host);
|
---|
| 91 | break;
|
---|
| 92 | default:
|
---|
| 93 | throw new Error('Unsupported workspace format.');
|
---|
| 94 | }
|
---|
| 95 | formatLookup.set(workspace, WorkspaceFormat.JSON);
|
---|
| 96 | return { workspace };
|
---|
| 97 | }
|
---|
| 98 | exports.readWorkspace = readWorkspace;
|
---|
| 99 | /**
|
---|
| 100 | * Writes a `WorkspaceDefinition` to the underlying storage via the provided `WorkspaceHost`.
|
---|
| 101 | * If the `WorkspaceDefinition` was created via the `readWorkspace` function, metadata will be
|
---|
| 102 | * used to determine the path and format of the Workspace. In all other cases, the `path` and
|
---|
| 103 | * `format` options must be specified as they would be otherwise unknown.
|
---|
| 104 | *
|
---|
| 105 | * @param workspace The `WorkspaceDefinition` that will be written.
|
---|
| 106 | * @param host The `WorkspaceHost` to use to access/write the file and directory data.
|
---|
| 107 | * @param path The path to a file location for the output. Required if `readWorkspace` was not
|
---|
| 108 | * used to create the `WorkspaceDefinition`. Optional otherwise; will override the
|
---|
| 109 | * `WorkspaceDefinition` metadata if provided.
|
---|
| 110 | * @param format The `WorkspaceFormat` to use for output. Required if `readWorkspace` was not
|
---|
| 111 | * used to create the `WorkspaceDefinition`. Optional otherwise; will override the
|
---|
| 112 | * `WorkspaceDefinition` metadata if provided.
|
---|
| 113 | *
|
---|
| 114 | *
|
---|
| 115 | * @return An `Promise` of type `void`.
|
---|
| 116 | */
|
---|
| 117 | async function writeWorkspace(workspace, host, path, format) {
|
---|
| 118 | if (format === undefined) {
|
---|
| 119 | format = formatLookup.get(workspace);
|
---|
| 120 | if (format === undefined) {
|
---|
| 121 | throw new Error('A format is required for custom workspace objects.');
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | switch (format) {
|
---|
| 125 | case WorkspaceFormat.JSON:
|
---|
| 126 | return writer_1.writeJsonWorkspace(workspace, host, path);
|
---|
| 127 | default:
|
---|
| 128 | throw new Error('Unsupported workspace format.');
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 | exports.writeWorkspace = writeWorkspace;
|
---|