-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
389 lines (383 loc) · 15.8 KB
/
index.ts
File metadata and controls
389 lines (383 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
export { default as getInputs } from './get-inputs';
export * from './types';
export * from './contants';
export { ParseSpecForContent } from './utils';
import * as utils from '@serverless-devs/utils';
import fs from 'fs-extra';
import path from 'path';
import dotenv from 'dotenv';
import { expand } from 'dotenv-expand';
import { getDefaultYamlPath, isExtendMode } from './utils';
import Order from './order';
import ParseContent from './parse-content';
import { each, filter, find, get, has, includes, isArray, isEmpty, isString, keys, map, set, split } from 'lodash';
import { ISpec, IYaml, IActionType, IActionLevel, IStep, IRecord } from './types';
import { ENVIRONMENT_FILE_NAME, ENVIRONMENT_FILE_PATH, ENVIRONMENT_KEY, REGX } from './contants';
import assert from 'assert';
import { DevsError, ETrackerType } from '@serverless-devs/utils';
const extend2 = require('extend2');
const debug = require('@serverless-cd/debug')('serverless-devs:parse-spec');
interface IOptions {
argv?: string[];
logger?: any;
isPreview?: boolean;
}
class ParseSpec {
private yaml = {} as IYaml;
private record = {} as IRecord;
constructor(filePath: string = '', private options: IOptions = {}) {
this.options.argv = this.options.argv || process.argv.slice(2);
this.options.logger = this.options.logger || console;
// ${config()} ${secret()} won't show real value in preview mode
this.options.isPreview = this.options.isPreview || false;
this.init(filePath);
debug(`yaml path: ${this.yaml.path}`);
debug(`argv: ${JSON.stringify(this.options.argv)}`);
}
private init(filePath: string) {
if (isEmpty(filePath)) {
return (this.yaml.path = getDefaultYamlPath() as string);
}
if (fs.existsSync(filePath)) {
return (this.yaml.path = utils.getAbsolutePath(filePath));
}
throw new Error(`The specified template file does not exist: ${filePath}`);
}
private async doYamlinit() {
await this.doExtend();
this.doEnvironment();
this.yaml.access = get(this.yaml.content, 'access');
const projectKey = this.yaml.use3x ? 'resources' : 'services';
const projects = get(this.yaml.content, projectKey, {});
this.yaml.projectNames = keys(get(this.yaml.content, projectKey, {}));
this.yaml.vars = get(this.yaml.content, 'vars', {});
this.yaml.flow = get(this.yaml.content, 'flow', {});
this.yaml.useFlow = false;
this.yaml.appName = get(this.yaml.content, 'name');
// 兼容2.0: 加入项目的.env环境变量
for (const i of this.yaml.projectNames) {
const code = get(projects, `${i}.props.code`);
if (code && isString(code)) {
const codePath = utils.getAbsolutePath(get(projects, `${i}.props.code`, ''));
expand(dotenv.config({ path: path.join(codePath, '.env'), override: true }));
}
}
expand(dotenv.config({ path: path.join(path.dirname(this.yaml.path), '.env'), override: true }));
}
private async doExtend() {
// this.yaml = { path: '' }
this.yaml.content = utils.getYamlContent(this.yaml.path);
this.yaml.extend = get(this.yaml.content, 'extend');
this.yaml.useExtend = isExtendMode(this.yaml.extend, path.dirname(this.yaml.path));
if (this.yaml.useExtend) {
// if useExtend, 则直接解析前后内容
const extendPath = utils.getAbsolutePath(this.yaml.extend, path.dirname(this.yaml.path));
expand(dotenv.config({ path: path.join(extendPath, '.env') }));
const extendContent = utils.getYamlContent(extendPath);
this.yaml.use3x = String(get(this.yaml.content, 'edition', get(extendContent, 'edition'))) === '3.0.0';
// 1.x 不做extend动作
if (!this.yaml.use3x) return;
const { resources: extendResource, ...extendRest } = extendContent;
const { resources: currentResource, ...currentRest } = this.yaml.content;
const tempRest = extend2(true, {}, extendRest, currentRest);
const base = await new ParseContent({ ...extendContent, ...tempRest }, this.getParsedContentOptions(extendPath)).start();
const current = await new ParseContent({ ...this.yaml.content, ...tempRest }, this.getParsedContentOptions(this.yaml.path)).start();
this.yaml.content = extend2(true, {}, get(base, 'content'), get(current, 'content'));
return;
}
this.yaml.use3x = String(get(this.yaml.content, 'edition')) === '3.0.0';
}
/**
* # 指定--env时:
- s.yaml使用extend,则报错
- 如果s.yaml声明了env yaml,则使用指定的env.yaml, 否则使用默认的env.yaml
- 如果env.yaml不存在,则报错
- 指定的env找不到,则报错
- s deploy --env test
# 不指定--env时
## s.yaml使用extend
- s.yaml声明了env yaml, 则报错,没声明env yaml,则不使用多环境
- s deploy
## s.yaml未使用extend
- s.yaml声明了env yaml,如果env.yaml不存在,则报错
- 使用指定env.yaml的默认环境,如果没有指定默认环境,则报错
- 如果指定的默认环境找不到,则报错。
- s deploy
*/
private doEnvironment() {
if (!this.yaml.use3x) return;
const envInfo = this.record.env ? this.doEnvWithSpecify() : this.doEnvWithNotSpecify();
// 不使用多环境, 则直接返回
if (isEmpty(envInfo)) return;
const { project, environment } = envInfo as { project: string; environment: Record<string, any> };
debug(`use environment: ${JSON.stringify(environment)}`);
set(environment, 'overlays.resources.region', get(environment, 'region'));
set(environment, '__project', project);
this.yaml.environment = environment;
}
// not specify --env
private doEnvWithNotSpecify() {
if (this.yaml.useExtend) {
if (has(this.yaml.content, ENVIRONMENT_KEY)) {
throw new DevsError('Environment and extend is conflict', {
trackerType: ETrackerType.parseException,
});
}
}
// default-env.json is not exist
if (!fs.existsSync(ENVIRONMENT_FILE_PATH)) {
return {};
}
// 若存在环境变量,默认项目为devsProject
const devsProject = process.env.ALIYUN_DEVS_REMOTE_PROJECT_NAME;
const project = devsProject ? devsProject : get(this.yaml.content, 'name');
const defaultEnvContent = require(ENVIRONMENT_FILE_PATH);
const defaultEnv = get(find(defaultEnvContent, { project: project }), 'default');
// project is not found in default-env.json
if (isEmpty(defaultEnv)) {
return {};
}
const envPath: string = utils.getAbsolutePath(get(this.yaml.content, ENVIRONMENT_KEY) || 'env.yaml', path.dirname(this.yaml.path));
const envYamlContent = utils.getYamlContent(envPath);
if (isEmpty(envYamlContent)) {
this.options.logger.warnOnce(`Environment file [${envPath}] is not found, run without environment.`);
return {};
}
const { environments } = envYamlContent;
const environment = find(environments, item => item.name === defaultEnv);
// default env is not found in env.yaml
if (isEmpty(environment)) {
this.options.logger.warnOnce(`Default env [${defaultEnv}] is not found, run without environment.`);
return {};
}
return { project, environment };
}
// specify --env
private doEnvWithSpecify() {
// env and extend is conflict
if (this.yaml.useExtend) {
// TODO: @封崇
throw new DevsError('Environment and extend is conflict', {
trackerType: ETrackerType.parseException,
});
}
const envPath: string = utils.getAbsolutePath(get(this.yaml.content, ENVIRONMENT_KEY, ENVIRONMENT_FILE_NAME), path.dirname(this.yaml.path));
const envYamlContent = utils.getYamlContent(envPath);
// env file is not exist
if (isEmpty(envYamlContent)) {
this.options.logger.warnOnce(`Environment file [${envPath}] is not found, run without environment.`);
return {};
}
debug(`environment content: ${JSON.stringify(envYamlContent)}`);
const { environments } = envYamlContent;
// 若存在环境变量,默认项目为devsProject
const devsProject = process.env.ALIYUN_DEVS_REMOTE_PROJECT_NAME;
const project = devsProject ? devsProject : get(this.yaml.content, 'name');
const environment = find(environments, item => item.name === this.record.env);
// env name is not found
if (isEmpty(environment)) {
this.options.logger.warnOnce(`Env [${this.record.env}] was not found, run without environment.`);
return {};
}
return { project, environment };
}
private getParsedContentOptions(basePath: string) {
return {
logger: this.options.logger,
basePath,
projectName: this.record.projectName,
access: this.record.access,
environment: this.yaml.environment,
isPreview: this.options.isPreview,
};
}
async start(): Promise<ISpec> {
debug('parse start');
// 第一次尝试解析参数,比如全局access给 extend 用
// 将命令行参数更新给 this.record
this.parseArgv();
// 处理继承、多环境后, 将 s.yaml 文件信息传入 this.yaml
await this.doYamlinit();
// 再次解析参数,比如projectNames
this.parseArgv();
if (!this.yaml.use3x) return this.v1();
const { steps, content, originSteps, allSteps } = await new ParseContent(this.yaml.content, this.getParsedContentOptions(this.yaml.path)).start();
const services = get(this.yaml.content, 'services', {});
if (isEmpty(steps) && !isEmpty(services)) {
this.options.logger.tips('Check https://docs.serverless-devs.com/user-guide/spec/ for more details. Use the \'s cli fc3 s2tos3\' command for automatic YAML transformation.');
throw new DevsError(`Keyword 'services' has been replaced by 'resources' in 3.0.0 YAML.`, {
trackerType: ETrackerType.parseException,
});
}
// steps 存放每个FC组件/函数的 yaml 配置 ([content.resource] => steps)
// content 为 yaml 已解析的整体完整信息
// originSteps 为 steps 的未解析版
// 获取到真实值后,重新赋值
this.yaml.content = content;
this.yaml.vars = get(this.yaml.content, 'vars', {});
const actions = get(this.yaml.content, 'actions', {});
this.yaml.actions = this.parseActions(actions);
const result = {
steps: this.record.projectName ? steps : this.doFlow(steps, originSteps),
yaml: this.yaml,
allSteps: allSteps,
...this.record,
};
debug(`parse result: ${JSON.stringify(result)}`);
debug('parse end');
return result;
}
// 简单兼容v1版本,不需要做魔法变量解析
private v1(): ISpec {
const steps = [];
const services = get(this.yaml.content, 'services', {});
for (const project in services) {
const element = services[project];
steps.push({
...element,
projectName: project,
});
}
return {
steps: this.record.projectName ? filter(steps, item => item.projectName === this.record.projectName) : steps,
yaml: this.yaml,
...this.record,
};
}
private parseArgv() {
const argv = utils.parseArgv(this.options.argv as string[]);
debug(`parse argv: ${JSON.stringify(argv)}`);
const { _ } = argv;
this.record.access = get(argv, 'access');
this.record.version = get(argv, 'version');
this.record.help = get(argv, 'help');
this.record.output = get(argv, 'output');
this.record.skipActions = get(argv, 'skip-actions');
this.record.debug = get(argv, 'debug');
this.record.env = get(argv, 'env');
this.record.baselineTemplate = get(argv, 'baseline-template');
if (includes(this.yaml.projectNames, _[0])) {
this.record.projectName = _[0];
this.record.command = _[1];
return;
}
this.record.command = _[0];
}
private doFlow(steps: IStep[], originSteps: IStep[]) {
const newSteps: IStep[] = [];
const flowObj = find(this.yaml.flow, (item, key) => this.matchFlow(key));
const orderInstance = new Order(originSteps).start();
const { steps: orderSteps, dependencies, useOrder } = orderInstance.sort(steps);
if (!flowObj) return orderSteps;
debug(`find flow: ${JSON.stringify(flowObj)}`);
const projectOrder = {} as Record<string, number>;
const fn = (projects: string[] = [], index: number) => {
assert(isArray(projects), `flow ${this.record.command} data format is invalid.`);
for (const project of projects) {
const step = find(steps, item => item.projectName === project);
assert(step, `Resource ${project} is not found. Please check the content of flow.`);
newSteps.push({ ...step, flowId: index });
projectOrder[step.projectName] = index;
}
};
each(flowObj, fn);
debug(`flow steps: ${JSON.stringify(newSteps)}`);
this.yaml.useFlow = true;
debug(`flow projectOrder: ${JSON.stringify(projectOrder)}`);
// 指定flow后,如果存在依赖关系,校验是否可以正常执行
if (useOrder) {
debug(`project dependencies: ${JSON.stringify(dependencies)}`);
for (const p1 in dependencies) {
const ele = dependencies[p1];
for (const p2 in ele) {
if (projectOrder[p2] >= projectOrder[p1]) {
throw new Error(`flow is invalid, ${p2} must be executed before ${p1}`);
}
}
}
}
return newSteps;
}
private matchFlow(flow: string) {
const useMagic = REGX.test(flow);
if (useMagic) {
const compile = require('@serverless-devs/art-template/lib/devs-compile');
return compile(flow, { command: this.record.command });
}
return flow === this.record.command;
}
parseActions(actions: Record<string, any> = {}, level: string = IActionLevel.GLOBAL) {
const actionList = [];
for (const action in actions) {
const element = actions[action];
if (!isArray(element)) {
throw new DevsError(`${level} action ${action} is invalid, it must be array`, {
trackerType: ETrackerType.parseException,
});
}
const actionInfo = this.matchAction(action);
debug(`action: ${action}, useAction: ${JSON.stringify(actionInfo)}`);
if (actionInfo.validate) {
actionList.push(
...map(element, item => {
if (item[IActionType.RUN]) {
const { run, ...rest } = item;
return {
...rest,
value: run,
path: utils.getAbsolutePath(get(item, 'path', './'), path.dirname(this.yaml.path)),
actionType: IActionType.RUN,
hookType: actionInfo.type,
level,
projectName: this.record.projectName,
};
}
if (item[IActionType.PLUGIN]) {
const { plugin, ...rest } = item;
const value = utils.getAbsolutePath(plugin, path.dirname(this.yaml.path));
return {
...rest,
value: fs.existsSync(value) ? value : plugin,
actionType: IActionType.PLUGIN,
hookType: actionInfo.type,
level,
projectName: this.record.projectName,
};
}
if (item[IActionType.COMPONENT]) {
const { component, ...rest } = item;
return {
...rest,
value: component,
actionType: IActionType.COMPONENT,
hookType: actionInfo.type,
level,
projectName: this.record.projectName,
};
}
}),
);
}
}
debug(`parse actions: ${JSON.stringify(actionList)}`);
return actionList;
}
private matchAction(action: string) {
const useMagic = REGX.test(action);
if (useMagic) {
const compile = require('@serverless-devs/art-template/lib/devs-compile');
const newAction = compile(action, { command: this.record.command });
const [type, command] = split(newAction, '-');
return {
validate: command === 'true',
type,
};
}
const [type, command] = split(action, '-');
return {
validate: command === this.record.command,
type,
};
}
}
export default ParseSpec;