UNPKG

3.36 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.updateBuilderConfig = exports.updateWorkspace = exports.getWorkspace = void 0;
4const tslib_1 = require("tslib");
5const core_1 = require("@angular-devkit/core");
6function createHost(tree) {
7 return {
8 readFile(path) {
9 return tslib_1.__awaiter(this, void 0, void 0, function* () {
10 const data = tree.read(path);
11 if (!data) {
12 throw new Error('File not found.');
13 }
14 return data.toString();
15 });
16 },
17 writeFile(path, data) {
18 return tslib_1.__awaiter(this, void 0, void 0, function* () {
19 return tree.overwrite(path, data);
20 });
21 },
22 isDirectory(path) {
23 return tslib_1.__awaiter(this, void 0, void 0, function* () {
24 // approximate a directory check
25 // special case needed when testing wrapped schematics
26 if (path === '/')
27 return true;
28 return !tree.exists(path) && tree.getDir(path).subfiles.length > 0;
29 });
30 },
31 isFile(path) {
32 return tslib_1.__awaiter(this, void 0, void 0, function* () {
33 return tree.exists(path);
34 });
35 },
36 };
37}
38function getWorkspace(tree, path = '/') {
39 return tslib_1.__awaiter(this, void 0, void 0, function* () {
40 const host = createHost(tree);
41 const { workspace } = yield core_1.workspaces.readWorkspace(path, host);
42 return workspace;
43 });
44}
45exports.getWorkspace = getWorkspace;
46function updateWorkspace(updaterOrWorkspace) {
47 return (tree) => tslib_1.__awaiter(this, void 0, void 0, function* () {
48 const host = createHost(tree);
49 if (typeof updaterOrWorkspace === 'function') {
50 const { workspace } = yield core_1.workspaces.readWorkspace('/', host);
51 const result = updaterOrWorkspace(workspace);
52 if (result !== undefined) {
53 yield result;
54 }
55 yield core_1.workspaces.writeWorkspace(workspace, host);
56 }
57 else {
58 yield core_1.workspaces.writeWorkspace(updaterOrWorkspace, host);
59 }
60 });
61}
62exports.updateWorkspace = updateWorkspace;
63/**
64 * Updates builder options for options and configurations for given builder names
65 */
66function updateBuilderConfig(updater, ...builderNames) {
67 return updateWorkspace((workspace) => {
68 if (!workspace.projects) {
69 return;
70 }
71 workspace.projects.forEach((project) => {
72 project.targets.forEach((target) => {
73 if (!builderNames.includes(target.builder)) {
74 return;
75 }
76 if (target.options) {
77 target.options = updater(target.options, target, project);
78 }
79 if (!target.configurations) {
80 return;
81 }
82 Object.entries(target.configurations).forEach(([configName, options]) => {
83 target.configurations[configName] = updater(options, target, project);
84 });
85 });
86 });
87 });
88}
89exports.updateBuilderConfig = updateBuilderConfig;
90//# sourceMappingURL=workspace.js.map
\No newline at end of file