UNPKG

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