index.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 lint_fix_1 = require("../utility/lint-fix");
  13. const parse_name_1 = require("../utility/parse-name");
  14. const workspace_1 = require("../utility/workspace");
  15. const schema_1 = require("./schema");
  16. function default_1(options) {
  17. return async (host) => {
  18. if (options.path === undefined) {
  19. options.path = await workspace_1.createDefaultPath(host, options.project);
  20. }
  21. if (!options.implements) {
  22. throw new schematics_1.SchematicsException('Option "implements" is required.');
  23. }
  24. const implementations = options.implements
  25. .map(implement => implement === 'CanDeactivate' ? 'CanDeactivate<unknown>' : implement)
  26. .join(', ');
  27. let implementationImports = `${options.implements.join(', ')}, `;
  28. // As long as we aren't in IE... ;)
  29. if (options.implements.includes(schema_1.Implement.CanLoad)) {
  30. implementationImports = `${implementationImports}Route, UrlSegment, `;
  31. }
  32. const parsedPath = parse_name_1.parseName(options.path, options.name);
  33. options.name = parsedPath.name;
  34. options.path = parsedPath.path;
  35. // todo remove these when we remove the deprecations
  36. options.skipTests = options.skipTests || !options.spec;
  37. const templateSource = schematics_1.apply(schematics_1.url('./files'), [
  38. options.skipTests ? schematics_1.filter(path => !path.endsWith('.spec.ts.template')) : schematics_1.noop(),
  39. schematics_1.applyTemplates({
  40. implementations,
  41. implementationImports,
  42. ...core_1.strings,
  43. ...options,
  44. }),
  45. schematics_1.move(parsedPath.path + (options.flat ? '' : '/' + core_1.strings.dasherize(options.name))),
  46. ]);
  47. return schematics_1.chain([
  48. schematics_1.mergeWith(templateSource),
  49. options.lintFix ? lint_fix_1.applyLintFix(options.path) : schematics_1.noop(),
  50. ]);
  51. };
  52. }
  53. exports.default = default_1;