[6a3a178] | 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 | */
|
---|
| 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 | * A simple EngineHost that uses NodeModules to resolve collections.
|
---|
| 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 ? path_1.dirname(requester) : process.cwd();
|
---|
| 41 | let collectionPath = undefined;
|
---|
| 42 | if (name.startsWith('.')) {
|
---|
| 43 | name = path_1.resolve(relativeBase, name);
|
---|
| 44 | }
|
---|
| 45 | const resolveOptions = {
|
---|
| 46 | paths: requester ? [path_1.dirname(requester), ...(this.paths || [])] : this.paths,
|
---|
| 47 | };
|
---|
| 48 | // Try to resolve as a package
|
---|
| 49 | try {
|
---|
| 50 | const packageJsonPath = require.resolve(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 | try {
|
---|
| 82 | file_system_utility_1.readJsonFile(collectionPath);
|
---|
| 83 | return collectionPath;
|
---|
| 84 | }
|
---|
| 85 | catch (e) {
|
---|
| 86 | if (e instanceof core_1.InvalidJsonCharacterException ||
|
---|
| 87 | e instanceof core_1.UnexpectedEndOfInputException) {
|
---|
| 88 | throw new file_system_engine_host_base_1.InvalidCollectionJsonException(name, collectionPath, e);
|
---|
| 89 | }
|
---|
| 90 | else {
|
---|
| 91 | throw e;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | _resolveReferenceString(refString, parentPath) {
|
---|
| 96 | const ref = new export_ref_1.ExportStringRef(refString, parentPath);
|
---|
| 97 | if (!ref.ref) {
|
---|
| 98 | return null;
|
---|
| 99 | }
|
---|
| 100 | return { ref: ref.ref, path: ref.module };
|
---|
| 101 | }
|
---|
| 102 | _transformCollectionDescription(name, desc) {
|
---|
| 103 | if (!desc.schematics || typeof desc.schematics != 'object') {
|
---|
| 104 | throw new file_system_engine_host_base_1.CollectionMissingSchematicsMapException(name);
|
---|
| 105 | }
|
---|
| 106 | return {
|
---|
| 107 | ...desc,
|
---|
| 108 | name,
|
---|
| 109 | };
|
---|
| 110 | }
|
---|
| 111 | _transformSchematicDescription(name, _collection, desc) {
|
---|
| 112 | if (!desc.factoryFn || !desc.path || !desc.description) {
|
---|
| 113 | throw new file_system_engine_host_base_1.SchematicMissingFieldsException(name);
|
---|
| 114 | }
|
---|
| 115 | return desc;
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | exports.NodeModulesEngineHost = NodeModulesEngineHost;
|
---|