1 | import { Rule, SchematicContext, Tree, SchematicsException } from '@angular-devkit/schematics';
|
2 | import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
|
3 |
|
4 | const TSCONFIG_DATA = {
|
5 | include: ['src/**/*.ts'],
|
6 | exclude: ['src/**/*.spec.ts']
|
7 | };
|
8 |
|
9 | function safeReadJSON(path: string, tree: Tree) {
|
10 | try {
|
11 | return JSON.parse(tree.read(path)!.toString());
|
12 | } catch (e) {
|
13 | throw new SchematicsException(`Error when parsing ${path}: ${e.message}`);
|
14 | }
|
15 | }
|
16 |
|
17 |
|
18 | export function ngAdd(): Rule {
|
19 | return (tree: Tree, context: SchematicContext) => {
|
20 |
|
21 | const tsconfigDocFile = 'tsconfig.doc.json';
|
22 | if (!tree.exists(tsconfigDocFile)) {
|
23 | tree.create(tsconfigDocFile, JSON.stringify(TSCONFIG_DATA));
|
24 | }
|
25 |
|
26 | const packageJsonFile = 'package.json';
|
27 | const packageJson = tree.exists(packageJsonFile) && safeReadJSON(packageJsonFile, tree);
|
28 |
|
29 | if (packageJson === undefined) {
|
30 | throw new SchematicsException('Could not locate package.json');
|
31 | }
|
32 |
|
33 | let packageScripts = {};
|
34 | if (packageJson['scripts']) {
|
35 | packageScripts = packageJson['scripts'];
|
36 | } else {
|
37 | packageScripts = {};
|
38 | }
|
39 |
|
40 | if (packageScripts) {
|
41 | packageScripts['compodoc:build'] = 'compodoc -p tsconfig.doc.json';
|
42 | packageScripts['compodoc:build-and-serve'] = 'compodoc -p tsconfig.doc.json -s';
|
43 | packageScripts['compodoc:serve'] = 'compodoc -s';
|
44 | }
|
45 |
|
46 | if (tree.exists(packageJsonFile)) {
|
47 | tree.overwrite(packageJsonFile, JSON.stringify(packageJson, null, 2));
|
48 | } else {
|
49 | tree.create(packageJsonFile, JSON.stringify(packageJson, null, 2));
|
50 | }
|
51 |
|
52 |
|
53 | context.addTask(new NodePackageInstallTask());
|
54 | return tree;
|
55 | };
|
56 | }
|