index.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. Object.defineProperty(exports, "__esModule", { value: true });
  10. exports.default = default_1;
  11. const schematics_1 = require("@angular-devkit/schematics");
  12. const utility_1 = require("@schematics/angular/utility");
  13. const node_path_1 = require("node:path");
  14. const paths_1 = require("../utility/paths");
  15. const schema_1 = require("./schema");
  16. function default_1(options) {
  17. switch (options.type) {
  18. case schema_1.Type.Karma:
  19. return addKarmaConfig(options);
  20. case schema_1.Type.Browserslist:
  21. return addBrowserslistConfig(options);
  22. default:
  23. throw new schematics_1.SchematicsException(`"${options.type}" is an unknown configuration file type.`);
  24. }
  25. }
  26. function addBrowserslistConfig(options) {
  27. return async (host) => {
  28. const workspace = await (0, utility_1.readWorkspace)(host);
  29. const project = workspace.projects.get(options.project);
  30. if (!project) {
  31. throw new schematics_1.SchematicsException(`Project name "${options.project}" doesn't not exist.`);
  32. }
  33. return (0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)('./files'), [
  34. (0, schematics_1.filter)((p) => p.endsWith('.browserslistrc.template')),
  35. (0, schematics_1.applyTemplates)({}),
  36. (0, schematics_1.move)(project.root),
  37. ]));
  38. };
  39. }
  40. function addKarmaConfig(options) {
  41. return (0, utility_1.updateWorkspace)((workspace) => {
  42. const project = workspace.projects.get(options.project);
  43. if (!project) {
  44. throw new schematics_1.SchematicsException(`Project name "${options.project}" doesn't not exist.`);
  45. }
  46. const testTarget = project.targets.get('test');
  47. if (!testTarget) {
  48. throw new schematics_1.SchematicsException(`No "test" target found for project "${options.project}".` +
  49. ' A "test" target is required to generate a karma configuration.');
  50. }
  51. if (testTarget.builder !== utility_1.AngularBuilder.Karma &&
  52. testTarget.builder !== utility_1.AngularBuilder.BuildKarma) {
  53. throw new schematics_1.SchematicsException(`Cannot add a karma configuration as builder for "test" target in project does not` +
  54. ` use "${utility_1.AngularBuilder.Karma}" or "${utility_1.AngularBuilder.BuildKarma}".`);
  55. }
  56. testTarget.options ??= {};
  57. testTarget.options.karmaConfig = node_path_1.posix.join(project.root, 'karma.conf.js');
  58. // If scoped project (i.e. "@foo/bar"), convert dir to "foo/bar".
  59. let folderName = options.project.startsWith('@') ? options.project.slice(1) : options.project;
  60. if (/[A-Z]/.test(folderName)) {
  61. folderName = schematics_1.strings.dasherize(folderName);
  62. }
  63. return (0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)('./files'), [
  64. (0, schematics_1.filter)((p) => p.endsWith('karma.conf.js.template')),
  65. (0, schematics_1.applyTemplates)({
  66. relativePathToWorkspaceRoot: (0, paths_1.relativePathToWorkspaceRoot)(project.root),
  67. folderName,
  68. needDevkitPlugin: testTarget.builder === utility_1.AngularBuilder.Karma,
  69. }),
  70. (0, schematics_1.move)(project.root),
  71. ]));
  72. });
  73. }