Skip to content

Commit 8a7add0

Browse files
committed
convert to typescript
1 parent 942fe2d commit 8a7add0

File tree

14 files changed

+3158
-521
lines changed

14 files changed

+3158
-521
lines changed

.eslintrc.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
root: true
2-
extends: silverwind
2+
extends:
3+
- silverwind
4+
- silverwind-typescript

.github/workflows/ci.yaml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@ on: [push, pull_request]
33

44
jobs:
55
test:
6-
strategy:
7-
fail-fast: false
8-
matrix:
9-
node: [18, 20]
10-
os: [ubuntu-latest, macos-latest, windows-latest]
11-
126
runs-on: ${{matrix.os}}
137
steps:
14-
- uses: actions/checkout@v3
15-
- uses: actions/setup-node@v3
8+
- uses: actions/checkout@v4
9+
- uses: actions/setup-node@v4
10+
with:
11+
node-version: latest
12+
- uses: oven-sh/setup-bun@v1
1613
with:
17-
node-version: ${{matrix.node}}
14+
bun-version: latest
1815
- run: make lint test

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/.vscode
2+
/dist
23
/node_modules
34
/npm-debug.log*
4-
/yarn-error.log
5-
/yarn.lock

Makefile

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
SOURCE_FILES := index.ts
2+
DIST_FILES := dist/index.js
3+
14
node_modules: package-lock.json
25
npm install --no-save
36
@touch node_modules
@@ -7,19 +10,27 @@ deps: node_modules
710

811
.PHONY: lint
912
lint: node_modules
10-
npx eslint --color .
13+
npx eslint --ext js,jsx,ts,tsx --color .
14+
npx tsc
1115

1216
.PHONY: lint-fix
1317
lint-fix: node_modules
14-
npx eslint --color . --fix
18+
npx eslint --ext js,jsx,ts,tsx --color . --fix
19+
npx tsc
1520

1621
.PHONY: test
1722
test: node_modules
18-
npx vitest
23+
bun test
1924

2025
.PHONY: test-update
2126
test-update: node_modules
22-
npx vitest --update
27+
bun test
28+
29+
.PHONY: build
30+
build: node_modules $(DIST_FILES)
31+
32+
$(DIST_FILES): $(SOURCE_FILES) package-lock.json vite.config.ts
33+
npx vite build
2334

2435
.PHONY: publish
2536
publish: node_modules
@@ -34,16 +45,16 @@ update: node_modules
3445
@touch node_modules
3546

3647
.PHONY: path
37-
patch: node_modules lint test
48+
patch: node_modules lint test build
3849
npx versions patch package.json package-lock.json
3950
@$(MAKE) --no-print-directory publish
4051

4152
.PHONY: minor
42-
minor: node_modules lint test
53+
minor: node_modules lint test build
4354
npx versions minor package.json package-lock.json
4455
@$(MAKE) --no-print-directory publish
4556

4657
.PHONY: major
47-
major: node_modules lint test
58+
major: node_modules lint test build
4859
npx versions major package.json package-lock.json
4960
@$(MAKE) --no-print-directory publish

child.js renamed to child.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {daemonizeProcess} from "./index.js";
1+
import {daemonizeProcess} from "./index.ts";
22
import {writeFileSync} from "node:fs";
33
import {env, ppid} from "node:process";
44

index.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

index.test.js renamed to index.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import {readFile, copyFile, rm} from "node:fs/promises";
55
import {join} from "node:path";
66

77
const testDir = mkdtempSync(join(tmpdir(), "daemonize-process-"));
8-
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
8+
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
99

1010
beforeAll(async () => {
11-
await copyFile(new URL("index.js", import.meta.url), join(testDir, "index.js"));
12-
await copyFile(new URL("child.js", import.meta.url), join(testDir, "child.js"));
11+
await copyFile(new URL("index.ts", import.meta.url), join(testDir, "index.ts"));
12+
await copyFile(new URL("child.ts", import.meta.url), join(testDir, "child.ts"));
1313
await copyFile(new URL("package.json", import.meta.url), join(testDir, "package.json"));
1414
});
1515

@@ -19,7 +19,7 @@ afterAll(async () => {
1919

2020
test("simple", () => {
2121
return new Promise(resolve => {
22-
const child = fork(join(testDir, "child.js"));
22+
const child = fork(join(testDir, "child.ts"));
2323

2424
child.on("exit", async () => {
2525
await sleep(1000); // give the child some time to exit
@@ -35,7 +35,7 @@ test("simple", () => {
3535

3636
// verify that internal tracking variable is not leaked to the child
3737
expect(envVar).toEqual("false");
38-
resolve();
38+
resolve(undefined);
3939
});
4040
});
4141
});

index.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {spawn} from "node:child_process";
2+
import {env, cwd, execPath, argv, exit} from "node:process";
3+
import type {SpawnOptions} from "node:child_process";
4+
5+
const id = "_DAEMONIZE_PROCESS";
6+
7+
type DaemonizeProcessOpts = {
8+
/** The path to the script to be executed. Default: The current script. */
9+
script?: string,
10+
/** The command line arguments to be used. Default: The current arguments. */
11+
arguments?: string[],
12+
/** The path to the Node.js binary to be used. Default: The current Node.js binary. */
13+
node?: string,
14+
/** The exit code to be used when exiting the parent process. Default: `0`. */
15+
exitCode?: number,
16+
} & SpawnOptions;
17+
18+
export function daemonizeProcess(opts: DaemonizeProcessOpts = {}) {
19+
if (id in env) {
20+
// In the child, clean up the tracking environment variable
21+
delete env[id];
22+
} else {
23+
// In the parent, set the tracking environment variable, fork the child and exit
24+
const o: DaemonizeProcessOpts = {
25+
// spawn options
26+
env: Object.assign(env, opts.env, {[id]: "1"}),
27+
cwd: cwd(),
28+
stdio: "ignore",
29+
detached: true,
30+
// custom options
31+
node: execPath,
32+
script: argv[1],
33+
arguments: argv.slice(2),
34+
exitCode: 0,
35+
...opts,
36+
};
37+
38+
const args: string[] = [o.script as string, ...(o.arguments as string[])];
39+
const proc: any = spawn(o.node as string, args, o);
40+
proc?.unref?.();
41+
exit(o.exitCode);
42+
}
43+
}

0 commit comments

Comments
 (0)