UNPKG

2.58 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/**
4 * @license
5 * Copyright Google Inc. All Rights Reserved.
6 *
7 * Use of this source code is governed by an MIT-style license that can be
8 * found in the LICENSE file at https://angular.io/license
9 */
10const core_1 = require("@angular-devkit/core");
11function createHost(tree) {
12 return {
13 async readFile(path) {
14 const data = tree.read(path);
15 if (!data) {
16 throw new Error('File not found.');
17 }
18 return core_1.virtualFs.fileBufferToString(data);
19 },
20 async writeFile(path, data) {
21 return tree.overwrite(path, data);
22 },
23 async isDirectory(path) {
24 // approximate a directory check
25 return !tree.exists(path) && tree.getDir(path).subfiles.length > 0;
26 },
27 async isFile(path) {
28 return tree.exists(path);
29 },
30 };
31}
32function updateWorkspace(updaterOrWorkspace) {
33 return async (tree) => {
34 const host = createHost(tree);
35 if (typeof updaterOrWorkspace === 'function') {
36 const { workspace } = await core_1.workspaces.readWorkspace('/', host);
37 const result = updaterOrWorkspace(workspace);
38 if (result !== undefined) {
39 await result;
40 }
41 await core_1.workspaces.writeWorkspace(workspace, host);
42 }
43 else {
44 await core_1.workspaces.writeWorkspace(updaterOrWorkspace, host);
45 }
46 };
47}
48exports.updateWorkspace = updateWorkspace;
49async function getWorkspace(tree, path = '/') {
50 const host = createHost(tree);
51 const { workspace } = await core_1.workspaces.readWorkspace(path, host);
52 return workspace;
53}
54exports.getWorkspace = getWorkspace;
55/**
56 * Build a default project path for generating.
57 * @param project The project which will have its default path generated.
58 */
59function buildDefaultPath(project) {
60 const root = project.sourceRoot ? `/${project.sourceRoot}/` : `/${project.root}/src/`;
61 const projectDirName = project.extensions['projectType'] === 'application' ? 'app' : 'lib';
62 return `${root}${projectDirName}`;
63}
64exports.buildDefaultPath = buildDefaultPath;
65async function createDefaultPath(tree, projectName) {
66 const workspace = await getWorkspace(tree);
67 const project = workspace.projects.get(projectName);
68 if (!project) {
69 throw new Error('Specified project does not exist.');
70 }
71 return buildDefaultPath(project);
72}
73exports.createDefaultPath = createDefaultPath;