index.js 3.8 KB

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