Skip to content

Commit 612cabf

Browse files
committedJun 25, 2023
fix: allow to kill onSuccess process using SIGKILL signal, closes #936
1 parent c151d53 commit 612cabf

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed
 

‎src/cli-main.ts

+4
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ export async function main(options: Options = {}) {
9191
'Using Rollup for treeshaking instead, "recommended" or "smallest" or "safest"'
9292
)
9393
.option('--publicDir [dir]', 'Copy public directory to output directory')
94+
.option(
95+
'--killSignal <signal>',
96+
'Signal to kill child process, "SIGINT" or "SIGTERM"'
97+
)
9498
.action(async (files: string[], flags) => {
9599
const { build } = await import('.')
96100
Object.assign(options, {

‎src/index.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import execa from 'execa'
1111
import kill from 'tree-kill'
1212
import { version } from '../package.json'
1313
import { createLogger, setSilent } from './log'
14-
import { NormalizedOptions, Format, Options } from './options'
14+
import { NormalizedOptions, Format, Options, KILL_SIGNAL } from './options'
1515
import { runEsbuild } from './esbuild'
1616
import { shebang } from './plugins/shebang'
1717
import { cjsSplitting } from './plugins/cjs-splitting'
@@ -34,15 +34,12 @@ export const defineConfig = (
3434
) => MaybePromise<Options | Options[]>)
3535
) => options
3636

37-
const killProcess = ({
38-
pid,
39-
signal = 'SIGTERM',
40-
}: {
41-
pid: number
42-
signal?: string | number
43-
}) =>
44-
new Promise<unknown>((resolve) => {
45-
kill(pid, signal, resolve)
37+
const killProcess = ({ pid, signal }: { pid: number; signal: KILL_SIGNAL }) =>
38+
new Promise<void>((resolve, reject) => {
39+
kill(pid, signal, (err) => {
40+
if (err) return reject(err)
41+
resolve()
42+
})
4643
})
4744

4845
const normalizeOptions = async (
@@ -207,6 +204,7 @@ export async function build(_options: Options) {
207204
if (onSuccessProcess) {
208205
await killProcess({
209206
pid: onSuccessProcess.pid,
207+
signal: options.killSignal || 'SIGTERM',
210208
})
211209
} else if (onSuccessCleanup) {
212210
await onSuccessCleanup()

‎src/options.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import type { Plugin } from './plugin'
55
import type { TreeshakingStrategy } from './plugins/tree-shaking'
66
import type { MinifyOptions } from 'terser'
77

8+
export type KILL_SIGNAL = 'SIGKILL' | 'SIGTERM'
9+
810
export type Format = 'cjs' | 'esm' | 'iife'
911

1012
export type ContextForOutPathGeneration = {
@@ -14,7 +16,7 @@ export type ContextForOutPathGeneration = {
1416
pkgType?: string
1517
}
1618

17-
export type OutExtensionObject = { js?: string, dts?: string }
19+
export type OutExtensionObject = { js?: string; dts?: string }
1820

1921
export type OutExtensionFactory = (
2022
ctx: ContextForOutPathGeneration
@@ -221,6 +223,7 @@ export type Options = {
221223
* Copy the files inside `publicDir` to output directory
222224
*/
223225
publicDir?: string | boolean
226+
killSignal?: KILL_SIGNAL
224227
}
225228

226229
export type NormalizedOptions = Omit<

0 commit comments

Comments
 (0)
Please sign in to comment.