Skip to content

Commit

Permalink
fix: allow to kill onSuccess process using SIGKILL signal, closes #936
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Jun 25, 2023
1 parent c151d53 commit 612cabf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/cli-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ export async function main(options: Options = {}) {
'Using Rollup for treeshaking instead, "recommended" or "smallest" or "safest"'
)
.option('--publicDir [dir]', 'Copy public directory to output directory')
.option(
'--killSignal <signal>',
'Signal to kill child process, "SIGINT" or "SIGTERM"'
)
.action(async (files: string[], flags) => {
const { build } = await import('.')
Object.assign(options, {
Expand Down
18 changes: 8 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import execa from 'execa'
import kill from 'tree-kill'
import { version } from '../package.json'
import { createLogger, setSilent } from './log'
import { NormalizedOptions, Format, Options } from './options'
import { NormalizedOptions, Format, Options, KILL_SIGNAL } from './options'
import { runEsbuild } from './esbuild'
import { shebang } from './plugins/shebang'
import { cjsSplitting } from './plugins/cjs-splitting'
Expand All @@ -34,15 +34,12 @@ export const defineConfig = (
) => MaybePromise<Options | Options[]>)
) => options

const killProcess = ({
pid,
signal = 'SIGTERM',
}: {
pid: number
signal?: string | number
}) =>
new Promise<unknown>((resolve) => {
kill(pid, signal, resolve)
const killProcess = ({ pid, signal }: { pid: number; signal: KILL_SIGNAL }) =>
new Promise<void>((resolve, reject) => {
kill(pid, signal, (err) => {
if (err) return reject(err)
resolve()
})
})

const normalizeOptions = async (
Expand Down Expand Up @@ -207,6 +204,7 @@ export async function build(_options: Options) {
if (onSuccessProcess) {
await killProcess({
pid: onSuccessProcess.pid,
signal: options.killSignal || 'SIGTERM',
})
} else if (onSuccessCleanup) {
await onSuccessCleanup()
Expand Down
5 changes: 4 additions & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type { Plugin } from './plugin'
import type { TreeshakingStrategy } from './plugins/tree-shaking'
import type { MinifyOptions } from 'terser'

export type KILL_SIGNAL = 'SIGKILL' | 'SIGTERM'

export type Format = 'cjs' | 'esm' | 'iife'

export type ContextForOutPathGeneration = {
Expand All @@ -14,7 +16,7 @@ export type ContextForOutPathGeneration = {
pkgType?: string
}

export type OutExtensionObject = { js?: string, dts?: string }
export type OutExtensionObject = { js?: string; dts?: string }

export type OutExtensionFactory = (
ctx: ContextForOutPathGeneration
Expand Down Expand Up @@ -221,6 +223,7 @@ export type Options = {
* Copy the files inside `publicDir` to output directory
*/
publicDir?: string | boolean
killSignal?: KILL_SIGNAL
}

export type NormalizedOptions = Omit<
Expand Down

0 comments on commit 612cabf

Please sign in to comment.