-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathscripts.js
83 lines (67 loc) · 2.29 KB
/
scripts.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
import { execSync } from "child_process";
/**
* This is a custom Serverless Framework Plugin that allows you to
* define and run custom scripts in your serverless.yml file, similar to npm scripts.
* For more information on creating custom plugins, see the documentation:
* https://www.serverless.com/framework/docs/guides/plugins/creating-plugins
*
* In this AI example, we need to run vite build script before deploying the website service.
* So we built this quick plugin, and loaded it in the serverless.yml file.
*/
class Scripts {
constructor(serverless, options, utils) {
this.serverless = serverless;
this.options = options; // CLI options are passed to the plugin
this.utils = utils; // Helper logging functions are passed to the plugin
this.commands = {};
this.hooks = {};
this.defineCommands();
this.defineHooks();
}
getConfig() {
const service = this.serverless.service;
return service.custom && service.custom.scripts;
}
defineCommands() {
const config = this.getConfig();
const commands = config && config.commands;
if (!commands) return;
for (const name of Object.keys(commands)) {
if (!this.commands[name]) {
this.commands[name] = { lifecycleEvents: [] };
}
this.commands[name].lifecycleEvents.push(name);
this.hooks[`${name}:${name}`] = this.runCommand.bind(this, name);
}
}
defineHooks() {
const config = this.getConfig();
const hooks = config && config.hooks;
if (!hooks) return;
for (const name of Object.keys(hooks)) {
this.hooks[name] = this.runHook.bind(this, name);
}
}
runCommand(name) {
const commands = this.getConfig().commands;
const command = commands[name];
this.execute(command);
}
runHook(name) {
const hooks = this.getConfig().hooks;
const hook = hooks[name];
this.execute(hook);
}
execute(command) {
// By default, only show stderr in the terminal
// So that you can see any build errors that may occur
let stdio = ["ignore", "ignore", "inherit"];
// But in verbose or debug mode, we show all output
if (this.options.verbose || this.options.debug) {
stdio = "inherit";
}
// Execute the command/script in a child service
execSync(command, { stdio });
}
}
export default Scripts;