Skip to content

Commit 0619201

Browse files
authored
Merge pull request #17 from serverless/add-custom-scripts-plugin
feat(web): add custom scripts plugin
2 parents 3ce5c0a + 03e6fb9 commit 0619201

File tree

4 files changed

+90
-12
lines changed

4 files changed

+90
-12
lines changed

package-lock.json

+1-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

website/package.json

+2-2
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

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { execSync } from "child_process";
2+
3+
/**
4+
* This is a custom Serverless Framework Plugin that allows you to
5+
* define and run custom scripts in your serverless.yml file, similar to npm scripts.
6+
* For more information on creating custom plugins, see the documentation:
7+
* https://www.serverless.com/framework/docs/guides/plugins/creating-plugins
8+
*
9+
* In this AI example, we need to run vite build script before deploying the website service.
10+
* So we built this quick plugin, and loaded it in the serverless.yml file.
11+
*/
12+
13+
class Scripts {
14+
constructor(serverless, options, utils) {
15+
this.serverless = serverless;
16+
this.options = options; // CLI options are passed to the plugin
17+
this.utils = utils; // Helper logging functions are passed to the plugin
18+
19+
this.commands = {};
20+
this.hooks = {};
21+
22+
this.defineCommands();
23+
this.defineHooks();
24+
}
25+
26+
getConfig() {
27+
const service = this.serverless.service;
28+
return service.custom && service.custom.scripts;
29+
}
30+
31+
defineCommands() {
32+
const config = this.getConfig();
33+
const commands = config && config.commands;
34+
if (!commands) return;
35+
36+
for (const name of Object.keys(commands)) {
37+
if (!this.commands[name]) {
38+
this.commands[name] = { lifecycleEvents: [] };
39+
}
40+
this.commands[name].lifecycleEvents.push(name);
41+
42+
this.hooks[`${name}:${name}`] = this.runCommand.bind(this, name);
43+
}
44+
}
45+
46+
defineHooks() {
47+
const config = this.getConfig();
48+
const hooks = config && config.hooks;
49+
if (!hooks) return;
50+
51+
for (const name of Object.keys(hooks)) {
52+
this.hooks[name] = this.runHook.bind(this, name);
53+
}
54+
}
55+
56+
runCommand(name) {
57+
const commands = this.getConfig().commands;
58+
const command = commands[name];
59+
this.execute(command);
60+
}
61+
62+
runHook(name) {
63+
const hooks = this.getConfig().hooks;
64+
const hook = hooks[name];
65+
this.execute(hook);
66+
}
67+
68+
execute(command) {
69+
// By default, only show stderr in the terminal
70+
// So that you can see any build errors that may occur
71+
let stdio = ["ignore", "ignore", "inherit"];
72+
73+
// But in verbose or debug mode, we show all output
74+
if (this.options.verbose || this.options.debug) {
75+
stdio = "inherit";
76+
}
77+
78+
// Execute the command/script in a child service
79+
execSync(command, { stdio });
80+
}
81+
}
82+
83+
export default Scripts;

website/serverless.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ provider:
55
runtime: nodejs20.x
66

77
plugins:
8-
- serverless-plugin-scripts
9-
- serverless-domain-manager
8+
- serverless-domain-manager # Load the community plugin for custom domains installed with npm
9+
- ./scripts # Load our custom scripts plugin
1010

1111
build:
1212
esbuild: true
@@ -25,6 +25,8 @@ custom:
2525
apiType: http
2626
autoDomain: true
2727
enabled: ${param:customDomainNameEnabled}
28+
29+
# This property is expected by our custom scripts plugin.
2830
scripts:
2931
hooks:
3032
# This hook builds the React App. It sets the environment variables for

0 commit comments

Comments
 (0)