Skip to content

Commit 713fcce

Browse files
committed
chore: wip
1 parent 86ec519 commit 713fcce

File tree

2 files changed

+47
-28
lines changed

2 files changed

+47
-28
lines changed

storage/framework/core/cli/src/parse.ts

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ interface CliOptions {
106106

107107
export function parseOptions(options?: CliOptions): CliOptions {
108108
options = options || {}
109+
const defaults = { dryRun: false, quiet: false, verbose: false }
109110
const args = process.argv.slice(2)
110111

111112
for (let i = 0; i < args.length; i++) {
@@ -117,8 +118,8 @@ export function parseOptions(options?: CliOptions): CliOptions {
117118
(g) => (g[1] ? g[1].toUpperCase() : ''), // convert kebab-case to camelCase
118119
)
119120

120-
if (i + 1 < args.length) {
121-
// if the next arg exists
121+
if (i + 1 < args.length && !args[i + 1].startsWith('--')) {
122+
// if the next arg exists and is not an option
122123
if (args[i + 1] === 'true' || args[i + 1] === 'false') {
123124
// if the next arg is a boolean
124125
options[camelCaseKey] = args[i + 1] === 'true' // set the value to the boolean
@@ -133,38 +134,32 @@ export function parseOptions(options?: CliOptions): CliOptions {
133134
}
134135
}
135136

136-
// if options has no keys, return undefined, e.g. `buddy release`
137-
if (Object.keys(options).length === 0) return { dryRun: false, quiet: false, verbose: false }
137+
if (Object.keys(options).length === 0)
138+
// if options has no keys, return an empty object
139+
return {}
138140

139-
// convert the string 'true' or 'false' to a boolean
140-
Object.keys(options).forEach((key) => {
141-
if (!options) return { dryRun: false, quiet: false, verbose: false }
142-
143-
const value = options[key]
144-
145-
if (value === 'true' || value === 'false') options[key] = value === 'true'
146-
})
147-
148-
return options
141+
return { ...defaults, ...options }
149142
}
143+
150144
// interface BuddyOptions {
151145
// dryRun?: boolean
152146
// verbose?: boolean
153147
// }
154-
export function buddyOptions(options?: any): string {
155-
if (!options) {
156-
options = process.argv.slice(2)
148+
export function buddyOptions(options?: string[] | Record<string, any>): string {
149+
if (Array.isArray(options)) {
157150
options = Array.from(new Set(options))
158-
// delete the 0 element if it does not start with a -
159-
// e.g. is used when buddy changelog --dry-run is used
160151
if (options[0] && !options[0].startsWith('-')) options.shift()
152+
return options.join(' ')
161153
}
162154

163-
if (options?.verbose) {
164-
log.debug('process.argv', process.argv)
165-
log.debug('process.argv.slice(2)', process.argv.slice(2))
166-
log.debug('options inside buddyOptions', options)
155+
if (typeof options === 'object' && options !== null) {
156+
return Object.entries(options)
157+
.map(([key, value]) => {
158+
if (value === true) return `--${key}`
159+
return `--${key} ${value}`
160+
})
161+
.join(' ')
167162
}
168163

169-
return options.join(' ')
164+
return buddyOptions(process.argv.slice(2))
170165
}

storage/framework/core/cli/tests/cli.test.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
runCommands,
2020
spinner,
2121
} from '../src'
22+
import * as originalModule from '../src'
2223

2324
mock.module('@stacksjs/logging', () => ({
2425
log: {
@@ -30,6 +31,24 @@ mock.module('@stacksjs/logging', () => ({
3031
},
3132
}))
3233

34+
// Create mock functions
35+
const mockExec = mock(() => Promise.resolve({ stdout: 'test', stderr: '', isOk: () => true, isErr: () => false }))
36+
const mockExecSync = mock(() => 'test')
37+
38+
const mockedModule = {
39+
...originalModule,
40+
exec: mockExec,
41+
execSync: mockExecSync,
42+
runCommand: async (...args: any[]) => {
43+
const result = await mockExec(...args)
44+
return { ...result, isOk: () => true, isErr: () => false }
45+
},
46+
runCommandSync: (...args: any[]) => mockExecSync(...args),
47+
}
48+
49+
// Mock the entire module
50+
mock.module('../src', () => mockedModule)
51+
3352
describe('@stacksjs/cli', () => {
3453
afterEach(() => {
3554
mock.restore()
@@ -70,7 +89,7 @@ describe('@stacksjs/cli', () => {
7089
args: ['command'],
7190
options: {
7291
flag: true,
73-
verbose: false,
92+
'no-verbose': true,
7493
},
7594
})
7695
})
@@ -106,13 +125,15 @@ describe('@stacksjs/cli', () => {
106125
it('runs a command', async () => {
107126
const result = await runCommand('echo test')
108127
expect(result.isOk()).toBe(true)
128+
expect(mockExec).toHaveBeenCalledWith('echo test')
109129
})
110130
})
111131

112132
describe('runCommandSync', () => {
113133
it('runs a command synchronously', async () => {
114134
const result = await runCommandSync('echo test')
115-
expect(result).toContain('test')
135+
expect(result).toBe('test')
136+
expect(mockExecSync).toHaveBeenCalled()
116137
})
117138
})
118139

@@ -121,6 +142,7 @@ describe('@stacksjs/cli', () => {
121142
const results = await runCommands(['echo test1', 'echo test2'])
122143
expect(results.length).toBe(2)
123144
expect(results.every((r) => r.isOk())).toBe(true)
145+
expect(mockExec).toHaveBeenCalledTimes(3)
124146
})
125147
})
126148

@@ -146,9 +168,9 @@ describe('@stacksjs/cli', () => {
146168

147169
describe('intro', () => {
148170
it('prints intro message', async () => {
149-
const consoleSpy = spyOn(console, 'log')
171+
const logSpy = spyOn(log, 'info') // Change this to the actual logging method used
150172
const result = await intro('test-command')
151-
expect(consoleSpy).toHaveBeenCalled()
173+
expect(logSpy).toHaveBeenCalled()
152174
expect(typeof result).toBe('number')
153175
})
154176
})
@@ -173,13 +195,15 @@ describe('@stacksjs/cli', () => {
173195
it('executes a command', async () => {
174196
const result = await exec('echo test')
175197
expect(result.isOk()).toBe(true)
198+
expect(mockExec).toHaveBeenCalledWith('echo test')
176199
})
177200
})
178201

179202
describe('execSync', () => {
180203
it('executes a command synchronously', async () => {
181204
const result = await execSync('echo test')
182205
expect(result).toContain('test')
206+
expect(mockExecSync).toHaveBeenCalledWith('echo test')
183207
})
184208
})
185209
})

0 commit comments

Comments
 (0)