1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.createDefaultPath = exports.buildDefaultPath = exports.getWorkspace = exports.updateWorkspace = void 0;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const core_1 = require("@angular-devkit/core");
|
12 | const workspace_models_1 = require("./workspace-models");
|
13 | function createHost(tree) {
|
14 | return {
|
15 | async readFile(path) {
|
16 | const data = tree.read(path);
|
17 | if (!data) {
|
18 | throw new Error('File not found.');
|
19 | }
|
20 | return core_1.virtualFs.fileBufferToString(data);
|
21 | },
|
22 | async writeFile(path, data) {
|
23 | return tree.overwrite(path, data);
|
24 | },
|
25 | async isDirectory(path) {
|
26 |
|
27 | return !tree.exists(path) && tree.getDir(path).subfiles.length > 0;
|
28 | },
|
29 | async isFile(path) {
|
30 | return tree.exists(path);
|
31 | },
|
32 | };
|
33 | }
|
34 | function updateWorkspace(updaterOrWorkspace) {
|
35 | return async (tree) => {
|
36 | const host = createHost(tree);
|
37 | if (typeof updaterOrWorkspace === 'function') {
|
38 | const { workspace } = await core_1.workspaces.readWorkspace('/', host);
|
39 | const result = updaterOrWorkspace(workspace);
|
40 | if (result !== undefined) {
|
41 | await result;
|
42 | }
|
43 | await core_1.workspaces.writeWorkspace(workspace, host);
|
44 | }
|
45 | else {
|
46 | await core_1.workspaces.writeWorkspace(updaterOrWorkspace, host);
|
47 | }
|
48 | };
|
49 | }
|
50 | exports.updateWorkspace = updateWorkspace;
|
51 | async function getWorkspace(tree, path = '/') {
|
52 | const host = createHost(tree);
|
53 | const { workspace } = await core_1.workspaces.readWorkspace(path, host);
|
54 | return workspace;
|
55 | }
|
56 | exports.getWorkspace = getWorkspace;
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | function 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 | }
|
66 | exports.buildDefaultPath = buildDefaultPath;
|
67 | async function createDefaultPath(tree, projectName) {
|
68 | const workspace = await getWorkspace(tree);
|
69 | const project = workspace.projects.get(projectName);
|
70 | if (!project) {
|
71 | throw new Error('Specified project does not exist.');
|
72 | }
|
73 | return buildDefaultPath(project);
|
74 | }
|
75 | exports.createDefaultPath = createDefaultPath;
|