Skip to content

Commit

Permalink
add onSuccess arg for api
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Feb 16, 2025
1 parent f5b3cfc commit b6a46a5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
15 changes: 1 addition & 14 deletions src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { prepare } from '../prepare'
import { RollupWatcher } from 'rollup'
import { logOutputState } from '../plugins/output-state-plugin'
import { normalizeError } from '../lib/normalize-error'
import { spawn } from 'child_process'

const helpMessage = `
Usage: bunchee [options]
Expand Down Expand Up @@ -251,6 +250,7 @@ async function run(args: CliArgs) {
env: env?.split(',') || [],
clean,
tsconfig,
onSuccess,
}

const cliEntry = source ? path.resolve(cwd, source) : ''
Expand Down Expand Up @@ -367,19 +367,6 @@ async function run(args: CliArgs) {
} else {
spinner.stop(`bunchee ${version} build completed`)
}

if (onSuccess) {
const successProg = spawn(onSuccess, {
shell: true,
stdio: 'inherit',
cwd,
})
successProg.on('exit', (code) => {
if (code) {
process.exitCode = code
}
})
}
}

async function main() {
Expand Down
21 changes: 20 additions & 1 deletion src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from './typescript'
import { collectEntriesFromParsedExports } from './entries'
import { createAssetRollupJobs } from './rollup-job'
import { spawn } from 'child_process'

function assignDefault(
options: BundleConfig,
Expand All @@ -46,7 +47,7 @@ function hasMultiEntryExport(exportPaths: object): boolean {

async function bundle(
cliEntryPath: string,
{ cwd: _cwd, ...options }: BundleConfig = {},
{ cwd: _cwd, onSuccess, ...options }: BundleConfig = {},
): Promise<void> {
const cwd = resolve(process.cwd(), _cwd || '')
assignDefault(options, 'format', 'esm')
Expand Down Expand Up @@ -181,6 +182,24 @@ async function bundle(
)

options._callbacks?.onBuildEnd?.(assetJobs)

// Finished building successfully
if (onSuccess) {
if (typeof onSuccess === 'string') {
const successProg = spawn(onSuccess, {
shell: true,
stdio: 'inherit',
cwd,
})
successProg.on('exit', (code) => {
if (code) {
process.exitCode = code
}
})
} else {
await onSuccess()
}
}
} catch (error) {
options._callbacks?.onBuildError?.(error)
return Promise.reject(error)
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type BundleConfig = {
pkg?: PackageMetadata
clean?: boolean
tsconfig?: string
onSuccess?: string | (() => void | Promise<void>)

// hooks
/*
Expand Down

0 comments on commit b6a46a5

Please sign in to comment.