workspace.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. function createHost(tree) {
  12. return {
  13. async readFile(path) {
  14. const data = tree.read(path);
  15. if (!data) {
  16. throw new Error('File not found.');
  17. }
  18. return core_1.virtualFs.fileBufferToString(data);
  19. },
  20. async writeFile(path, data) {
  21. return tree.overwrite(path, data);
  22. },
  23. async isDirectory(path) {
  24. // approximate a directory check
  25. return !tree.exists(path) && tree.getDir(path).subfiles.length > 0;
  26. },
  27. async isFile(path) {
  28. return tree.exists(path);
  29. },
  30. };
  31. }
  32. function updateWorkspace(updaterOrWorkspace) {
  33. return async (tree) => {
  34. const host = createHost(tree);
  35. if (typeof updaterOrWorkspace === 'function') {
  36. const { workspace } = await core_1.workspaces.readWorkspace('/', host);
  37. const result = updaterOrWorkspace(workspace);
  38. if (result !== undefined) {
  39. await result;
  40. }
  41. await core_1.workspaces.writeWorkspace(workspace, host);
  42. }
  43. else {
  44. await core_1.workspaces.writeWorkspace(updaterOrWorkspace, host);
  45. }
  46. };
  47. }
  48. exports.updateWorkspace = updateWorkspace;
  49. async function getWorkspace(tree, path = '/') {
  50. const host = createHost(tree);
  51. const { workspace } = await core_1.workspaces.readWorkspace(path, host);
  52. return workspace;
  53. }
  54. exports.getWorkspace = getWorkspace;
  55. /**
  56. * Build a default project path for generating.
  57. * @param project The project which will have its default path generated.
  58. */
  59. function buildDefaultPath(project) {
  60. const root = project.sourceRoot ? `/${project.sourceRoot}/` : `/${project.root}/src/`;
  61. const projectDirName = project.extensions['projectType'] === 'application' ? 'app' : 'lib';
  62. return `${root}${projectDirName}`;
  63. }
  64. exports.buildDefaultPath = buildDefaultPath;
  65. async function createDefaultPath(tree, projectName) {
  66. const workspace = await getWorkspace(tree);
  67. const project = workspace.projects.get(projectName);
  68. if (!project) {
  69. throw new Error('Specified project does not exist.');
  70. }
  71. return buildDefaultPath(project);
  72. }
  73. exports.createDefaultPath = createDefaultPath;