Skip to content

Commit c04bbc4

Browse files
committed
feat(web): add custom scripts plugin
1 parent c5cfaea commit c04bbc4

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

website/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
"name": "website",
33
"version": "1.0.0",
44
"main": "index.js",
5+
"type": "module",
56
"license": "MIT",
67
"dependencies": {
78
"express": "^4.19.2",
89
"serverless-http": "^3.2.0"
910
},
1011
"devDependencies": {
11-
"serverless-domain-manager": "^7.4.0",
12-
"serverless-plugin-scripts": "^1.0.2"
12+
"serverless-domain-manager": "^7.4.0"
1313
}
1414
}

website/scripts.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { execSync } from "child_process";
2+
3+
class Scripts {
4+
constructor(serverless) {
5+
this.serverless = serverless;
6+
this.commands = {};
7+
this.hooks = {};
8+
9+
this.defineCommands();
10+
this.defineHooks();
11+
}
12+
13+
getConfig() {
14+
const service = this.serverless.service;
15+
return service.custom && service.custom.scripts;
16+
}
17+
18+
defineCommands() {
19+
const config = this.getConfig();
20+
const commands = config && config.commands;
21+
if (!commands) return;
22+
23+
for (const name of Object.keys(commands)) {
24+
if (!this.commands[name]) {
25+
this.commands[name] = { lifecycleEvents: [] };
26+
}
27+
this.commands[name].lifecycleEvents.push(name);
28+
29+
this.hooks[`${name}:${name}`] = this.runCommand.bind(this, name);
30+
}
31+
}
32+
33+
defineHooks() {
34+
const config = this.getConfig();
35+
const hooks = config && config.hooks;
36+
if (!hooks) return;
37+
38+
for (const name of Object.keys(hooks)) {
39+
this.hooks[name] = this.runHook.bind(this, name);
40+
}
41+
}
42+
43+
runCommand(name) {
44+
const commands = this.getConfig().commands;
45+
const command = commands[name];
46+
this.execute(command);
47+
}
48+
49+
runHook(name) {
50+
const hooks = this.getConfig().hooks;
51+
const hook = hooks[name];
52+
this.execute(hook);
53+
}
54+
55+
execute(command) {
56+
execSync(command, { stdio: ["ignore", "ignore", "inherit"] });
57+
}
58+
}
59+
60+
export default Scripts;

website/serverless.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ provider:
55
runtime: nodejs20.x
66

77
plugins:
8-
- serverless-plugin-scripts
98
- serverless-domain-manager
9+
- ./scripts
1010

1111
build:
1212
esbuild: true

0 commit comments

Comments
 (0)