UNPKG

3.47 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const schematics_1 = require("@angular-devkit/schematics");
4const core_1 = require("@angular-devkit/core");
5const CONFIG_PATH = 'angular.json';
6function readConfig(host) {
7 const sourceText = host.read(CONFIG_PATH).toString('utf-8');
8 return JSON.parse(sourceText);
9}
10exports.readConfig = readConfig;
11function writeConfig(host, config) {
12 host.overwrite(CONFIG_PATH, JSON.stringify(config, null, 2));
13}
14exports.writeConfig = writeConfig;
15function 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}
22function 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}
33exports.getDefaultAngularAppName = getDefaultAngularAppName;
34function 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}
50exports.getAngularAppConfig = getAngularAppConfig;
51function 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}
59exports.addStyle = addStyle;
60function 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}
66exports.addAsset = addAsset;
67function 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}
73exports.addArchitectBuilder = addArchitectBuilder;
74function getWorkspacePath(host) {
75 const possibleFiles = ['/angular.json', '/.angular.json'];
76 const path = possibleFiles.filter(path => host.exists(path))[0];
77 return path;
78}
79exports.getWorkspacePath = getWorkspacePath;
80function 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}
89exports.getWorkspace = getWorkspace;