Skip to content

Commit def0883

Browse files
authoredMar 26, 2024··
feat: export kill helper (#744)
* feat: export `kill` helper from core * test: check index exports
1 parent fac8630 commit def0883

File tree

4 files changed

+115
-18
lines changed

4 files changed

+115
-18
lines changed
 

‎src/core.ts

+15-16
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export interface Options {
6767
spawn: typeof spawn
6868
spawnSync: typeof spawnSync
6969
log: typeof log
70+
kill: typeof kill
7071
}
7172

7273
const storage = new AsyncLocalStorage<Options>()
@@ -95,6 +96,7 @@ export const defaults: Options = {
9596
spawn,
9697
spawnSync,
9798
log,
99+
kill,
98100
}
99101
const isWin = process.platform == 'win32'
100102
try {
@@ -173,13 +175,6 @@ export const $: Shell & Options = new Proxy<Shell & Options>(
173175
}
174176
)
175177

176-
function substitute(arg: ProcessPromise | any) {
177-
if (arg?.stdout) {
178-
return arg.stdout.replace(/\n$/, '')
179-
}
180-
return `${arg}`
181-
}
182-
183178
type Resolve = (out: ProcessOutput) => void
184179
type IO = StdioPipe | StdioNull
185180

@@ -412,15 +407,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
412407
throw new Error('Trying to kill a process without creating one.')
413408
if (!this.child.pid) throw new Error('The process pid is undefined.')
414409

415-
let children = await ps.tree({ pid: this.child.pid, recursive: true })
416-
for (const p of children) {
417-
try {
418-
process.kill(+p.pid, signal)
419-
} catch (e) {}
420-
}
421-
try {
422-
process.kill(-this.child.pid, signal)
423-
} catch (e) {}
410+
return $.kill(this.child.pid, signal)
424411
}
425412

426413
stdio(stdin: IO, stdout: IO = 'pipe', stderr: IO = 'pipe'): ProcessPromise {
@@ -573,6 +560,18 @@ export function cd(dir: string | ProcessOutput) {
573560
$[processCwd] = process.cwd()
574561
}
575562

563+
export async function kill(pid: number, signal?: string) {
564+
let children = await ps.tree({ pid, recursive: true })
565+
for (const p of children) {
566+
try {
567+
process.kill(+p.pid, signal)
568+
} catch (e) {}
569+
}
570+
try {
571+
process.kill(-pid, signal)
572+
} catch (e) {}
573+
}
574+
576575
export type LogEntry =
577576
| {
578577
kind: 'cmd'

‎src/util.ts

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { promisify } from 'node:util'
1615
import { chalk } from './vendor.js'
1716

1817
export function noop() {}

‎test/all.test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import './experimental.test.js'
1919
import './extra.test.js'
2020
import './global.test.js'
2121
import './goods.test.js'
22+
import './index.test.js'
2223
import './package.test.js'
23-
import './win32.test.js'
2424
import './util.test.js'
25+
import './win32.test.js'

‎test/index.test.js

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import assert from 'node:assert'
16+
import { describe, test } from 'node:test'
17+
import {
18+
nothrow,
19+
quiet,
20+
$,
21+
log,
22+
cd,
23+
kill,
24+
ProcessOutput,
25+
ProcessPromise,
26+
defaults,
27+
minimist,
28+
chalk,
29+
fs,
30+
which,
31+
YAML,
32+
ssh,
33+
ps,
34+
quote,
35+
quotePowerShell,
36+
within,
37+
argv,
38+
os,
39+
updateArgv,
40+
globby,
41+
glob,
42+
sleep,
43+
fetch,
44+
echo,
45+
question,
46+
stdin,
47+
retry,
48+
expBackoff,
49+
spinner,
50+
path,
51+
} from '../build/index.js'
52+
53+
describe('index', () => {
54+
test('has proper exports', () => {
55+
// index
56+
assert(nothrow)
57+
assert(quiet)
58+
59+
// core
60+
assert($)
61+
assert(ProcessOutput)
62+
assert(ProcessPromise)
63+
assert(cd)
64+
assert(log)
65+
assert(kill)
66+
assert(defaults)
67+
assert(within)
68+
69+
// goods
70+
assert(argv)
71+
assert(os)
72+
assert(updateArgv)
73+
assert(globby)
74+
assert(glob)
75+
assert(sleep)
76+
assert(fetch)
77+
assert(echo)
78+
assert(question)
79+
assert(stdin)
80+
assert(retry)
81+
assert(expBackoff)
82+
assert(spinner)
83+
assert(path)
84+
85+
// vendor
86+
assert(minimist)
87+
assert(chalk)
88+
assert(fs)
89+
assert(which)
90+
assert(YAML)
91+
assert(ssh)
92+
assert(ps)
93+
94+
// utils
95+
assert(quote)
96+
assert(quotePowerShell)
97+
})
98+
})

0 commit comments

Comments
 (0)
Please sign in to comment.