Skip to content

Commit 64deb61

Browse files
committedJul 30, 2022
fix: return a function in onSuccess to do cleanup
1 parent b595f72 commit 64deb61

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed
 

‎docs/README.md

+23-2
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,34 @@ tsup src/index.ts --watch --onSuccess "node dist/index.js"
331331
332332
> Warning: You should not use shell scripts, if you need to specify shell scripts you can add it in your "scripts" field and set for example `tsup src/index.ts --watch --onSuccess \"npm run dev\"`
333333

334-
`onSuccess` can also be a `function` that returns `Promise`. For this to work, you need to use `tsup.config.ts` instead of the cli flag:
334+
`onSuccess` can also be a `function` that returns `Promise`. For this to work, you need to use `tsup.config.ts` instead of the cli flag:
335335

336336
```ts
337337
import { defineConfig } from 'tsup'
338338
339339
export default defineConfig({
340-
onSuccess: async () => { ... }
340+
async onSuccess() {
341+
// Start some long running task
342+
// Like a server
343+
},
344+
})
345+
```
346+
347+
You can return a cleanup function in `onSuccess`:
348+
349+
```ts
350+
import { defineConfig } from 'tsup'
351+
352+
export default defineConfig({
353+
async onSuccess() {
354+
const server = http.createServer((req, res) => {
355+
res.end('Hello World!')
356+
})
357+
server.listen(3000)
358+
return () => {
359+
server.close()
360+
}
361+
},
341362
})
342363
```
343364

‎src/index.ts

+15-20
Original file line numberDiff line numberDiff line change
@@ -179,29 +179,24 @@ export async function build(_options: Options) {
179179
}
180180
}
181181

182-
const otherTasks = async () => {
182+
const mainTasks = async () => {
183183
if (!options.dts?.only) {
184-
let existingOnSuccess: ChildProcess | undefined
185-
let existingOnSuccessFnPromise: Promise<any> | undefined
184+
let onSuccessProcess: ChildProcess | undefined
185+
let onSuccessCleanup: (() => any) | undefined | void
186186
/** Files imported by the entry */
187187
const buildDependencies: Set<string> = new Set()
188188

189-
const killPreviousProcessOrPromise = async () => {
190-
if (existingOnSuccess) {
189+
const doOnSuccessCleanup = async () => {
190+
if (onSuccessProcess) {
191191
await killProcess({
192-
pid: existingOnSuccess.pid,
192+
pid: onSuccessProcess.pid,
193193
})
194-
} else if (existingOnSuccessFnPromise) {
195-
await Promise.race([
196-
existingOnSuccessFnPromise,
197-
// cancel existingOnSuccessFnPromise if it is still running,
198-
// using a promise that's been already resolved
199-
Promise.resolve(),
200-
])
194+
} else if (onSuccessCleanup) {
195+
await onSuccessCleanup()
201196
}
202197
// reset them in all occassions anyway
203-
existingOnSuccess = undefined
204-
existingOnSuccessFnPromise = undefined
198+
onSuccessProcess = undefined
199+
onSuccessCleanup = undefined
205200
}
206201

207202
const debouncedBuildAll = debouncePromise(
@@ -213,7 +208,7 @@ export async function build(_options: Options) {
213208
)
214209

215210
const buildAll = async () => {
216-
const killPromise = killPreviousProcessOrPromise()
211+
await doOnSuccessCleanup()
217212
// Store previous build dependencies in case the build failed
218213
// So we can restore it
219214
const previousBuildDependencies = new Set(buildDependencies)
@@ -258,12 +253,12 @@ export async function build(_options: Options) {
258253
})
259254
}),
260255
])
261-
await killPromise
256+
262257
if (options.onSuccess) {
263258
if (typeof options.onSuccess === 'function') {
264-
existingOnSuccessFnPromise = options.onSuccess()
259+
onSuccessCleanup = await options.onSuccess()
265260
} else {
266-
existingOnSuccess = execa(options.onSuccess, {
261+
onSuccessProcess = execa(options.onSuccess, {
267262
shell: true,
268263
stdio: 'inherit',
269264
})
@@ -338,7 +333,7 @@ export async function build(_options: Options) {
338333
}
339334
}
340335

341-
await Promise.all([dtsTask(), otherTasks()])
336+
await Promise.all([dtsTask(), mainTasks()])
342337
}
343338
)
344339
)

‎src/options.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ export type Options = {
7272
keepNames?: boolean
7373
watch?: boolean | string | (string | boolean)[]
7474
ignoreWatch?: string[] | string
75-
onSuccess?: string | ((...params: any[]) => Promise<any>),
75+
onSuccess?:
76+
| string
77+
| (() => Promise<void | undefined | (() => void | Promise<void>)>)
7678
jsxFactory?: string
7779
jsxFragment?: string
7880
outDir?: string

0 commit comments

Comments
 (0)
Please sign in to comment.