config.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. Object.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. */
  10. const core_1 = require("@angular-devkit/core");
  11. const schematics_1 = require("@angular-devkit/schematics");
  12. function getWorkspacePath(host) {
  13. const possibleFiles = ['/angular.json', '/.angular.json'];
  14. const path = possibleFiles.filter(path => host.exists(path))[0];
  15. return path;
  16. }
  17. exports.getWorkspacePath = getWorkspacePath;
  18. function 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. }
  27. exports.getWorkspace = getWorkspace;
  28. function 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. }
  42. exports.addProjectToWorkspace = addProjectToWorkspace;
  43. function updateWorkspace(workspace) {
  44. return (host, context) => {
  45. host.overwrite(getWorkspacePath(host), JSON.stringify(workspace, null, 2));
  46. };
  47. }
  48. exports.updateWorkspace = updateWorkspace;
  49. exports.configPath = '/.angular-cli.json';
  50. function 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. }
  58. exports.getConfig = getConfig;
  59. function 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. }
  68. exports.getAppFromConfig = getAppFromConfig;