-
Notifications
You must be signed in to change notification settings - Fork 97
/
ng-add.ts
58 lines (50 loc) · 1.58 KB
/
ng-add.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
import { workspaces } from '@angular-devkit/core';
import {
SchematicContext,
SchematicsException,
Tree
} from '@angular-devkit/schematics';
import { createHost } from './utils';
interface NgAddOptions {
project: string;
}
export const ngAdd = (options: NgAddOptions) => async (
tree: Tree,
_context: SchematicContext
) => {
const host = createHost(tree);
const { workspace } = await workspaces.readWorkspace('/', host);
if (!options.project) {
if (workspace.projects.size === 1) {
// If there is only one project, return that one.
options.project = Array.from(workspace.projects.keys())[0];
} else {
throw new SchematicsException(
'There is more than one project in your workspace. Please select it manually by using the --project argument.'
);
}
}
const project = workspace.projects.get(options.project);
if (!project) {
throw new SchematicsException(
'The specified Angular project is not defined in this workspace'
);
}
if (project.extensions.projectType !== 'application') {
throw new SchematicsException(
`Deploy requires an Angular project type of "application" in angular.json`
);
}
if (!project.targets.get('build')?.options?.outputPath) {
throw new SchematicsException(
`Cannot read the output path (architect.build.options.outputPath) of the Angular project "${options.project}" in angular.json`
);
}
project.targets.add({
name: 'deploy',
builder: 'angular-cli-ghpages:deploy',
options: {}
});
workspaces.writeWorkspace(workspace, host);
return tree;
};