-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
137 lines (104 loc) · 3.93 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
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
'use strict';
const exec = require('child_process').exec;
const fs = require('fs');
class ServerlessPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.stage = this.options.stage || this.serverless.service.provider.stage;
this.deploymentTracker = this.serverless.service.custom.gitCommitTracker;
this.commands = {
gitTrackerCreate: {
usage: "Creates a file with git and version information for deployment identification",
lifecycleEvents: ['set'],
},
gitCommitVersion: {
usage: "Returns the latest commit version from git",
lifecycleEvents: ['set'],
},
};
this.hooks = {
// 'before:welcome:hello': this.version-file.bind(this),
"before:package:initialize": this.createVersionFile.bind(this),
"gitTrackerCreate:set": this.createVersionFile.bind(this),
"gitCommitVersion:set": this.getGitVersion.bind(this),
"before:offline:start:init": this.createVersionFile.bind(this),
};
this.gitCmd = 'git log --name-status HEAD^..HEAD | head -1 | cut -c8-14';
}
//This executes an OS command
runExec(cmd) {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error || stderr) {
reject(error || stderr);
}
else resolve(stdout);
});
});
}
isvalidObject(item) {
return item && typeof item == "object";
}
async getGitVersion() {
await this.runExec(this.gitCmd).then(data => {
this.serverless.cli.log(`Latest git Commit: ${data}`);
}).catch(err => {
this.serverless.cli.log("git Not Installed");
});
}
async createMessage(html) {
let git
await this.runExec(this.gitCmd).then(data => {
git = data;
}).catch(err => {
git = "Not Installed";
});
let message = `Git Version: ${git}\nDate: `;
message += new Date().toLocaleString() + "\n";//.replace(/T/, ' ').replace(/\..+/, '')
message += `Stage: ${this.stage}\n`;
//If we want HTML then make the new lines html line breaks
if (html) message = message.replace(/\n/g,"<br/>");
return message
}
async replaceFile(location,message,regex) {
//read the file
let text = fs.readFileSync(location)
//Use the regex from serverless.yml to create a new regex with "s" option
//(dot matches new line)
let replace_regex = new RegExp(`${regex}`,"s")
//replace anchors in the file with the message
text = text.toString().replace(replace_regex,`$1${message}$2`);
//write the new text back to the file
fs.writeFileSync(location, text)
}
async createVersionFile() {
let params = this.deploymentTracker;
// If we don't have an object then get out of here
if (!this.isvalidObject(params)) {
return;
}
// Only execute if we have a location for our file
if (typeof params.location != undefined && params.location) {
//Determine if we should deploy based on the deployment stage
let skipDeployment = false;
skipDeployment = (this.isvalidObject(params.deployment) && ! params.deployment.includes(this.stage));
if (! skipDeployment) {
let message = await this.createMessage(params.html);
//this.serverless.cli.log(`Message: ${message}`);
//If we've specified a regex expression then we'll replace that expression in the file.
//Otherwise we'll just write (overwrite) the file
if (params.regex) {
this.serverless.cli.log(`Replacing version file.`);
this.replaceFile(params.location, message, params.regex)
}
else {
this.serverless.cli.log(`Creating version file.`);
fs.writeFileSync(params.location, message);
}
}
else this.serverless.cli.log("Skipping Deployment Tracker");
}
}
} //class
module.exports = ServerlessPlugin;