Skip to content

Commit

Permalink
refactor: pass unknown args to configure command
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Feb 18, 2024
1 parent 382fb93 commit 26dc2cb
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
10 changes: 9 additions & 1 deletion commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class Add extends BaseCommand {
@flags.string({ description: 'Select the package manager you want to use' })
declare packageManager?: 'npm' | 'pnpm' | 'yarn'

@flags.boolean({ description: 'Should we install the package as a dev dependency' })
@flags.boolean({ description: 'Should we install the package as a dev dependency', alias: 'D' })
declare dev?: boolean

@flags.boolean({ description: 'Forcefully overwrite existing files' })
Expand All @@ -56,10 +56,18 @@ export default class Add extends BaseCommand {
* Configure the package by delegating the work to the `node ace configure` command
*/
async #configurePackage() {
/**
* Sending unknown flags to the configure command
*/
const flagValueArray = this.parsed.unknownFlags
.filter((flag) => !!this.parsed.flags[flag])
.map((flag) => `--${flag}=${this.parsed.flags[flag]}`)

const configureArgs = [
this.name,
this.force ? '--force' : undefined,
this.verbose ? '--verbose' : undefined,
...flagValueArray,
].filter(Boolean) as string[]

return await this.kernel.exec('configure', configureArgs)
Expand Down
60 changes: 59 additions & 1 deletion tests/commands/add.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,65 @@ test.group('Install', (group) => {
await assert.fileContains('package.json', 'foo')
})

test('should install dev dependency', async ({ assert, fs }) => {
const ace = await new AceFactory().make(fs.baseUrl, {
importer: (filePath) => import(join(filePath, `index.js?${Math.random()}`)),
})

await setupProject(fs, 'npm')
await setupPackage(fs)

await ace.app.init()

ace.addLoader(new ListLoader([Configure]))
ace.ui.switchMode('raw')
ace.prompt.trap('install').accept()

const command = await ace.create(Add, [
join(fileURLToPath(fs.baseUrl), 'node_modules', 'foo'),
'-D',
])
await command.exec()

const pkgJson = await fs.contentsJson('package.json')
assert.deepEqual(pkgJson.devDependencies, { test: 'file:node_modules/foo' })
})

test('pass unknown args to configure', async ({ fs, assert }) => {
const ace = await new AceFactory().make(fs.baseUrl, {
importer: (filePath) => import(join(filePath, `index.js?${Math.random()}`)),
})

await setupProject(fs, 'npm')
await setupPackage(
fs,
`
command.logger.log(command.parsedFlags)
`
)

await ace.app.init()

ace.addLoader(new ListLoader([Configure]))
ace.ui.switchMode('raw')
ace.prompt.trap('install').accept()

const command = await ace.create(Add, [
join(fileURLToPath(fs.baseUrl), 'node_modules', 'foo'),
'--foo',
'--auth=session',
'-x',
])
await command.exec()

const logs = command.logger.getLogs()

assert.deepInclude(logs, {
message: { foo: 'true', auth: 'session', x: 'true' },
stream: 'stdout',
})
})

test('should configure package', async ({ assert, fs }) => {
const ace = await new AceFactory().make(fs.baseUrl, {
importer: (filePath) => import(join(filePath, `index.js?${Math.random()}`)),
Expand Down Expand Up @@ -239,7 +298,6 @@ test.group('Install', (group) => {

await ace.app.init()
ace.addLoader(new ListLoader([Configure]))
// ace.ui.switchMode('raw')
ace.prompt.trap('install').accept()

const command = await ace.create(Add, ['vinejs'])
Expand Down

0 comments on commit 26dc2cb

Please sign in to comment.