1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
10 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
11 | };
|
12 | var _a;
|
13 | Object.defineProperty(exports, "__esModule", { value: true });
|
14 | exports.SchematicEngineHost = void 0;
|
15 | const schematics_1 = require("@angular-devkit/schematics");
|
16 | const tools_1 = require("@angular-devkit/schematics/tools");
|
17 | const fs_1 = require("fs");
|
18 | const jsonc_parser_1 = require("jsonc-parser");
|
19 | const module_1 = __importDefault(require("module"));
|
20 | const path_1 = require("path");
|
21 | const vm_1 = require("vm");
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | const schematicRedirectVariable = (_a = process.env['NG_SCHEMATIC_REDIRECT']) === null || _a === void 0 ? void 0 : _a.toLowerCase();
|
27 | function shouldWrapSchematic(schematicFile) {
|
28 |
|
29 | if (schematicRedirectVariable !== undefined) {
|
30 | switch (schematicRedirectVariable) {
|
31 | case '0':
|
32 | case 'false':
|
33 | case 'off':
|
34 | case 'none':
|
35 | return false;
|
36 | case 'all':
|
37 | return true;
|
38 | }
|
39 | }
|
40 | const normalizedSchematicFile = schematicFile.replace(/\\/g, '/');
|
41 |
|
42 |
|
43 |
|
44 | if (normalizedSchematicFile.includes('node_modules/@angular/cli/') &&
|
45 | !normalizedSchematicFile.includes('node_modules/@angular/cli/node_modules/')) {
|
46 | return false;
|
47 | }
|
48 |
|
49 |
|
50 | return /\/node_modules\/@(?:angular|schematics|nguniversal)\//.test(normalizedSchematicFile);
|
51 | }
|
52 | class SchematicEngineHost extends tools_1.NodeModulesEngineHost {
|
53 | _resolveReferenceString(refString, parentPath) {
|
54 | const [path, name] = refString.split('#', 2);
|
55 |
|
56 | const fullPath = path[0] === '.' ? path_1.resolve(parentPath !== null && parentPath !== void 0 ? parentPath : process.cwd(), path) : path;
|
57 | const schematicFile = require.resolve(fullPath, { paths: [parentPath] });
|
58 | if (shouldWrapSchematic(schematicFile)) {
|
59 | const schematicPath = path_1.dirname(schematicFile);
|
60 | const moduleCache = new Map();
|
61 | const factoryInitializer = wrap(schematicFile, schematicPath, moduleCache, name || 'default');
|
62 | const factory = factoryInitializer();
|
63 | if (!factory || typeof factory !== 'function') {
|
64 | return null;
|
65 | }
|
66 | return { ref: factory, path: schematicPath };
|
67 | }
|
68 |
|
69 | return super._resolveReferenceString(refString, parentPath);
|
70 | }
|
71 | }
|
72 | exports.SchematicEngineHost = SchematicEngineHost;
|
73 |
|
74 |
|
75 |
|
76 | const legacyModules = {
|
77 | '@schematics/angular/utility/config': {
|
78 | getWorkspace(host) {
|
79 | const path = '/.angular.json';
|
80 | const data = host.read(path);
|
81 | if (!data) {
|
82 | throw new schematics_1.SchematicsException(`Could not find (${path})`);
|
83 | }
|
84 | return jsonc_parser_1.parse(data.toString(), [], { allowTrailingComma: true });
|
85 | },
|
86 | },
|
87 | '@schematics/angular/utility/project': {
|
88 | buildDefaultPath(project) {
|
89 | const root = project.sourceRoot ? `/${project.sourceRoot}/` : `/${project.root}/src/`;
|
90 | return `${root}${project.projectType === 'application' ? 'app' : 'lib'}`;
|
91 | },
|
92 | },
|
93 | };
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | function wrap(schematicFile, schematicDirectory, moduleCache, exportName) {
|
104 | const scopedRequire = module_1.default.createRequire(schematicFile);
|
105 | const customRequire = function (id) {
|
106 | if (legacyModules[id]) {
|
107 |
|
108 | return legacyModules[id];
|
109 | }
|
110 | else if (id.startsWith('@angular-devkit/') || id.startsWith('@schematics/')) {
|
111 |
|
112 | const packagePath = require.resolve(id);
|
113 | return require(packagePath);
|
114 | }
|
115 | else if (id.startsWith('.') || id.startsWith('@angular/cdk')) {
|
116 |
|
117 |
|
118 |
|
119 | const modulePath = scopedRequire.resolve(id);
|
120 |
|
121 | const cachedModule = moduleCache.get(modulePath);
|
122 | if (cachedModule) {
|
123 | return cachedModule;
|
124 | }
|
125 |
|
126 | if (!/[\/\\]node_modules[\/\\]@schematics[\/\\]angular[\/\\]third_party[\/\\]/.test(modulePath) &&
|
127 | !modulePath.endsWith('.json')) {
|
128 |
|
129 | const wrappedModule = wrap(modulePath, path_1.dirname(modulePath), moduleCache)();
|
130 | moduleCache.set(modulePath, wrappedModule);
|
131 | return wrappedModule;
|
132 | }
|
133 | }
|
134 |
|
135 | return scopedRequire(id);
|
136 | };
|
137 |
|
138 | const schematicCode = fs_1.readFileSync(schematicFile, 'utf8');
|
139 |
|
140 | const headerCode = '(function() {\nvar exports = {};\nvar module = { exports };\n';
|
141 | const footerCode = exportName ? `\nreturn exports['${exportName}'];});` : '\nreturn exports;});';
|
142 | const script = new vm_1.Script(headerCode + schematicCode + footerCode, {
|
143 | filename: schematicFile,
|
144 | lineOffset: 3,
|
145 | });
|
146 | const context = {
|
147 | __dirname: schematicDirectory,
|
148 | __filename: schematicFile,
|
149 | Buffer,
|
150 | console,
|
151 | process,
|
152 | get global() {
|
153 | return this;
|
154 | },
|
155 | require: customRequire,
|
156 | };
|
157 | const exportsFactory = script.runInNewContext(context);
|
158 | return exportsFactory;
|
159 | }
|