Skip to content

Commit b943ceb

Browse files
committed
feat: support on browsers
1 parent 8478fb4 commit b943ceb

File tree

6 files changed

+36
-18
lines changed

6 files changed

+36
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"url": "https://github.com/cacjs/cac/issues"
1616
},
1717
"exports": {
18-
".": "./dist/index.mjs",
18+
".": "./dist/index.js",
1919
"./package.json": "./package.json"
2020
},
2121
"files": [

src/cac.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
type CommandExample,
77
type HelpCallback,
88
} from './command.ts'
9-
import { processArgs } from './node.ts'
9+
import { runtimeProcessArgs } from './runtime.ts'
1010
import {
1111
camelcaseOptionName,
1212
getFileName,
@@ -172,14 +172,23 @@ export class CAC extends EventTarget {
172172
* Parse argv
173173
*/
174174
parse(
175-
argv: string[] = processArgs,
175+
argv?: string[],
176176
{
177177
run = true,
178178
}: {
179179
/** Whether to run the action for matched command */
180180
run?: boolean | undefined
181181
} = {},
182182
): ParsedArgv {
183+
if (!argv) {
184+
if (!runtimeProcessArgs) {
185+
throw new Error(
186+
'No argv provided and runtime process argv is not available.',
187+
)
188+
}
189+
argv = runtimeProcessArgs
190+
}
191+
183192
this.rawArgs = argv
184193
if (!this.name) {
185194
this.name = argv[1] ? getFileName(argv[1]) : 'cli'

src/command.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { platformInfo } from './node.ts'
1+
import { runtimeInfo } from './runtime.ts'
22
import { Option, type OptionConfig } from './option.ts'
33
import {
44
CACError,
@@ -251,7 +251,7 @@ export class Command {
251251
const { name } = this.cli
252252
const { versionNumber } = this.cli.globalCommand
253253
if (versionNumber) {
254-
console.info(`${name}/${versionNumber} ${platformInfo}`)
254+
console.info(`${name}/${versionNumber} ${runtimeInfo}`)
255255
}
256256
}
257257

src/node.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/runtime.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* eslint-disable import/no-mutable-exports */
2+
export let runtimeProcessArgs: string[] | undefined
3+
export let runtimeInfo: string
4+
5+
if (typeof process !== 'undefined') {
6+
let runtimeName: string
7+
if (typeof Deno !== 'undefined' && typeof Deno.version?.deno === 'string') {
8+
runtimeName = 'deno'
9+
} else if (typeof Bun !== 'undefined' && typeof Bun.version === 'string') {
10+
runtimeName = 'bun'
11+
} else {
12+
runtimeName = 'node'
13+
}
14+
runtimeInfo = `${process.platform}-${process.arch} ${runtimeName}-${process.version}`
15+
runtimeProcessArgs = process.argv
16+
} else if (typeof navigator === 'undefined') {
17+
runtimeInfo = `unknown`
18+
} else {
19+
runtimeInfo = `${navigator.platform} ${navigator.userAgent}`
20+
}

tsdown.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { nodeLib } from 'tsdown-preset-sxzz'
1+
import { lib } from 'tsdown-preset-sxzz'
22

3-
export default nodeLib(
3+
export default lib(
44
{
55
inlineDeps: ['mri'],
66
},

0 commit comments

Comments
 (0)