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 getDefaultAngularAppName(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 projectNames[0];
|
32 | }
|
33 | exports.getDefaultAngularAppName = getDefaultAngularAppName;
|
34 | function getAngularAppConfig(config, projectName) {
|
35 | if (!config.projects.hasOwnProperty(projectName)) {
|
36 | throw new schematics_1.SchematicsException(`Could not find project: ${projectName}`);
|
37 | }
|
38 | const projectConfig = config.projects[projectName];
|
39 | if (isAngularBrowserProject(projectConfig)) {
|
40 | return projectConfig;
|
41 | }
|
42 | if (config.projectType !== 'application') {
|
43 | throw new schematics_1.SchematicsException(`Invalid projectType for ${projectName}: ${config.projectType}`);
|
44 | }
|
45 | else {
|
46 | const buildConfig = projectConfig.architect.build;
|
47 | throw new schematics_1.SchematicsException(`Invalid builder for ${projectName}: ${buildConfig.builder}`);
|
48 | }
|
49 | }
|
50 | exports.getAngularAppConfig = getAngularAppConfig;
|
51 | function addStyle(host, projectName, stylePath) {
|
52 | const config = readConfig(host);
|
53 | const appConfig = getAngularAppConfig(config, projectName);
|
54 | appConfig.architect.build.options.styles.push({
|
55 | input: stylePath
|
56 | });
|
57 | writeConfig(host, config);
|
58 | }
|
59 | exports.addStyle = addStyle;
|
60 | function addAsset(host, projectName, asset) {
|
61 | const config = readConfig(host);
|
62 | const appConfig = getAngularAppConfig(config, projectName);
|
63 | appConfig.architect.build.options.assets.push(asset);
|
64 | writeConfig(host, config);
|
65 | }
|
66 | exports.addAsset = addAsset;
|
67 | function addArchitectBuilder(host, projectName, builderName, builderOpts) {
|
68 | const config = readConfig(host);
|
69 | const appConfig = getAngularAppConfig(config, projectName);
|
70 | appConfig.architect[builderName] = builderOpts;
|
71 | writeConfig(host, config);
|
72 | }
|
73 | exports.addArchitectBuilder = addArchitectBuilder;
|
74 | function getWorkspacePath(host) {
|
75 | const possibleFiles = ['/angular.json', '/.angular.json'];
|
76 | const path = possibleFiles.filter(path => host.exists(path))[0];
|
77 | return path;
|
78 | }
|
79 | exports.getWorkspacePath = getWorkspacePath;
|
80 | function getWorkspace(host) {
|
81 | const path = getWorkspacePath(host);
|
82 | const configBuffer = host.read(path);
|
83 | if (configBuffer === null) {
|
84 | throw new schematics_1.SchematicsException(`Could not find (${path})`);
|
85 | }
|
86 | const content = configBuffer.toString();
|
87 | return core_1.parseJson(content, core_1.JsonParseMode.Loose);
|
88 | }
|
89 | exports.getWorkspace = getWorkspace;
|