UNPKG

2.71 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getAppFromConfig = exports.getConfig = exports.configPath = exports.updateWorkspace = exports.addProjectToWorkspace = exports.getWorkspace = exports.getWorkspacePath = void 0;
4/**
5 * @license
6 * Copyright Google Inc. All Rights Reserved.
7 *
8 * Use of this source code is governed by an MIT-style license that can be
9 * found in the LICENSE file at https://angular.io/license
10 */
11const core_1 = require("@angular-devkit/core");
12const schematics_1 = require("@angular-devkit/schematics");
13function getWorkspacePath(host) {
14 const possibleFiles = ['/angular.json', '/.angular.json'];
15 const path = possibleFiles.filter(path => host.exists(path))[0];
16 return path;
17}
18exports.getWorkspacePath = getWorkspacePath;
19function getWorkspace(host) {
20 const path = getWorkspacePath(host);
21 const configBuffer = host.read(path);
22 if (configBuffer === null) {
23 throw new schematics_1.SchematicsException(`Could not find (${path})`);
24 }
25 const content = configBuffer.toString();
26 return core_1.parseJson(content, core_1.JsonParseMode.Loose);
27}
28exports.getWorkspace = getWorkspace;
29function addProjectToWorkspace(workspace, name, project) {
30 return (host, context) => {
31 if (workspace.projects[name]) {
32 throw new Error(`Project '${name}' already exists in workspace.`);
33 }
34 // Add project to workspace.
35 workspace.projects[name] = project;
36 if (!workspace.defaultProject && Object.keys(workspace.projects).length === 1) {
37 // Make the new project the default one.
38 workspace.defaultProject = name;
39 }
40 return updateWorkspace(workspace);
41 };
42}
43exports.addProjectToWorkspace = addProjectToWorkspace;
44function updateWorkspace(workspace) {
45 return (host, context) => {
46 host.overwrite(getWorkspacePath(host), JSON.stringify(workspace, null, 2));
47 };
48}
49exports.updateWorkspace = updateWorkspace;
50exports.configPath = '/.angular-cli.json';
51function getConfig(host) {
52 const configBuffer = host.read(exports.configPath);
53 if (configBuffer === null) {
54 throw new schematics_1.SchematicsException('Could not find .angular-cli.json');
55 }
56 const config = core_1.parseJson(configBuffer.toString(), core_1.JsonParseMode.Loose);
57 return config;
58}
59exports.getConfig = getConfig;
60function getAppFromConfig(config, appIndexOrName) {
61 if (!config.apps) {
62 return null;
63 }
64 if (parseInt(appIndexOrName) >= 0) {
65 return config.apps[parseInt(appIndexOrName)];
66 }
67 return config.apps.filter((app) => app.name === appIndexOrName)[0];
68}
69exports.getAppFromConfig = getAppFromConfig;