Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: respect onSuccess from config file #780

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 44 additions & 32 deletions src/index.ts
Expand Up @@ -141,8 +141,11 @@ export async function build(_options: Options) {
? await config.data(_options)
: config.data

const allTasks = [...(Array.isArray(configData) ? configData : [configData])]
let completedTasks = 0

await Promise.all(
[...(Array.isArray(configData) ? configData : [configData])].map(
allTasks.map(
async (item) => {
const logger = createLogger(item?.name)
const options = await normalizeOptions(logger, item, _options)
Expand Down Expand Up @@ -186,28 +189,46 @@ export async function build(_options: Options) {
}
}

let onSuccessProcess: ChildProcess | undefined
let onSuccessCleanup: (() => any) | undefined | void

const doOnSuccessCleanup = async () => {
if (onSuccessProcess) {
await killProcess({
pid: onSuccessProcess.pid,
})
} else if (onSuccessCleanup) {
await onSuccessCleanup()
}
// reset them in all occasions anyway
onSuccessProcess = undefined
onSuccessCleanup = undefined
}

const handleOnSuccess = async (onSuccess: Options['onSuccess']) => {
if (!onSuccess) return
else if (typeof onSuccess === 'function') {
onSuccessCleanup = await onSuccess()
} else {
onSuccessProcess = execa(onSuccess, {
shell: true,
stdio: 'inherit',
})
onSuccessProcess.on('exit', (code) => {
if (code && code !== 0) {
process.exitCode = code
}
})
}
}

const mainTasks = async () => {
if (!options.dts?.only) {
let onSuccessProcess: ChildProcess | undefined
let onSuccessCleanup: (() => any) | undefined | void
/** Files imported by the entry */
const buildDependencies: Set<string> = new Set()

let depsHash = await getAllDepsHash(process.cwd())

const doOnSuccessCleanup = async () => {
if (onSuccessProcess) {
await killProcess({
pid: onSuccessProcess.pid,
})
} else if (onSuccessCleanup) {
await onSuccessCleanup()
}
// reset them in all occasions anyway
onSuccessProcess = undefined
onSuccessCleanup = undefined
}

const debouncedBuildAll = debouncePromise(
() => {
return buildAll()
Expand Down Expand Up @@ -264,21 +285,7 @@ export async function build(_options: Options) {
}),
])

if (options.onSuccess) {
if (typeof options.onSuccess === 'function') {
onSuccessCleanup = await options.onSuccess()
} else {
onSuccessProcess = execa(options.onSuccess, {
shell: true,
stdio: 'inherit',
})
onSuccessProcess.on('exit', (code) => {
if (code && code !== 0) {
process.exitCode = code
}
})
}
}
await handleOnSuccess(item?.onSuccess)
}

const startWatcher = async () => {
Expand Down Expand Up @@ -372,7 +379,12 @@ export async function build(_options: Options) {
}
}

await Promise.all([dtsTask(), mainTasks()])
await Promise.all([dtsTask(), mainTasks()]).then(() => ++completedTasks)

if (options.onSuccess && completedTasks === allTasks.length) {
await doOnSuccessCleanup()
await handleOnSuccess(options.onSuccess)
}
}
)
)
Expand Down