Skip to content

Commit 628e753

Browse files
authored
feat: add TypeScript types (#15)
1 parent 7b17c20 commit 628e753

File tree

4 files changed

+57
-16
lines changed

4 files changed

+57
-16
lines changed

package.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"description": "tinyspawn is a minimalistic wrapper around child_process",
44
"homepage": "https://github.com/microlinkhq/tinyspawn",
55
"version": "1.2.14",
6+
"types": "src/index.d.ts",
67
"main": "src/index.js",
78
"author": {
89
"email": "[email protected]",
@@ -56,7 +57,8 @@
5657
"simple-git-hooks": "latest",
5758
"standard": "latest",
5859
"standard-markdown": "latest",
59-
"standard-version": "latest"
60+
"standard-version": "latest",
61+
"tsd": "latest"
6062
},
6163
"engines": {
6264
"node": ">= 18"
@@ -68,7 +70,7 @@
6870
"clean": "rm -rf node_modules",
6971
"contributors": "(npx git-authors-cli && npx finepack && git add package.json && git commit -m 'build: contributors' --no-verify) || true",
7072
"coverage": "c8 report --reporter=text-lcov > coverage/lcov.info",
71-
"lint": "standard",
73+
"lint": "standard && tsd",
7274
"postrelease": "npm run release:tags && npm run release:github && (ci-publish || npm publish --access=public)",
7375
"prerelease": "npm run update:check",
7476
"pretest": "npm run lint",
@@ -102,5 +104,8 @@
102104
"simple-git-hooks": {
103105
"commit-msg": "npx commitlint --edit",
104106
"pre-commit": "npx nano-staged"
107+
},
108+
"tsd": {
109+
"directory": "test"
105110
}
106111
}

src/index.d.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { ChildProcess, SpawnOptions } from 'child_process';
2+
3+
interface ExtendOptions extends SpawnOptions {
4+
json?: boolean;
5+
}
6+
7+
interface ResolvedSubprocess extends Omit<ChildProcess, 'stdout' | 'stderr'> {
8+
stdout: string;
9+
stderr: string;
10+
}
11+
12+
interface SubprocessPromise extends Promise<ResolvedSubprocess>, ChildProcess {}
13+
14+
declare function extend(defaults?: ExtendOptions): (input: string, args?: string[] | ExtendOptions, options?: ExtendOptions) => SubprocessPromise;
15+
16+
declare const $: ReturnType<typeof extend> & {
17+
extend: typeof extend;
18+
json: ReturnType<typeof extend>;
19+
};
20+
21+
export = $;

test/index.js

-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

3-
const { setTimeout } = require('timers/promises')
43
const { execSync } = require('child_process')
54
const { Writable } = require('stream')
65
const { EOL } = require('os')
@@ -22,19 +21,6 @@ test('meaningful errors', async t => {
2221
t.is(error.killed, false)
2322
})
2423

25-
test.skip('control process lifecycle', async t => {
26-
t.plan(2)
27-
try {
28-
const subprocess = $('sleep 100')
29-
await setTimeout(500)
30-
subprocess.kill('SIGKILL')
31-
await subprocess
32-
} catch (error) {
33-
t.is(error.killed, true)
34-
t.is(error.signalCode, 'SIGKILL')
35-
}
36-
})
37-
3824
test.serial('run a command', async t => {
3925
{
4026
const result = await $('echo hello world')

test/index.test-d.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import $ from '..'
2+
3+
/* basic */
4+
5+
const promise = $('curl https://www.youtube.com/watch?v=6xKWiCMKKJg')
6+
7+
// at this point it's a child_process instance
8+
promise.kill()
9+
promise.ref()
10+
promise.unref()
11+
12+
const result = await promise
13+
14+
// after that stdout/stderr are available as string values
15+
16+
console.log(result.stdout)
17+
console.log(result.stderr)
18+
19+
console.log(result.connected)
20+
console.log(result.signalCode)
21+
console.log(result.exitCode)
22+
console.log(result.killed)
23+
console.log(result.spawnfile)
24+
console.log(result.spawnargs)
25+
console.log(result.pid)
26+
console.log(result.stdin)
27+
console.log(result.stdout)
28+
console.log(result.stderr)
29+
console.log(result.stdin)

0 commit comments

Comments
 (0)