1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const schematics_1 = require("@angular-devkit/schematics");
|
4 | const core_1 = require("@angular-devkit/core");
|
5 | const CONFIG_PATH = 'angular.json';
|
6 | function readConfig(host) {
|
7 | const sourceText = host.read(CONFIG_PATH).toString('utf-8');
|
8 | return JSON.parse(sourceText);
|
9 | }
|
10 | exports.readConfig = readConfig;
|
11 | function writeConfig(host, config) {
|
12 | host.overwrite(CONFIG_PATH, JSON.stringify(config, null, 2));
|
13 | }
|
14 | exports.writeConfig = writeConfig;
|
15 | function isAngularBrowserProject(projectConfig) {
|
16 | if (projectConfig.projectType === 'application') {
|
17 | const buildConfig = projectConfig.architect.build;
|
18 | return buildConfig.builder === '@angular-devkit/build-angular:browser';
|
19 | }
|
20 | return false;
|
21 | }
|
22 | function getAngularAppName(config) {
|
23 | const projects = config.projects;
|
24 | const projectNames = Object.keys(projects);
|
25 | for (const projectName of projectNames) {
|
26 | const projectConfig = projects[projectName];
|
27 | if (isAngularBrowserProject(projectConfig)) {
|
28 | return projectName;
|
29 | }
|
30 | }
|
31 | return null;
|
32 | }
|
33 | exports.getAngularAppName = getAngularAppName;
|
34 | function getAngularAppConfig(config) {
|
35 | const projects = config.projects;
|
36 | const projectNames = Object.keys(projects);
|
37 | for (const projectName of projectNames) {
|
38 | const projectConfig = projects[projectName];
|
39 | if (isAngularBrowserProject(projectConfig)) {
|
40 | return projectConfig;
|
41 | }
|
42 | }
|
43 | return null;
|
44 | }
|
45 | exports.getAngularAppConfig = getAngularAppConfig;
|
46 | function addStyle(host, stylePath) {
|
47 | const config = readConfig(host);
|
48 | const appConfig = getAngularAppConfig(config);
|
49 | if (appConfig) {
|
50 | appConfig.architect.build.options.styles.push({
|
51 | input: stylePath
|
52 | });
|
53 | writeConfig(host, config);
|
54 | }
|
55 | else {
|
56 | throw new schematics_1.SchematicsException(`Cannot find valid app`);
|
57 | }
|
58 | }
|
59 | exports.addStyle = addStyle;
|
60 | function addAsset(host, asset) {
|
61 | const config = readConfig(host);
|
62 | const appConfig = getAngularAppConfig(config);
|
63 | if (appConfig) {
|
64 | appConfig.architect.build.options.assets.push(asset);
|
65 | writeConfig(host, config);
|
66 | }
|
67 | else {
|
68 | throw new schematics_1.SchematicsException(`Cannot find valid app`);
|
69 | }
|
70 | }
|
71 | exports.addAsset = addAsset;
|
72 | function addArchitectBuilder(host, builderName, builderOpts) {
|
73 | const config = readConfig(host);
|
74 | const appConfig = getAngularAppConfig(config);
|
75 | if (appConfig) {
|
76 | appConfig.architect[builderName] = builderOpts;
|
77 | writeConfig(host, config);
|
78 | }
|
79 | else {
|
80 | throw new schematics_1.SchematicsException(`Cannot find valid app`);
|
81 | }
|
82 | }
|
83 | exports.addArchitectBuilder = addArchitectBuilder;
|
84 | function getWorkspacePath(host) {
|
85 | const possibleFiles = ['/angular.json', '/.angular.json'];
|
86 | const path = possibleFiles.filter(path => host.exists(path))[0];
|
87 | return path;
|
88 | }
|
89 | exports.getWorkspacePath = getWorkspacePath;
|
90 | function getWorkspace(host) {
|
91 | const path = getWorkspacePath(host);
|
92 | const configBuffer = host.read(path);
|
93 | if (configBuffer === null) {
|
94 | throw new schematics_1.SchematicsException(`Could not find (${path})`);
|
95 | }
|
96 | const content = configBuffer.toString();
|
97 | return core_1.parseJson(content, core_1.JsonParseMode.Loose);
|
98 | }
|
99 | exports.getWorkspace = getWorkspace;
|