UNPKG

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