spooder 2.0.5 → 2.0.7

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
@@ -16,5 +16,26 @@ The `instance` is an API that can be imported into a Bun process to scaffold a w
16
16
 
17
17
  The `watcher` is a daemon responsible for updating, starting and monitoring a collection of `instance` processes. It is intended to be run as a service on the host machine.
18
18
 
19
+ ## Watcher
20
+
21
+ ```toml
22
+ # /var/www/spooder.toml
23
+ [[domains]]
24
+ name = "testdomain.net"
25
+ path = "/var/www/testdomain.net"
26
+ port = 3000
27
+ ```
28
+
29
+ ```bash
30
+ # Installation
31
+ bun add spooder -g
32
+ ```
33
+
34
+ ```bash
35
+ # Running the watcher
36
+ screen -S spooder # Create a new screen session
37
+ spooder
38
+ ```
39
+
19
40
  ## License
20
41
  The code in this repository is licensed under the ISC license. See the [LICENSE](LICENSE) file for more information.
package/bun.lockb CHANGED
Binary file
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "spooder",
3
3
  "type": "module",
4
- "version": "2.0.5",
4
+ "version": "2.0.7",
5
5
  "exports": {
6
6
  ".": {
7
7
  "bun": "./src/api.ts",
@@ -9,9 +9,15 @@
9
9
  }
10
10
  },
11
11
  "bin": {
12
- "spooder": "./src/watcher.ts"
12
+ "spooder": "./src/instance.ts",
13
+ "spooder-update": "./src/update.ts",
14
+ "spooder-heartbeat": "./src/heartbeat.ts"
13
15
  },
14
16
  "devDependencies": {
17
+ "@types/node": "^20.0.0",
15
18
  "bun-types": "^0.5.0"
19
+ },
20
+ "dependencies": {
21
+ "zod": "^3.21.4"
16
22
  }
17
23
  }
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bun
2
+
3
+ // TODO: Iterate over all directories in /var/www and identify which ones are spooder domains.
4
+ // TODO: For each spooder domain, check if the process is running.
5
+ // TODO: Run `spooder` inside any domain directory that is not running.
6
+ // TODO: Report crashed domains somewhere/somehow?
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env bun
2
+ export {};
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env bun
2
+
3
+ import { join } from 'node:path';
4
+
5
+ async function write_pid_file() {
6
+ const pid_file = join(process.cwd(), '.spooder_pid');
7
+ await Bun.write(pid_file, process.pid.toString());
8
+ }
9
+
10
+ async function main() {
11
+ await write_pid_file();
12
+
13
+ const index_file = join(process.cwd(), 'index.ts');
14
+ // TODO: Add error handling if index_file does not exist?
15
+
16
+ const module = await import(index_file);
17
+ console.log(module);
18
+ // TODO: Add a route for GitHub webhooks.
19
+ // TODO: Add authentication for the webhook to prevent abuse.
20
+ // TODO: Run `spooder-update` when the webhook is triggered, then self-terminate.
21
+ }
22
+
23
+ main();
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env bun
2
+ export {};
package/src/update.ts ADDED
@@ -0,0 +1,93 @@
1
+ #!/usr/bin/env bun
2
+
3
+ import { join } from 'node:path';
4
+ import { z } from 'zod';
5
+
6
+ const ConfigSchema = z.object({
7
+ domain: z.object({
8
+ name: z.string(),
9
+ port: z.number()
10
+ }),
11
+
12
+ git: z.object({
13
+ url: z.string(),
14
+ branch: z.string().default('main')
15
+ }).optional()
16
+ });
17
+
18
+ async function get_last_pid() : Promise<number> {
19
+ const pid_file = Bun.file(join(process.cwd(), '.spooder_pid'));
20
+ if (pid_file.size > 0) {
21
+ const pid = parseInt(await pid_file.text());
22
+ if (!isNaN(pid))
23
+ return pid;
24
+ }
25
+
26
+ return -1;
27
+ }
28
+
29
+ async function pid_is_running(pid: number) : Promise<boolean> {
30
+ const process = Bun.spawnSync(['ps', '-p', pid.toString()]);
31
+ if (!process.success)
32
+ return false;
33
+
34
+ const stdout = process.stdout?.toString();
35
+
36
+ // Ensure stdout contains more than one line.
37
+ if (!stdout || stdout.split('\n').length <= 1)
38
+ return false;
39
+
40
+ return false;
41
+ }
42
+
43
+ async function wait_for_pid_to_exit(pid: number) : Promise<void> {
44
+ console.log(`Waiting for process ${pid} to exit...`);
45
+ while (await pid_is_running(pid))
46
+ await Bun.sleep(1000);
47
+ }
48
+
49
+ function is_git_repository(directory: string): boolean {
50
+ // The command `git rev-parse --is-inside-work-tree` returns `true` to stdout
51
+ // if the current working directory is inside a git repository.
52
+ const process = Bun.spawnSync(['git', 'rev-parse', '--is-inside-work-tree'], { cwd: directory });
53
+ return process.stdout?.toString().trim() === 'true';
54
+ }
55
+
56
+ function pull_git_repository(directory: string, url: string, branch: string) {
57
+ console.log(`Pulling git repository ${branch} @ ${url} into ${directory}...`);
58
+ const process = Bun.spawnSync(['git', 'pull', url, branch], { cwd: directory });
59
+ if (!process.success)
60
+ throw new Error('Failed to pull git repository.');
61
+ }
62
+
63
+ function init_git_repository(directory: string) {
64
+ console.log(`Initializing git repository in ${directory}...`);
65
+ const process = Bun.spawnSync(['git', 'init'], { cwd: directory });
66
+ if (!process.success)
67
+ throw new Error('Failed to initialize git repository.');
68
+ }
69
+
70
+ async function load_configuration() {
71
+ const config_file = join(process.cwd(), 'spooder.toml');
72
+ return ConfigSchema.parse(await import(config_file));
73
+ }
74
+
75
+ async function main() {
76
+ const config = await load_configuration();
77
+
78
+ // Wait for the previous process, if it exists, to exit.
79
+ await wait_for_pid_to_exit(await get_last_pid());
80
+
81
+ const cwd = process.cwd();
82
+ if (config.git) {
83
+ if (!is_git_repository(cwd))
84
+ init_git_repository(cwd);
85
+
86
+ pull_git_repository(cwd, config.git.url, config.git.branch);
87
+ }
88
+
89
+ const server = Bun.spawn(['spooder'], { cwd });
90
+ server.unref();
91
+ }
92
+
93
+ main();
package/tsconfig.json CHANGED
@@ -16,6 +16,7 @@
16
16
  "declaration": true,
17
17
  "allowJs": true,
18
18
  "types": [
19
+ "node",
19
20
  "bun-types" // add Bun global
20
21
  ]
21
22
  }
package/src/api.d.ts DELETED
@@ -1,2 +0,0 @@
1
- declare function test_function(): void;
2
- export { test_function };
package/src/api.ts DELETED
@@ -1,5 +0,0 @@
1
- function test_function() {
2
- console.log("Hello World");
3
- }
4
-
5
- export { test_function };
package/src/watcher.ts DELETED
@@ -1,7 +0,0 @@
1
- #!/usr/bin/env bun
2
-
3
- console.log('Begin test.');
4
-
5
- setInterval(() => {
6
- console.log('This is a test.');
7
- }, 1000);
File without changes