UNPKG

2.35 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.checkTargets = void 0;
4const devkit_1 = require("@nrwl/devkit");
5/**
6 * Check whether the project to be removed has builders targetted by another project
7 *
8 * Throws an error if the project is in use, unless the `--forceRemove` option is used.
9 *
10 * @param schema The options provided to the schematic
11 */
12function checkTargets(tree, schema) {
13 if (schema.forceRemove) {
14 return;
15 }
16 const errors = [];
17 (0, devkit_1.getProjects)(tree).forEach((projectConfig, projectName) => {
18 if (projectName === schema.projectName) {
19 return;
20 }
21 Object.entries(projectConfig.targets || {}).forEach(([, targetConfig]) => {
22 checkIfProjectIsUsed(targetConfig, (value) => {
23 try {
24 const { project } = (0, devkit_1.parseTargetString)(value);
25 if (project === schema.projectName) {
26 errors.push(`"${value}" is used by "${projectName}"`);
27 }
28 }
29 catch (err) {
30 /**
31 * It threw because the target string was not
32 * in the format of project:target:configuration
33 *
34 * In that case, we don't care about it.
35 * So we can ignore this error.
36 */
37 }
38 });
39 });
40 });
41 if (errors.length > 0) {
42 let message = `${schema.projectName} is still targeted by some projects:\n\n`;
43 for (let error of errors) {
44 message += `${error}\n`;
45 }
46 throw new Error(message);
47 }
48}
49exports.checkTargets = checkTargets;
50function checkIfProjectIsUsed(config, callback) {
51 function recur(obj, key, value) {
52 if (typeof value === 'string') {
53 callback(value);
54 }
55 else if (Array.isArray(value)) {
56 value.forEach((x, idx) => recur(value, idx, x));
57 }
58 else if (typeof value === 'object') {
59 Object.entries(value).forEach(([k, v]) => {
60 recur(value, k, v);
61 });
62 }
63 }
64 Object.entries(config).forEach(([k, v]) => {
65 recur(config, k, v);
66 });
67}
68//# sourceMappingURL=check-targets.js.map
\No newline at end of file