UNPKG

8.14 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 */
9var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
12}) : (function(o, m, k, k2) {
13 if (k2 === undefined) k2 = k;
14 o[k2] = m[k];
15}));
16var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
17 Object.defineProperty(o, "default", { enumerable: true, value: v });
18}) : function(o, v) {
19 o["default"] = v;
20});
21var __importStar = (this && this.__importStar) || function (mod) {
22 if (mod && mod.__esModule) return mod;
23 var result = {};
24 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
25 __setModuleDefault(result, mod);
26 return result;
27};
28Object.defineProperty(exports, "__esModule", { value: true });
29exports.WorkspaceNodeModulesArchitectHost = void 0;
30const path = __importStar(require("path"));
31const v8_1 = require("v8");
32const internal_1 = require("../src/internal");
33function clone(obj) {
34 try {
35 return (0, v8_1.deserialize)((0, v8_1.serialize)(obj));
36 }
37 catch {
38 return JSON.parse(JSON.stringify(obj));
39 }
40}
41function findProjectTarget(workspace, project, target) {
42 const projectDefinition = workspace.projects.get(project);
43 if (!projectDefinition) {
44 throw new Error(`Project "${project}" does not exist.`);
45 }
46 const targetDefinition = projectDefinition.targets.get(target);
47 if (!targetDefinition) {
48 throw new Error('Project target does not exist.');
49 }
50 return targetDefinition;
51}
52class WorkspaceNodeModulesArchitectHost {
53 constructor(workspaceOrHost, _root) {
54 this._root = _root;
55 if ('getBuilderName' in workspaceOrHost) {
56 this.workspaceHost = workspaceOrHost;
57 }
58 else {
59 this.workspaceHost = {
60 async getBuilderName(project, target) {
61 const targetDefinition = findProjectTarget(workspaceOrHost, project, target);
62 return targetDefinition.builder;
63 },
64 async getOptions(project, target, configuration) {
65 var _a, _b, _c, _d;
66 const targetDefinition = findProjectTarget(workspaceOrHost, project, target);
67 if (configuration === undefined) {
68 return ((_a = targetDefinition.options) !== null && _a !== void 0 ? _a : {});
69 }
70 if (!((_b = targetDefinition.configurations) === null || _b === void 0 ? void 0 : _b[configuration])) {
71 throw new Error(`Configuration '${configuration}' is not set in the workspace.`);
72 }
73 return ((_d = (_c = targetDefinition.configurations) === null || _c === void 0 ? void 0 : _c[configuration]) !== null && _d !== void 0 ? _d : {});
74 },
75 async getMetadata(project) {
76 const projectDefinition = workspaceOrHost.projects.get(project);
77 if (!projectDefinition) {
78 throw new Error(`Project "${project}" does not exist.`);
79 }
80 return {
81 root: projectDefinition.root,
82 sourceRoot: projectDefinition.sourceRoot,
83 prefix: projectDefinition.prefix,
84 ...clone(workspaceOrHost.extensions),
85 ...clone(projectDefinition.extensions),
86 };
87 },
88 async hasTarget(project, target) {
89 var _a;
90 return !!((_a = workspaceOrHost.projects.get(project)) === null || _a === void 0 ? void 0 : _a.targets.has(target));
91 },
92 async getDefaultConfigurationName(project, target) {
93 var _a, _b;
94 return (_b = (_a = workspaceOrHost.projects.get(project)) === null || _a === void 0 ? void 0 : _a.targets.get(target)) === null || _b === void 0 ? void 0 : _b.defaultConfiguration;
95 },
96 };
97 }
98 }
99 async getBuilderNameForTarget(target) {
100 return this.workspaceHost.getBuilderName(target.project, target.target);
101 }
102 /**
103 * Resolve a builder. This needs to be a string which will be used in a dynamic `import()`
104 * clause. This should throw if no builder can be found. The dynamic import will throw if
105 * it is unsupported.
106 * @param builderStr The name of the builder to be used.
107 * @returns All the info needed for the builder itself.
108 */
109 resolveBuilder(builderStr) {
110 const [packageName, builderName] = builderStr.split(':', 2);
111 if (!builderName) {
112 throw new Error('No builder name specified.');
113 }
114 const packageJsonPath = require.resolve(packageName + '/package.json', {
115 paths: [this._root],
116 });
117 const packageJson = require(packageJsonPath);
118 if (!packageJson['builders']) {
119 throw new Error(`Package ${JSON.stringify(packageName)} has no builders defined.`);
120 }
121 const builderJsonPath = path.resolve(path.dirname(packageJsonPath), packageJson['builders']);
122 const builderJson = require(builderJsonPath);
123 const builder = builderJson.builders && builderJson.builders[builderName];
124 if (!builder) {
125 throw new Error(`Cannot find builder ${JSON.stringify(builderStr)}.`);
126 }
127 const importPath = builder.implementation;
128 if (!importPath) {
129 throw new Error('Could not find the implementation for builder ' + builderStr);
130 }
131 return Promise.resolve({
132 name: builderStr,
133 builderName,
134 description: builder['description'],
135 optionSchema: require(path.resolve(path.dirname(builderJsonPath), builder.schema)),
136 import: path.resolve(path.dirname(builderJsonPath), importPath),
137 });
138 }
139 async getCurrentDirectory() {
140 return process.cwd();
141 }
142 async getWorkspaceRoot() {
143 return this._root;
144 }
145 async getOptionsForTarget(target) {
146 if (!(await this.workspaceHost.hasTarget(target.project, target.target))) {
147 return null;
148 }
149 let options = await this.workspaceHost.getOptions(target.project, target.target);
150 const targetConfiguration = target.configuration ||
151 (await this.workspaceHost.getDefaultConfigurationName(target.project, target.target));
152 if (targetConfiguration) {
153 const configurations = targetConfiguration.split(',').map((c) => c.trim());
154 for (const configuration of configurations) {
155 options = {
156 ...options,
157 ...(await this.workspaceHost.getOptions(target.project, target.target, configuration)),
158 };
159 }
160 }
161 return clone(options);
162 }
163 async getProjectMetadata(target) {
164 const projectName = typeof target === 'string' ? target : target.project;
165 const metadata = this.workspaceHost.getMetadata(projectName);
166 return metadata;
167 }
168 async loadBuilder(info) {
169 const builder = (await Promise.resolve().then(() => __importStar(require(info.import)))).default;
170 if (builder[internal_1.BuilderSymbol]) {
171 return builder;
172 }
173 // Default handling code is for old builders that incorrectly export `default` with non-ESM module
174 if (builder === null || builder === void 0 ? void 0 : builder.default[internal_1.BuilderSymbol]) {
175 return builder.default;
176 }
177 throw new Error('Builder is not a builder');
178 }
179}
180exports.WorkspaceNodeModulesArchitectHost = WorkspaceNodeModulesArchitectHost;