Skip to content

Commit 9a16a48

Browse files
committed
chore: wip
1 parent bc9c34a commit 9a16a48

File tree

4 files changed

+103
-6
lines changed

4 files changed

+103
-6
lines changed

.stacks/core/actions/src/deploy/domains.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { parseArgs } from '@stacksjs/cli'
1+
import { parseOptions } from '@stacksjs/cli'
22

3-
const options = parseArgs(process.argv.slice(2))
3+
const options = parseOptions()
44

55
console.log('options', options)
66

.stacks/core/buddy/src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Action } from '@stacksjs/types'
77
import { filesystem } from '@stacksjs/storage'
88
import { build, changelog, clean, commit, create, dev, example, fresh, generate, key, lint, make, migrate, preinstall, prepublish, release, seed, setup, test, upgrade, version } from './commands'
99

10-
const cli = command('stacks')
10+
const cli = command('buddy')
1111
const { fs } = filesystem
1212

1313
// setup global error handlers

.stacks/core/cli/src/parse.ts

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,99 @@
1-
import mri from 'mri'
1+
interface ParsedArgv {
2+
args: ReadonlyArray<string>
3+
options: {
4+
[k: string]: string | boolean | number
5+
}
6+
}
27

3-
export const parseArgs = (argv: string[]) => mri(argv)
8+
function isLongOption(arg: string): boolean {
9+
return arg.startsWith('--')
10+
}
11+
12+
function isShortOption(arg: string): boolean {
13+
return arg.startsWith('-') && !isLongOption(arg)
14+
}
15+
16+
function parseValue(value: string): string | boolean | number {
17+
if (value === 'true')
18+
return true
19+
20+
if (value === 'false')
21+
return false
22+
23+
const numberValue = parseFloat(value)
24+
if (!isNaN(numberValue))
25+
return numberValue
26+
27+
return value.replace(/"/g, '')
28+
}
29+
30+
function parseLongOption(arg: string, argv: ReadonlyArray<string>, index: number, options: { [k: string]: string | boolean | number }): number {
31+
const [key, value] = arg.slice(2).split('=')
32+
if (value !== undefined) {
33+
options[key] = parseValue(value)
34+
}
35+
else if (index + 1 < argv.length && !argv[index + 1].startsWith('-')) {
36+
options[key] = argv[index + 1]
37+
index++
38+
}
39+
else {
40+
options[key] = true
41+
}
42+
return index
43+
}
44+
45+
function parseShortOption(arg: string, argv: ReadonlyArray<string>, index: number, options: { [k: string]: string | boolean | number }): number {
46+
const [key, value] = arg.slice(1).split('=')
47+
48+
if (value !== undefined) {
49+
for (let j = 0; j < key.length; j++)
50+
options[key[j]] = parseValue(value)
51+
}
52+
else {
53+
for (let j = 0; j < key.length; j++) {
54+
if (index + 1 < argv.length && j === key.length - 1 && !argv[index + 1].startsWith('-')) {
55+
options[key[j]] = parseValue(argv[index + 1])
56+
index++
57+
}
58+
else {
59+
options[key[j]] = true
60+
}
61+
}
62+
}
63+
64+
return index
65+
}
66+
67+
export function parseArgv(argv?: ReadonlyArray<string>): ParsedArgv {
68+
if (argv === undefined)
69+
argv = process.argv.slice(2)
70+
71+
const args: string[] = []
72+
const options: { [k: string]: string | boolean | number } = {}
73+
74+
for (let i = 0; i < argv.length; i++) {
75+
const arg = argv[i]
76+
if (isLongOption(arg))
77+
i = parseLongOption(arg, argv, i, options)
78+
else if (isShortOption(arg))
79+
i = parseShortOption(arg, argv, i, options)
80+
else
81+
args.push(arg)
82+
}
83+
84+
return { args, options }
85+
}
86+
87+
export function parseOptions(argv?: ReadonlyArray<string>): { [k: string]: string | boolean | number } {
88+
if (argv === undefined)
89+
argv = process.argv.slice(2)
90+
91+
return parseArgv(argv).options
92+
}
93+
94+
export function parseArgs(argv?: ReadonlyArray<string>): ReadonlyArray<string> {
95+
if (argv === undefined)
96+
argv = process.argv.slice(2)
97+
98+
return parseArgv(argv).args
99+
}

.stacks/core/cli/src/run.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { execSync as childExec } from 'node:child_process'
22
import type { CliOptions, CommandResult, SpinnerOptions as Spinner } from '@stacksjs/types'
33
import { projectPath } from '@stacksjs/path'
44
import { ResultAsync, err } from '@stacksjs/error-handling'
5+
import { log } from '@stacksjs/cli'
56
import { determineDebugLevel } from '@stacksjs/utils'
67
import { spawn } from './command'
78
import { startSpinner } from './helpers'
@@ -23,7 +24,7 @@ export function exec(command: string, options?: CliOptions): CommandResult {
2324
return ResultAsync.fromPromise(
2425
spawn(command, { stdio, cwd, shell }),
2526
// () => new Error(`Failed to run command: ${italic(command)}`),
26-
() => new Error(`Failed to run command: ${italic(command)}`),
27+
() => log.error(new Error(`Failed to run command: ${italic(command)}`)),
2728
)
2829
}
2930

0 commit comments

Comments
 (0)