-
Notifications
You must be signed in to change notification settings - Fork 12k
/
schematics.ts
63 lines (52 loc) · 1.72 KB
/
schematics.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
/**
* Refer to the angular shematics library to let the dependency validator
* know it is used..
*
* require('@schematics/angular')
*/
import {
Collection,
Engine,
Schematic,
SchematicEngine,
} from '@angular-devkit/schematics';
import {
FileSystemCollectionDesc,
FileSystemSchematicDesc,
NodeModulesEngineHost
} from '@angular-devkit/schematics/tools';
import { SchemaClassFactory } from '@ngtools/json-schema';
import 'rxjs/add/operator/concatMap';
import 'rxjs/add/operator/map';
const SilentError = require('silent-error');
const engineHost = new NodeModulesEngineHost();
const engine: Engine<FileSystemCollectionDesc, FileSystemSchematicDesc>
= new SchematicEngine(engineHost);
export function getEngineHost() {
return engineHost;
}
export function getEngine(): Engine<FileSystemCollectionDesc, FileSystemSchematicDesc> {
return engine;
}
export function getCollection(collectionName: string): Collection<any, any> {
const engineHost = getEngineHost();
const engine = getEngine();
// Add support for schemaJson.
engineHost.registerOptionsTransform((schematic: FileSystemSchematicDesc, options: any) => {
if (schematic.schema) {
const SchemaMetaClass = SchemaClassFactory<any>(schematic.schemaJson!);
const schemaClass = new SchemaMetaClass(options);
return schemaClass.$$root();
}
return options;
});
const collection = engine.createCollection(collectionName);
if (collection === null) {
throw new SilentError(`Invalid collection (${collectionName}).`);
}
return collection;
}
export function getSchematic(collection: Collection<any, any>,
schematicName: string): Schematic<any, any> {
return collection.createSchematic(schematicName);
}