-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
80 lines (71 loc) · 2.52 KB
/
index.js
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
'use strict';
const util = require('util');
const exec = util.promisify(require('child_process').exec);
class ServerlessPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.provider = this.serverless.getProvider(this.serverless.service.provider.name);
this.stage = this.options.stage || this.serverless.service.provider.stage;
this.versionTrackerStages = this.serverless.service.custom.versionTrackerStages || ['production'];
this.hooks = {
'before:package:initialize': this.checkClean.bind(this),
'after:deploy:deploy': this.tagVersion.bind(this),
};
}
async checkClean() {
if (!this.shouldRun()) {
return;
}
const { stdout, stderr } = await exec('git status --porcelain');
if (stderr) {
throw new Error(`stderr: ${stderr}`);
}
if (stdout) {
throw new Error('Working directory is not clean. Commit all changes to Git and try again.');
}
}
async tagVersion() {
if (!this.shouldRun()) {
return;
}
const arns = await this.getArns();
if (arns.length === 0) {
throw new Error('Serverless Version Tracker error: cannot retrieve deployed ARN');
}
arns.forEach(async (arn) => {
const regex = /([-\w]*):(\d*)$/;
const name = arn.match(regex)[1];
const version = arn.match(regex)[2];
const tag = `${name}-${version}`;
this.serverless.cli.log(`Creating local git tag '${tag}'...`);
const { stdout, stderr } = await exec(`git tag ${tag}`);
if (stdout || stderr) {
throw new Error(`stdout: ${stdout}; stderr: ${stderr}`);
}
});
}
async getArns() {
const resp = await this.provider.request('CloudFormation', 'describeStacks', { StackName: this.provider.naming.getStackName(this.stage) });
const output = resp.Stacks[0].Outputs;
let arns = [];
let filteredOutputs;
try {
filteredOutputs = output.filter(entry => entry.OutputKey.match('LambdaFunctionQualifiedArn'));
} catch (error) {
return arns;
}
filteredOutputs.forEach((entry) => { arns.push(entry.OutputValue) });
return arns
}
shouldRun() {
// Default behavior if user has not configured stages.
if (!this.versionTrackerStages
|| !Array.isArray(this.versionTrackerStages)) {
throw new Error('Invalid configuration value for option versionTrackerStages');
}
// Check against configured stages.
return this.versionTrackerStages.indexOf(this.stage) !== -1;
}
}
module.exports = ServerlessPlugin;