index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. const ts = require("../third_party/github.com/Microsoft/TypeScript/lib/typescript");
  13. const ast_utils_1 = require("../utility/ast-utils");
  14. const change_1 = require("../utility/change");
  15. const find_module_1 = require("../utility/find-module");
  16. const lint_fix_1 = require("../utility/lint-fix");
  17. const parse_name_1 = require("../utility/parse-name");
  18. const validation_1 = require("../utility/validation");
  19. const workspace_1 = require("../utility/workspace");
  20. function readIntoSourceFile(host, modulePath) {
  21. const text = host.read(modulePath);
  22. if (text === null) {
  23. throw new schematics_1.SchematicsException(`File ${modulePath} does not exist.`);
  24. }
  25. const sourceText = text.toString('utf-8');
  26. return ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
  27. }
  28. function addDeclarationToNgModule(options) {
  29. return (host) => {
  30. if (options.skipImport || !options.module) {
  31. return host;
  32. }
  33. options.type = !!options.type ? options.type : 'Component';
  34. const modulePath = options.module;
  35. const source = readIntoSourceFile(host, modulePath);
  36. const componentPath = `/${options.path}/`
  37. + (options.flat ? '' : core_1.strings.dasherize(options.name) + '/')
  38. + core_1.strings.dasherize(options.name)
  39. + '.'
  40. + core_1.strings.dasherize(options.type);
  41. const relativePath = find_module_1.buildRelativePath(modulePath, componentPath);
  42. const classifiedName = core_1.strings.classify(options.name) + core_1.strings.classify(options.type);
  43. const declarationChanges = ast_utils_1.addDeclarationToModule(source, modulePath, classifiedName, relativePath);
  44. const declarationRecorder = host.beginUpdate(modulePath);
  45. for (const change of declarationChanges) {
  46. if (change instanceof change_1.InsertChange) {
  47. declarationRecorder.insertLeft(change.pos, change.toAdd);
  48. }
  49. }
  50. host.commitUpdate(declarationRecorder);
  51. if (options.export) {
  52. // Need to refresh the AST because we overwrote the file in the host.
  53. const source = readIntoSourceFile(host, modulePath);
  54. const exportRecorder = host.beginUpdate(modulePath);
  55. const exportChanges = ast_utils_1.addExportToModule(source, modulePath, core_1.strings.classify(options.name) + core_1.strings.classify(options.type), relativePath);
  56. for (const change of exportChanges) {
  57. if (change instanceof change_1.InsertChange) {
  58. exportRecorder.insertLeft(change.pos, change.toAdd);
  59. }
  60. }
  61. host.commitUpdate(exportRecorder);
  62. }
  63. if (options.entryComponent) {
  64. // Need to refresh the AST because we overwrote the file in the host.
  65. const source = readIntoSourceFile(host, modulePath);
  66. const entryComponentRecorder = host.beginUpdate(modulePath);
  67. const entryComponentChanges = ast_utils_1.addEntryComponentToModule(source, modulePath, core_1.strings.classify(options.name) + core_1.strings.classify(options.type), relativePath);
  68. for (const change of entryComponentChanges) {
  69. if (change instanceof change_1.InsertChange) {
  70. entryComponentRecorder.insertLeft(change.pos, change.toAdd);
  71. }
  72. }
  73. host.commitUpdate(entryComponentRecorder);
  74. }
  75. return host;
  76. };
  77. }
  78. function buildSelector(options, projectPrefix) {
  79. let selector = core_1.strings.dasherize(options.name);
  80. if (options.prefix) {
  81. selector = `${options.prefix}-${selector}`;
  82. }
  83. else if (options.prefix === undefined && projectPrefix) {
  84. selector = `${projectPrefix}-${selector}`;
  85. }
  86. return selector;
  87. }
  88. function default_1(options) {
  89. return async (host) => {
  90. const workspace = await workspace_1.getWorkspace(host);
  91. const project = workspace.projects.get(options.project);
  92. if (options.path === undefined && project) {
  93. options.path = workspace_1.buildDefaultPath(project);
  94. }
  95. options.module = find_module_1.findModuleFromOptions(host, options);
  96. const parsedPath = parse_name_1.parseName(options.path, options.name);
  97. options.name = parsedPath.name;
  98. options.path = parsedPath.path;
  99. options.selector = options.selector || buildSelector(options, project && project.prefix || '');
  100. validation_1.validateName(options.name);
  101. validation_1.validateHtmlSelector(options.selector);
  102. const templateSource = schematics_1.apply(schematics_1.url('./files'), [
  103. options.skipTests ? schematics_1.filter(path => !path.endsWith('.spec.ts.template')) : schematics_1.noop(),
  104. options.inlineStyle ? schematics_1.filter(path => !path.endsWith('.__style__.template')) : schematics_1.noop(),
  105. options.inlineTemplate ? schematics_1.filter(path => !path.endsWith('.html.template')) : schematics_1.noop(),
  106. schematics_1.applyTemplates({
  107. ...core_1.strings,
  108. 'if-flat': (s) => options.flat ? '' : s,
  109. ...options,
  110. }),
  111. schematics_1.move(parsedPath.path),
  112. ]);
  113. return schematics_1.chain([
  114. addDeclarationToNgModule(options),
  115. schematics_1.mergeWith(templateSource),
  116. options.lintFix ? lint_fix_1.applyLintFix(options.path) : schematics_1.noop(),
  117. ]);
  118. };
  119. }
  120. exports.default = default_1;