1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | const schematics_1 = require("@angular-devkit/schematics");
|
11 | const utility_1 = require("@schematics/angular/utility");
|
12 | const path_1 = require("path");
|
13 | const ENVIRONMENTS_DIRECTORY = 'environments';
|
14 | const ENVIRONMENT_FILE_CONTENT = 'export const environment = {};\n';
|
15 | function 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 |
|
35 |
|
36 | return (0, schematics_1.chain)([
|
37 | ...generateConfigurationEnvironments(buildTarget, serverTarget, sourceRoot, options.project),
|
38 | ]);
|
39 | });
|
40 | }
|
41 | exports.default = default_1;
|
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 | 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 |
|
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 |
|
77 | continue;
|
78 | }
|
79 |
|
80 | if (name === buildTarget.defaultConfiguration) {
|
81 | continue;
|
82 | }
|
83 | const configurationFilePath = path_1.posix.join(sourceRoot, ENVIRONMENTS_DIRECTORY, `environment.${name}.ts`);
|
84 |
|
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 |
|
100 | if (!addedFiles.has(configurationFilePath)) {
|
101 | addedFiles.add(configurationFilePath);
|
102 | yield createIfMissing(configurationFilePath);
|
103 | }
|
104 | }
|
105 | }
|