spooder 3.0.6 → 3.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -71,13 +71,26 @@ When starting your server, `spooder` can automatically update the source code in
71
71
  ```json
72
72
  {
73
73
  "spooder": {
74
- "update": "git pull && bun install"
74
+ "update": "git pull"
75
75
  }
76
76
  }
77
77
  ```
78
- It is worth nothing that if the `update` command fails to execute, the server will still be started. This is preferred over entering a restart loop or failing to start the server at all.
78
+ To execute multiple commands in sequence, provide an array rather than using the `&&` operator.
79
79
 
80
- As well as being executed when the server is first started, the `update` command is also run when `spooder` automatically restarts the server after it exits.
80
+ ```json
81
+ {
82
+ "spooder": {
83
+ "update": [
84
+ "git pull",
85
+ "bun install"
86
+ ]
87
+ }
88
+ }
89
+ ```
90
+
91
+ If a command in the sequence fails, the remaining commands will not be executed. However, if the update fails, the server will still be started. This is preferred over entering a restart loop or failing to start the server at all.
92
+
93
+ As well as being executed when the server is first started, the `update` commands are also run when `spooder` automatically restarts the server after it exits.
81
94
 
82
95
  You can utilize this to automatically update your server in response to a webhook or other event by simply exiting the process.
83
96
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "spooder",
3
3
  "type": "module",
4
- "version": "3.0.6",
4
+ "version": "3.0.8",
5
5
  "exports": {
6
6
  ".": {
7
7
  "bun": "./src/api.ts",
package/src/cli.d.ts CHANGED
@@ -1 +1,2 @@
1
+ #!/usr/bin/env bun
1
2
  export {};
package/src/cli.ts CHANGED
@@ -1,3 +1,4 @@
1
+ #!/usr/bin/env bun
1
2
  import path from 'node:path';
2
3
 
3
4
  type Config = Record<string, unknown>;
@@ -58,41 +59,59 @@ function log(message: string, ...args: unknown[]): void {
58
59
  }
59
60
 
60
61
  const config = await load_config();
61
- const config_update_command = config.update as string ?? '';
62
62
  const config_run_command = config.run as string ?? 'bun run index.ts';
63
63
  const config_auto_restart_ms = config.autoRestart as number ?? 5000;
64
64
 
65
+ let config_update_commands = [] as string[];
66
+ if (config.update) {
67
+ if (typeof config.update === 'string')
68
+ config_update_commands = [config.update]
69
+ else if (Array.isArray(config.update))
70
+ config_update_commands = config.update;
71
+ }
72
+
65
73
  async function start_server() {
66
74
  log('start_server');
67
75
 
68
- if (config_update_command.length > 0) {
69
- log('running update command: %s', config_update_command);
70
- const update = Bun.spawn(parse_command(config_update_command), {
71
- cwd: process.cwd(),
72
- stdout: 'inherit',
73
- stderr: 'inherit'
74
- });
76
+ if (config_update_commands.length > 0) {
77
+ log('running %d update commands', config_update_commands.length);
78
+
79
+ for (let i = 0; i < config_update_commands.length; i++) {
80
+ const config_update_command = config_update_commands[i];
81
+
82
+ log('[%d] %s', i, config_update_command);
83
+
84
+ const update_proc = Bun.spawn(parse_command(config_update_command), {
85
+ cwd: process.cwd(),
86
+ stdout: 'inherit',
87
+ stderr: 'inherit'
88
+ });
75
89
 
76
- await update.exited;
77
- log('auto update exited with code %d', update.exitCode)
90
+ await update_proc.exited;
91
+
92
+ log('[%d] exited with code %d', i, update_proc.exitCode);
93
+
94
+ if (update_proc.exitCode !== 0) {
95
+ log('aborting update due to non-zero exit code from [%d]', i);
96
+ break;
97
+ }
98
+ }
78
99
  }
79
100
 
80
- const server_process = Bun.spawn(parse_command(config_run_command), {
101
+ Bun.spawn(parse_command(config_run_command), {
81
102
  cwd: process.cwd(),
82
103
  stdout: 'inherit',
83
- stderr: 'inherit'
84
- });
85
-
86
- log('server process started with pid %d', server_process.pid);
104
+ stderr: 'inherit',
87
105
 
88
- server_process.exited.then(() => {
89
- log('server exited with code %d', server_process.exitCode);
106
+ onExit: (proc, exitCode, signal) => {
107
+ log('server exited with code %d', exitCode);
90
108
 
91
- if (config_auto_restart_ms > -1) {
92
- log('restarting server in %dms', config_auto_restart_ms);
93
- setTimeout(start_server, config_auto_restart_ms);
109
+ if (config_auto_restart_ms > -1) {
110
+ log('restarting server in %dms', config_auto_restart_ms);
111
+ setTimeout(start_server, config_auto_restart_ms);
112
+ }
94
113
  }
95
114
  });
96
115
  }
97
116
 
98
- start_server();
117
+ await start_server();