Skip to content

Commit

Permalink
feat: --success arg (#659)
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi authored Feb 16, 2025
1 parent 53d1011 commit 406d9ac
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 2 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ This convention keeps shared modules private while enabling efficient bundling a
### CLI
#### CLI Options
#### Options
`bunchee` CLI provides few options to create different bundles or generating types. Call `bunchee --help` to see the help information in the terminal.
Expand Down Expand Up @@ -317,6 +317,14 @@ bunchee --no-external
This will include all dependencies within your output bundle.
#### Build Successful Command
A command to be executed after a build is successful can be specified using the `--success` option, which is useful for development watching mode:
```sh
bunchee --watch --success "node ./dist/index.js"
```
#### Prepare Package
```sh
Expand Down
8 changes: 8 additions & 0 deletions src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Options:
--no-dts do not generate types, default: undefined
--tsconfig path to tsconfig file, default: tsconfig.json
--dts-bundle bundle type declaration files, default: false
--success <cmd> run command after build success
`

function help() {
Expand Down Expand Up @@ -135,6 +136,10 @@ async function parseCliArgs(argv: string[]) {
type: 'boolean',
description: 'auto setup package.json for building',
})
.option('success', {
type: 'string',
description: 'run command after build success',
})
.command(
'prepare',
'auto configure package.json exports for building',
Expand Down Expand Up @@ -193,6 +198,7 @@ async function parseCliArgs(argv: string[]) {
clean: args['clean'] !== false,
env: args['env'],
tsconfig: args['tsconfig'],
onSuccess: args['success'],
}

// When minify is enabled, sourcemap should be enabled by default, unless explicitly opted out
Expand Down Expand Up @@ -222,6 +228,7 @@ async function run(args: CliArgs) {
env,
clean,
tsconfig,
onSuccess,
} = args
const cwd = args.cwd || process.cwd()
const file = args.file ? path.resolve(cwd, args.file) : undefined
Expand All @@ -243,6 +250,7 @@ async function run(args: CliArgs) {
env: env?.split(',') || [],
clean,
tsconfig,
onSuccess,
}

const cliEntry = source ? path.resolve(cwd, source) : ''
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
2 changes: 2 additions & 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 Expand Up @@ -107,6 +108,7 @@ type CliArgs = {
prepare?: boolean
clean?: boolean
tsconfig?: string
onSuccess?: string
}

type BundleOptions = BundleConfig
Expand Down
15 changes: 15 additions & 0 deletions test/integration/success-cmd/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { describe, expect, it } from 'vitest'
import { createJob } from '../../testing-utils'

describe('integration - success arg', () => {
const { job } = createJob({
directory: __dirname,
args: ['--success', 'node dist/index.js'],
})

it('should work', async () => {
console.log(job.stdout + job.stderr)
expect(job.code).toBe(0)
expect(job.stdout).toContain('Log from --success')
})
})
5 changes: 5 additions & 0 deletions test/integration/success-cmd/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "on-success",
"main": "./dist/index.js",
"type": "module"
}
5 changes: 5 additions & 0 deletions test/integration/success-cmd/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function main() {
console.log('Log from --success')
}

main()

0 comments on commit 406d9ac

Please sign in to comment.