Skip to content

Commit

Permalink
fix: support for bun (#913)
Browse files Browse the repository at this point in the history
* fix: support for bun

* chore: update docs with bun example
  • Loading branch information
stijnvanhulle authored Apr 1, 2024
1 parent 89d865a commit 5003315
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 18 deletions.
6 changes: 6 additions & 0 deletions .changeset/little-walls-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@kubb/core": patch
"@kubb/cli": patch
---

full support for Bun v1.1.0
10 changes: 9 additions & 1 deletion docs/guide/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@ yarn add @kubb/cli

## Usage

```sh
::: code-group

```shell [bun <img src="/feature/bun.svg"/>]
kubb --bun --config kubb.config.js
bkubb --config kubb.config.js
```
```sh [node]
kubb --config kubb.config.js
```
:::

```mdx
kubb/2.0.0
Expand Down Expand Up @@ -73,6 +80,7 @@ Options:
-l, --log-level <type> Info, silent or debug
-w, --watch Watch mode based on the input file
-h, --help Display this message
-b, --bun Run Kubb with Bun
```

Path of the input file(overrides the one in `kubb.config.js`)
Expand Down
10 changes: 9 additions & 1 deletion docs/plugins/cli/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@ yarn add @kubb/cli

## Usage

```sh
::: code-group

```shell [bun <img src="/feature/bun.svg"/>]
kubb --bun --config kubb.config.js
bkubb --config kubb.config.js
```
```sh [node]
kubb --config kubb.config.js
```
:::

```mdx
kubb/1.2.1
Expand Down Expand Up @@ -73,6 +80,7 @@ Options:
-l, --log-level <type> Info, silent or debug
-w, --watch Watch mode based on the input file
-h, --help Display this message
-b, --bun Run Kubb with Bun
```

Path of the input file(overrides the one in `kubb.config.js`)
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"build": "tsup",
"clean": "npx rimraf ./dist",
"generate": "kubb generate",
"generate:bun": "bun ./node_modules/@kubb/cli/bin/kubb.js generate --config .kubbrc.js",
"generate:bun": "kubb generate --bun",
"generate:js": "kubb generate --config .kubbrc.js",
"generate:json": "kubb generate --config kubb.json",
"generate:ts": "kubb generate --config configs/kubb.config.ts",
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/bin/bkubb.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bun

import('../dist/index.js').then(({ run }) => {
process.title = 'Kubb'
run(process.argv)
})
10 changes: 8 additions & 2 deletions packages/cli/bin/kubb.cjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#!/usr/bin/env node
try {
const cachedSourceMaps = new Map()

require('source-map-support').install({
environment: 'node',
handleUncaughtExceptions: false,
retrieveSourceMap(source) {
if (cachedSourceMaps.has(source)) {
return cachedSourceMaps.get(source)
}
return null
},
})
} catch (err) {}

Expand Down
4 changes: 3 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
}
},
"bin": {
"kubb": "bin/kubb.cjs"
"kubb": "bin/kubb.cjs",
"bkubb": "bin/bkubb.cjs"
},
"files": ["src", "dist", "bin", "!/**/**.test.**", "!/**/__tests__/**"],
"scripts": {
Expand All @@ -40,6 +41,7 @@
"dependencies": {
"@kubb/core": "workspace:*",
"bundle-require": "^4.0.2",
"js-runtime": "^0.0.7",
"cac": "^6.7.14",
"chokidar": "^3.6.0",
"cosmiconfig": "^9.0.0",
Expand Down
16 changes: 5 additions & 11 deletions packages/cli/src/generate.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { safeBuild } from '@kubb/core'
import { LogLevel, createLogger, randomCliColour } from '@kubb/core/logger'

import { execa } from 'execa'
import { get } from 'js-runtime'
import { parseArgsStringToArgv } from 'string-argv'
import c from 'tinyrainbow'

import { OraWritable } from './utils/OraWritable.ts'
import { getSummary } from './utils/getSummary.ts'
import { spinner } from './utils/spinner.ts'

import type { Writable } from 'node:stream'
import type { CLIOptions, Config } from '@kubb/core'
import type { ExecaReturnValue } from 'execa'
import { type CLIOptions, type Config, safeBuild } from '@kubb/core'
import { getSummary } from './utils/getSummary.ts'

type GenerateProps = {
input?: string
Expand All @@ -24,11 +23,6 @@ type ExecutingHooksProps = {
logLevel: LogLevel
}

type Executer = {
subProcess: ExecaReturnValue<string>
abort: AbortController['abort']
}

async function executeHooks({ hooks, logLevel }: ExecutingHooksProps): Promise<void> {
if (!hooks?.done) {
return
Expand Down Expand Up @@ -109,7 +103,7 @@ export async function generate({ input, config, CLIOptions }: GenerateProps): Pr
const logLevel = logger.logLevel
const inputPath = input ?? ('path' in userConfig.input ? userConfig.input.path : undefined)

spinner.start(`🚀 Building ${logLevel !== 'silent' ? c.dim(inputPath) : ''}`)
spinner.start(`🚀 Building with ${get()} ${logLevel !== 'silent' ? c.dim(inputPath) : ''}`)

const definedConfig: Config = {
root: process.cwd(),
Expand Down Expand Up @@ -150,7 +144,7 @@ export async function generate({ input, config, CLIOptions }: GenerateProps): Pr
await executeHooks({ hooks: config.hooks, logLevel })

spinner.suffixText = ''
spinner.succeed(`🚀 Build completed ${logLevel !== 'silent' ? c.dim(inputPath) : ''}`)
spinner.succeed(`🚀 Build completed with ${get()} ${logLevel !== 'silent' ? c.dim(inputPath) : ''}`)

console.log(summary.join(''))
}
11 changes: 11 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { spinner } from './utils/spinner.ts'
import { startWatcher } from './utils/watcher.ts'

import type { CLIOptions } from '@kubb/core'
import { execa } from 'execa'
import { OraWritable } from './utils/OraWritable.ts'

const moduleName = 'kubb'

Expand All @@ -31,6 +33,13 @@ function programCatcher(e: unknown, CLIOptions: CLIOptions): void {
}

async function generateAction(input: string, CLIOptions: CLIOptions) {
if (CLIOptions.bun) {
const command = process.argv.splice(2).filter((item) => item !== '--bun')

await execa('bkubb', command, { stdout: process.stdout, stderr: process.stderr })
return
}

spinner.start('🔍 Loading config')
const result = await getCosmiConfig(moduleName, CLIOptions.config)
spinner.succeed(`🔍 Config loaded(${c.dim(path.relative(process.cwd(), result.filepath))})`)
Expand Down Expand Up @@ -71,13 +80,15 @@ export async function run(argv?: string[]): Promise<void> {
.option('-c, --config <path>', 'Path to the Kubb config')
.option('-l, --log-level <type>', 'Info, silent or debug')
.option('-w, --watch', 'Watch mode based on the input file')
.option('-b, --bun', 'Run Kubb with Bun')
.action(generateAction)

program
.command('generate [input]', 'Path of the input file(overrides the one in `kubb.config.js`)')
.option('-c, --config <path>', 'Path to the Kubb config')
.option('-l, --log-level <type>', 'Info, silent or debug')
.option('-w, --watch', 'Watch mode based on the input file')
.option('-b, --bun', 'Run Kubb with Bun')
.action(generateAction)

program.command('init', 'Init Kubb').action(async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/utils/getSummary.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path'

import { LogLevel, randomCliColour } from '@kubb/core/logger'
import { randomCliColour } from '@kubb/core/logger'

import c from 'tinyrainbow'

Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ export type CLIOptions = {
* @default `silent`
*/
logLevel?: LogLevel
/**
* Run Kubb with Bun
*/
bun?: boolean
}

// plugin
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5003315

Please sign in to comment.