UNPKG

3.64 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.allTargetOptions = exports.allWorkspaceTargets = exports.createDefaultPath = exports.buildDefaultPath = exports.getWorkspace = exports.updateWorkspace = void 0;
11const core_1 = require("@angular-devkit/core");
12const schematics_1 = require("@angular-devkit/schematics");
13const workspace_models_1 = require("./workspace-models");
14function createHost(tree) {
15 return {
16 async readFile(path) {
17 const data = tree.read(path);
18 if (!data) {
19 throw new Error('File not found.');
20 }
21 return core_1.virtualFs.fileBufferToString(data);
22 },
23 async writeFile(path, data) {
24 return tree.overwrite(path, data);
25 },
26 async isDirectory(path) {
27 // approximate a directory check
28 return !tree.exists(path) && tree.getDir(path).subfiles.length > 0;
29 },
30 async isFile(path) {
31 return tree.exists(path);
32 },
33 };
34}
35function updateWorkspace(updaterOrWorkspace) {
36 return async (tree) => {
37 const host = createHost(tree);
38 if (typeof updaterOrWorkspace === 'function') {
39 const { workspace } = await core_1.workspaces.readWorkspace('/', host);
40 const result = await updaterOrWorkspace(workspace);
41 await core_1.workspaces.writeWorkspace(workspace, host);
42 return result || schematics_1.noop;
43 }
44 else {
45 await core_1.workspaces.writeWorkspace(updaterOrWorkspace, host);
46 return schematics_1.noop;
47 }
48 };
49}
50exports.updateWorkspace = updateWorkspace;
51async function getWorkspace(tree, path = '/') {
52 const host = createHost(tree);
53 const { workspace } = await core_1.workspaces.readWorkspace(path, host);
54 return workspace;
55}
56exports.getWorkspace = getWorkspace;
57/**
58 * Build a default project path for generating.
59 * @param project The project which will have its default path generated.
60 */
61function buildDefaultPath(project) {
62 const root = project.sourceRoot ? `/${project.sourceRoot}/` : `/${project.root}/src/`;
63 const projectDirName = project.extensions['projectType'] === workspace_models_1.ProjectType.Application ? 'app' : 'lib';
64 return `${root}${projectDirName}`;
65}
66exports.buildDefaultPath = buildDefaultPath;
67async function createDefaultPath(tree, projectName) {
68 const workspace = await getWorkspace(tree);
69 const project = workspace.projects.get(projectName);
70 if (!project) {
71 throw new Error(`Project "${projectName}" does not exist.`);
72 }
73 return buildDefaultPath(project);
74}
75exports.createDefaultPath = createDefaultPath;
76function* allWorkspaceTargets(workspace) {
77 for (const [projectName, project] of workspace.projects) {
78 for (const [targetName, target] of project.targets) {
79 yield [targetName, target, projectName, project];
80 }
81 }
82}
83exports.allWorkspaceTargets = allWorkspaceTargets;
84function* allTargetOptions(target, skipBaseOptions = false) {
85 if (!skipBaseOptions && target.options) {
86 yield [undefined, target.options];
87 }
88 if (!target.configurations) {
89 return;
90 }
91 for (const [name, options] of Object.entries(target.configurations)) {
92 if (options !== undefined) {
93 yield [name, options];
94 }
95 }
96}
97exports.allTargetOptions = allTargetOptions;