index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 ENVIRONMENTS_DIRECTORY = 'environments';
  15. const ENVIRONMENT_FILE_CONTENT = 'export const environment = {};\n';
  16. function default_1(options) {
  17. return (0, utility_1.updateWorkspace)((workspace) => {
  18. const project = workspace.projects.get(options.project);
  19. if (!project) {
  20. throw new schematics_1.SchematicsException(`Project name "${options.project}" doesn't not exist.`);
  21. }
  22. const type = project.extensions['projectType'];
  23. if (type !== 'application') {
  24. return log('error', 'Only application project types are support by this schematic.' + type
  25. ? ` Project "${options.project}" has a "projectType" of "${type}".`
  26. : ` Project "${options.project}" has no "projectType" defined.`);
  27. }
  28. const buildTarget = project.targets.get('build');
  29. if (!buildTarget) {
  30. return log('error', `No "build" target found for project "${options.project}".` +
  31. ' A "build" target is required to generate environment files.');
  32. }
  33. const serverTarget = project.targets.get('server');
  34. const sourceRoot = project.sourceRoot ?? node_path_1.posix.join(project.root, 'src');
  35. // The generator needs to be iterated prior to returning to ensure all workspace changes that occur
  36. // within the generator are present for `updateWorkspace` when it writes the workspace file.
  37. return (0, schematics_1.chain)([
  38. ...generateConfigurationEnvironments(buildTarget, serverTarget, sourceRoot, options.project),
  39. ]);
  40. });
  41. }
  42. function createIfMissing(path) {
  43. return (tree, context) => {
  44. if (tree.exists(path)) {
  45. context.logger.info(`Skipping creation of already existing environment file "${path}".`);
  46. }
  47. else {
  48. tree.create(path, ENVIRONMENT_FILE_CONTENT);
  49. }
  50. };
  51. }
  52. function log(type, text) {
  53. return (_, context) => context.logger[type](text);
  54. }
  55. function* generateConfigurationEnvironments(buildTarget, serverTarget, sourceRoot, projectName) {
  56. if (buildTarget.builder !== utility_1.AngularBuilder.Browser &&
  57. buildTarget.builder !== utility_1.AngularBuilder.BrowserEsbuild &&
  58. buildTarget.builder !== utility_1.AngularBuilder.Application &&
  59. buildTarget.builder !== utility_1.AngularBuilder.BuildApplication) {
  60. yield log('warn', `"build" target found for project "${projectName}" has a third-party builder "${buildTarget.builder}".` +
  61. ' The generated project options may not be compatible with this builder.');
  62. }
  63. if (serverTarget && serverTarget.builder !== utility_1.AngularBuilder.Server) {
  64. yield log('warn', `"server" target found for project "${projectName}" has a third-party builder "${buildTarget.builder}".` +
  65. ' The generated project options may not be compatible with this builder.');
  66. }
  67. // Create default environment file
  68. const defaultFilePath = node_path_1.posix.join(sourceRoot, ENVIRONMENTS_DIRECTORY, 'environment.ts');
  69. yield createIfMissing(defaultFilePath);
  70. const configurationEntries = [
  71. ...Object.entries(buildTarget.configurations ?? {}),
  72. ...Object.entries(serverTarget?.configurations ?? {}),
  73. ];
  74. const addedFiles = new Set();
  75. for (const [name, configurationOptions] of configurationEntries) {
  76. if (!configurationOptions) {
  77. // Invalid configuration
  78. continue;
  79. }
  80. // Default configuration will use the default environment file
  81. if (name === buildTarget.defaultConfiguration) {
  82. continue;
  83. }
  84. const configurationFilePath = node_path_1.posix.join(sourceRoot, ENVIRONMENTS_DIRECTORY, `environment.${name}.ts`);
  85. // Add file replacement option entry for the configuration environment file
  86. const replacements = (configurationOptions['fileReplacements'] ??= []);
  87. const existing = replacements.find((value) => value.replace === defaultFilePath);
  88. if (existing) {
  89. if (existing.with === configurationFilePath) {
  90. yield log('info', `Skipping addition of already existing file replacements option for "${defaultFilePath}" to "${configurationFilePath}".`);
  91. }
  92. else {
  93. yield log('warn', `Configuration "${name}" has a file replacements option for "${defaultFilePath}" but with a different replacement.` +
  94. ` Expected "${configurationFilePath}" but found "${existing.with}". This may result in unexpected build behavior.`);
  95. }
  96. }
  97. else {
  98. replacements.push({ replace: defaultFilePath, with: configurationFilePath });
  99. }
  100. // Create configuration specific environment file if not already added
  101. if (!addedFiles.has(configurationFilePath)) {
  102. addedFiles.add(configurationFilePath);
  103. yield createIfMissing(configurationFilePath);
  104. }
  105. }
  106. }