schematic-options.js 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright Google LLC All Rights Reserved.
  5. *
  6. * Use of this source code is governed by an MIT-style license that can be
  7. * found in the LICENSE file at https://angular.dev/license
  8. */
  9. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  10. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  11. return new (P || (P = Promise))(function (resolve, reject) {
  12. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  13. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  14. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  15. step((generator = generator.apply(thisArg, _arguments || [])).next());
  16. });
  17. };
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. exports.getDefaultComponentOptions = getDefaultComponentOptions;
  20. exports.isStandaloneSchematic = isStandaloneSchematic;
  21. const core_1 = require("@angular-devkit/core");
  22. const schema_1 = require("@schematics/angular/component/schema");
  23. const ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils");
  24. const project_main_file_1 = require("./project-main-file");
  25. const workspace_1 = require("@schematics/angular/utility/workspace");
  26. const get_project_1 = require("./get-project");
  27. /**
  28. * Returns the default options for the `@schematics/angular:component` schematic which would
  29. * have been specified at project initialization (ng new or ng init).
  30. *
  31. * This is necessary because the Angular CLI only exposes the default values for the "--style",
  32. * "--inlineStyle", "--skipTests" and "--inlineTemplate" options to the "component" schematic.
  33. */
  34. function getDefaultComponentOptions(project) {
  35. // Note: Not all options which are available when running "ng new" will be stored in the
  36. // workspace config. List of options which will be available in the configuration:
  37. // angular/angular-cli/blob/main/packages/schematics/angular/application/index.ts#L109-L131
  38. let skipTests = getDefaultComponentOption(project, ['skipTests'], null);
  39. // In case "skipTests" is not set explicitly, also look for the "spec" option. The "spec"
  40. // option has been deprecated but can be still used in older Angular CLI projects.
  41. // See: https://github.com/angular/angular-cli/commit/a12a4e02a4689b5bdbc6e740c0d9865afb55671a
  42. if (skipTests === null) {
  43. skipTests = !getDefaultComponentOption(project, ['spec'], true);
  44. }
  45. return {
  46. style: getDefaultComponentOption(project, ['style', 'styleext'], schema_1.Style.Css),
  47. inlineStyle: getDefaultComponentOption(project, ['inlineStyle'], false),
  48. inlineTemplate: getDefaultComponentOption(project, ['inlineTemplate'], false),
  49. skipTests: skipTests,
  50. };
  51. }
  52. /** Determines whether the schematic is configured to be standalone. */
  53. function isStandaloneSchematic(host, options) {
  54. return __awaiter(this, void 0, void 0, function* () {
  55. var _a;
  56. if (options.standalone != null) {
  57. return options.standalone;
  58. }
  59. // If the `--standalone` flag isn't passed and there isn't a default, infer based on the project.
  60. const workspace = yield (0, workspace_1.getWorkspace)(host);
  61. const project = (0, get_project_1.getProjectFromWorkspace)(workspace, options.project);
  62. // Legacy projects might not have a `build` target, but they're likely
  63. // not on an Angular version that supports standalone either.
  64. if (!((_a = project.targets) === null || _a === void 0 ? void 0 : _a.has('build'))) {
  65. return false;
  66. }
  67. return (0, ng_ast_utils_1.isStandaloneApp)(host, (0, project_main_file_1.getProjectMainFile)(project));
  68. });
  69. }
  70. /**
  71. * Gets the default value for the specified option. The default options will be determined
  72. * by looking at the stored schematic options for `@schematics/angular:component` in the
  73. * CLI workspace configuration.
  74. */
  75. function getDefaultComponentOption(project, optionNames, fallbackValue) {
  76. const schematicOptions = (0, core_1.isJsonObject)(project.extensions['schematics'] || null)
  77. ? project.extensions['schematics']
  78. : null;
  79. const defaultSchematic = schematicOptions
  80. ? schematicOptions['@schematics/angular:component']
  81. : null;
  82. for (const optionName of optionNames) {
  83. if (defaultSchematic && defaultSchematic[optionName] != null) {
  84. return defaultSchematic[optionName];
  85. }
  86. }
  87. return fallbackValue;
  88. }
  89. //# sourceMappingURL=schematic-options.js.map