1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | exports.NodeModulesEngineHost = exports.NodePackageDoesNotSupportSchematics = void 0;
|
11 | const core_1 = require("@angular-devkit/core");
|
12 | const path_1 = require("path");
|
13 | const export_ref_1 = require("./export-ref");
|
14 | const file_system_engine_host_base_1 = require("./file-system-engine-host-base");
|
15 | const file_system_utility_1 = require("./file-system-utility");
|
16 | class NodePackageDoesNotSupportSchematics extends core_1.BaseException {
|
17 | constructor(name) {
|
18 | super(`Package ${JSON.stringify(name)} was found but does not support schematics.`);
|
19 | }
|
20 | }
|
21 | exports.NodePackageDoesNotSupportSchematics = NodePackageDoesNotSupportSchematics;
|
22 |
|
23 |
|
24 |
|
25 | class NodeModulesEngineHost extends file_system_engine_host_base_1.FileSystemEngineHostBase {
|
26 | constructor(paths) {
|
27 | super();
|
28 | this.paths = paths;
|
29 | }
|
30 | resolve(name, requester, references = new Set()) {
|
31 | if (requester) {
|
32 | if (references.has(requester)) {
|
33 | references.add(requester);
|
34 | throw new Error('Circular schematic reference detected: ' + JSON.stringify(Array.from(references)));
|
35 | }
|
36 | else {
|
37 | references.add(requester);
|
38 | }
|
39 | }
|
40 | const relativeBase = requester ? (0, path_1.dirname)(requester) : process.cwd();
|
41 | let collectionPath = undefined;
|
42 | if (name.startsWith('.')) {
|
43 | name = (0, path_1.resolve)(relativeBase, name);
|
44 | }
|
45 | const resolveOptions = {
|
46 | paths: requester ? [(0, path_1.dirname)(requester), ...(this.paths || [])] : this.paths,
|
47 | };
|
48 |
|
49 | try {
|
50 | const packageJsonPath = require.resolve((0, path_1.join)(name, 'package.json'), resolveOptions);
|
51 | const { schematics } = require(packageJsonPath);
|
52 | if (!schematics || typeof schematics !== 'string') {
|
53 | throw new NodePackageDoesNotSupportSchematics(name);
|
54 | }
|
55 | collectionPath = this.resolve(schematics, packageJsonPath, references);
|
56 | }
|
57 | catch (e) {
|
58 | if (e.code !== 'MODULE_NOT_FOUND') {
|
59 | throw e;
|
60 | }
|
61 | }
|
62 |
|
63 | if (!collectionPath) {
|
64 | try {
|
65 | collectionPath = require.resolve(name, resolveOptions);
|
66 | }
|
67 | catch (e) {
|
68 | if (e.code !== 'MODULE_NOT_FOUND') {
|
69 | throw e;
|
70 | }
|
71 | }
|
72 | }
|
73 |
|
74 | if (!collectionPath) {
|
75 | throw new file_system_engine_host_base_1.CollectionCannotBeResolvedException(name);
|
76 | }
|
77 | return collectionPath;
|
78 | }
|
79 | _resolveCollectionPath(name, requester) {
|
80 | const collectionPath = this.resolve(name, requester);
|
81 | (0, file_system_utility_1.readJsonFile)(collectionPath);
|
82 | return collectionPath;
|
83 | }
|
84 | _resolveReferenceString(refString, parentPath, collectionDescription) {
|
85 | const ref = new export_ref_1.ExportStringRef(refString, parentPath);
|
86 | if (!ref.ref) {
|
87 | return null;
|
88 | }
|
89 | return { ref: ref.ref, path: ref.module };
|
90 | }
|
91 | _transformCollectionDescription(name, desc) {
|
92 | if (!desc.schematics || typeof desc.schematics != 'object') {
|
93 | throw new file_system_engine_host_base_1.CollectionMissingSchematicsMapException(name);
|
94 | }
|
95 | return {
|
96 | ...desc,
|
97 | name,
|
98 | };
|
99 | }
|
100 | _transformSchematicDescription(name, _collection, desc) {
|
101 | if (!desc.factoryFn || !desc.path || !desc.description) {
|
102 | throw new file_system_engine_host_base_1.SchematicMissingFieldsException(name);
|
103 | }
|
104 | return desc;
|
105 | }
|
106 | }
|
107 | exports.NodeModulesEngineHost = NodeModulesEngineHost;
|