UNPKG

4.17 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 */
9Object.defineProperty(exports, "__esModule", { value: true });
10exports.NodeModulesEngineHost = exports.NodePackageDoesNotSupportSchematics = void 0;
11const core_1 = require("@angular-devkit/core");
12const path_1 = require("path");
13const export_ref_1 = require("./export-ref");
14const file_system_engine_host_base_1 = require("./file-system-engine-host-base");
15const file_system_utility_1 = require("./file-system-utility");
16class NodePackageDoesNotSupportSchematics extends core_1.BaseException {
17 constructor(name) {
18 super(`Package ${JSON.stringify(name)} was found but does not support schematics.`);
19 }
20}
21exports.NodePackageDoesNotSupportSchematics = NodePackageDoesNotSupportSchematics;
22/**
23 * A simple EngineHost that uses NodeModules to resolve collections.
24 */
25class 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 // Try to resolve as a package
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 // If not a package, try to resolve as a file
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 // If not a package or a file, error
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}
107exports.NodeModulesEngineHost = NodeModulesEngineHost;