UNPKG

4.5 kBPlain TextView Raw
1/**
2 * This module must not import anything globally not workin in web-mode! if needed, require it within the functions
3 */
4import { IConfigParseResult } from '../libs/config-parse-result';
5import { IPlugin } from '../libs/plugin';
6import { isEnvironment } from './environment-component'
7import * as deepmerge from 'deepmerge';
8import {PARSER_MODES} from "../libs/parser";
9
10/**
11 * Parameters that apply to the whole Plugin, passed by other plugins
12 */
13export interface IEnvironmentPlugin {
14
15 /**
16 * the stage is the environment to apply
17 */
18 stage: string,
19
20 /**
21 * one of the [[PARSER_MODES]]
22 */
23 parserMode: string
24}
25
26/**
27 * A Plugin to detect WebApp-Components
28 * @param props
29 */
30export const EnvironmentPlugin = (props: IEnvironmentPlugin): IPlugin => {
31 const path = require('path');
32
33 const result: IPlugin = {
34 // identify Isomorphic-App-Components
35 applies: (component):boolean => {
36
37 return isEnvironment(component);
38 },
39
40 // convert the component into configuration parts
41 // while the component is of Type `any`, its props must be of type `IWebApp`
42 process: (component:any,
43 childConfigs:Array<IConfigParseResult>,
44 infrastructureMode:string | undefined
45 ):IConfigParseResult => {
46
47 // when we did not provide a stage, we evaluate all stages, but only with regard to the b
48 if (props.stage === undefined) {
49
50 console.log(`No stage specified, load environment ${component.name} in build mode`);
51
52
53 return {
54 slsConfigs: [],
55 webpackConfigs: [],
56 postBuilds: [],
57 environments: [component],
58 iamRoleStatements: [],
59 }
60
61 } else if (props.stage !== component.name) {
62 // we ignore any environment that does not match the specified one!
63 console.log(`environment ${component.name} does not apply to specified ${props.stage}`);
64 return {
65 slsConfigs: [],
66 webpackConfigs: [],
67 postBuilds: [],
68 iamRoleStatements: [],
69 }
70 }
71
72 console.log("apply environment: ", component);
73
74 return {
75
76 slsConfigs: deepmerge.all([
77 {
78 // set the stage-name
79 provider: {
80 stage: component.name,
81
82 environment: component.envValues.reduce((result, entry) => {
83 const newResult = Object.assign({}, result);
84 newResult[entry.name] = entry.value
85 return newResult;
86 }, {})
87 }
88 },
89 component.offlinePort !== undefined && props.parserMode === PARSER_MODES.MODE_START ? {
90 provider: {
91 port: component.offlinePort
92 }
93 } : {},
94 // the stage path is valid only
95 component.domain == undefined && props.parserMode === PARSER_MODES.MODE_DEPLOY ? {
96 provider: {
97 stage_path: component.name,
98
99 }
100 } : {},
101 component.domain !== undefined ? {
102 provider: {
103 stage_path: "''",
104
105 environment: {
106 DOMAIN_URL: `"https://${component.domain}"`
107 }
108 }
109 } : {},
110
111 // adding the domain to the sls-config is to be done by the app for
112 // there is a difference of the kind of app and how it uses the domain
113
114
115 ]),
116
117 webpackConfigs: [],
118
119 postBuilds: [],
120
121 iamRoleStatements: [],
122
123 environments: [component],
124
125 // here, we provide the name of the domain to the upper-level components
126 domain: component.domain,
127
128 // and the certArn
129
130 certArn: component.certArn
131 }
132 }
133 };
134
135
136 return result;
137
138};
\No newline at end of file