-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerlessRunWatch.js
148 lines (134 loc) · 3.95 KB
/
ServerlessRunWatch.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
138
139
140
141
142
143
144
145
146
147
148
const childProcess = require("node:child_process");
const chokidar = require("chokidar");
const slsLogs = require("@serverless/utils/log");
const mainProgress = slsLogs.progress.get("main");
const chokidarConfig = {
interval: 500,
awaitWriteFinish: { stabilityThreshold: 2000, pollInterval: 100 },
};
/*
* Watches your code for changes
* Runs fast deploys with 'serverless deploy function'
* Tails logs
*
*/
class ServerlessRunWatch {
constructor(serverless, cliOptions) {
this.cliOptions = cliOptions;
this.serverless = serverless;
this.serviceDir = serverless.config.serviceDir;
this.watchPaths = [];
this.slsRegex = /serverless.(yml|yaml|json|ts)$/;
this.spawnSlsOptions = []
this.hooks = {
"run-watch:start": this.start.bind(this),
initialize: this.init.bind(this),
};
this.commands = {
"run-watch": {
lifecycleEvents: ["start"],
options: {
function: {
type: "string",
usage: "Specify a function",
required: true,
shortcut: "f",
},
config: {
type: "string",
usage: "Path to serverless config file",
shortcut: "c",
},
"disable-logs": {
type: "boolean",
usage: "Skip tailing logs",
},
"watch-glob": {
type: "string",
usage: "Comma separated list of files or globs to watch",
},
},
usage:
"Watch and redeploy your Lambda function when code is changed. Uses direct function updates for speed",
},
};
}
async init() {
if (!this.cliOptions["disable-logs"]) {
let plugins = this.serverless.cli.loadedPlugins;
for (let plugin of plugins) {
if (plugin.constructor.name === "AwsLogs") {
plugin.options["tail"] = true;
}
}
}
this.spawnSlsOptions = Object.entries(this.cliOptions).reduce((accum, [k, v]) => {
if (['config', 'stage', 'region', 'verbose', 'force'].includes(k)) {
accum.push(`--${k}`, v)
}
return accum
}, [])
if (this.cliOptions["watch-glob"]) {
this.watchPaths.push(...this.cliOptions["watch-glob"].split(","));
} else {
this.watchPaths.push(`${this.serviceDir}/**/*.(js|mjs|py|ts|go|java|rb)`);
}
if (this.cliOptions.config) {
this.watchPaths.push(this.cliOptions.config);
} else {
this.watchPaths.push(`${this.serviceDir}/serverless.(yml|yaml|json|ts)`);
}
}
async tailLogs() {
await this.serverless.pluginManager.spawn("logs");
}
spawnServerless() {
const cmdArray = [
"deploy",
"function",
"--function",
this.cliOptions.function,
...this.spawnSlsOptions
];
return childProcess.execFileSync("serverless", cmdArray, { stdio: "inherit" });
}
async delay(time) {
return new Promise((res) => setTimeout(res, time));
}
async setCli() {
if (this.cliOptions["disable-logs"]) {
mainProgress.notice("Waiting for file change", { isMainEvent: true });
await this.delay(5000000);
} else {
mainProgress.notice("Waiting for logs", { isMainEvent: true });
await this.tailLogs();
}
}
serverlessFileChanged(event) {
if (this.cliOptions.config) {
return event.includes(this.cliOptions.config);
} else {
return event.match(this.slsRegex) !== null;
}
}
async processEvent(event) {
// Serverless won't reload itself
// so if the file changes, we have to shell-out
// to a new serverless instance
if (this.serverlessFileChanged(event)) {
this.spawnServerless();
} else {
await this.serverless.pluginManager.spawn("deploy:function");
}
await this.setCli();
}
async start() {
chokidar
.watch(this.watchPaths, chokidarConfig)
.on("change", async (event) => {
await this.processEvent(event);
});
await this.setCli();
}
}
module.exports = ServerlessRunWatch;