-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathnode-module-engine-host.ts
152 lines (130 loc) · 4.22 KB
/
node-module-engine-host.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {
BaseException,
InvalidJsonCharacterException,
UnexpectedEndOfInputException,
} from '@angular-devkit/core';
import { dirname, join, resolve } from 'path';
import { RuleFactory } from '../src';
import { FileSystemCollectionDesc, FileSystemSchematicDesc } from './description';
import { ExportStringRef } from './export-ref';
import {
CollectionCannotBeResolvedException,
CollectionMissingSchematicsMapException,
FileSystemEngineHostBase,
InvalidCollectionJsonException,
SchematicMissingFieldsException,
} from './file-system-engine-host-base';
import { readJsonFile } from './file-system-utility';
export class NodePackageDoesNotSupportSchematics extends BaseException {
constructor(name: string) {
super(`Package ${JSON.stringify(name)} was found but does not support schematics.`);
}
}
/**
* A simple EngineHost that uses NodeModules to resolve collections.
*/
export class NodeModulesEngineHost extends FileSystemEngineHostBase {
constructor(private readonly paths?: string[]) {
super();
}
private resolve(name: string, requester?: string, references = new Set<string>()): string {
if (requester) {
if (references.has(requester)) {
references.add(requester);
throw new Error(
'Circular schematic reference detected: ' + JSON.stringify(Array.from(references)),
);
} else {
references.add(requester);
}
}
const relativeBase = requester ? dirname(requester) : process.cwd();
let collectionPath: string | undefined = undefined;
if (name.startsWith('.')) {
name = resolve(relativeBase, name);
}
const resolveOptions = {
paths: requester ? [dirname(requester), ...(this.paths || [])] : this.paths,
};
// Try to resolve as a package
try {
const packageJsonPath = require.resolve(join(name, 'package.json'), resolveOptions);
const { schematics } = require(packageJsonPath);
if (!schematics || typeof schematics !== 'string') {
throw new NodePackageDoesNotSupportSchematics(name);
}
collectionPath = this.resolve(schematics, packageJsonPath, references);
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
}
// If not a package, try to resolve as a file
if (!collectionPath) {
try {
collectionPath = require.resolve(name, resolveOptions);
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
}
}
// If not a package or a file, error
if (!collectionPath) {
throw new CollectionCannotBeResolvedException(name);
}
return collectionPath;
}
protected _resolveCollectionPath(name: string, requester?: string): string {
const collectionPath = this.resolve(name, requester);
try {
readJsonFile(collectionPath);
return collectionPath;
} catch (e) {
if (
e instanceof InvalidJsonCharacterException ||
e instanceof UnexpectedEndOfInputException
) {
throw new InvalidCollectionJsonException(name, collectionPath, e);
} else {
throw e;
}
}
}
protected _resolveReferenceString(refString: string, parentPath: string) {
const ref = new ExportStringRef<RuleFactory<{}>>(refString, parentPath);
if (!ref.ref) {
return null;
}
return { ref: ref.ref, path: ref.module };
}
protected _transformCollectionDescription(
name: string,
desc: Partial<FileSystemCollectionDesc>,
): FileSystemCollectionDesc {
if (!desc.schematics || typeof desc.schematics != 'object') {
throw new CollectionMissingSchematicsMapException(name);
}
return {
...desc,
name,
} as FileSystemCollectionDesc;
}
protected _transformSchematicDescription(
name: string,
_collection: FileSystemCollectionDesc,
desc: Partial<FileSystemSchematicDesc>,
): FileSystemSchematicDesc {
if (!desc.factoryFn || !desc.path || !desc.description) {
throw new SchematicMissingFieldsException(name);
}
return desc as FileSystemSchematicDesc;
}
}