Skip to content

Commit

Permalink
chore: code review
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Feb 13, 2024
1 parent 2d1e018 commit f825dc5
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 21 deletions.
14 changes: 3 additions & 11 deletions src/config/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,10 @@ function determineCommandDiscoveryOptions(
return {globPatterns: GLOB_PATTERNS, strategy: 'pattern', target: commandDiscovery}
}

if (commandDiscovery.strategy === 'explicit') {
if (!commandDiscovery.target) throw new CLIError('`oclif.commandDiscovery.target` is required.')
return commandDiscovery
}

if (commandDiscovery.strategy === 'pattern') {
if (!commandDiscovery.target) {
throw new CLIError('`oclif.commandDiscovery.target` is required.')
}
if (!commandDiscovery.target) throw new CLIError('`oclif.commandDiscovery.target` is required.')
if (!commandDiscovery.strategy) throw new CLIError('`oclif.commandDiscovery.strategy` is required.')

return commandDiscovery
}
return commandDiscovery
}

type CommandExportModule = {
Expand Down
10 changes: 8 additions & 2 deletions test/command/explicit-command-strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ USAGE
$ oclif foo COMMAND
COMMANDS
foo bar foo bar description
foo baz foo baz description
foo alias foo bar description
foo bar foo bar description
foo baz foo baz description
`)
})
Expand All @@ -36,4 +37,9 @@ COMMANDS
await run(['foo:bar'], resolve(__dirname, 'fixtures/explicit-commands/package.json'))
expect(stdoutStub.firstCall.firstArg).to.equal('hello world!\n')
})

it('should run alias', async () => {
await run(['foo:alias'], resolve(__dirname, 'fixtures/explicit-commands/package.json'))
expect(stdoutStub.firstCall.firstArg).to.equal('hello world!\n')
})
})
4 changes: 0 additions & 4 deletions test/command/fixtures/explicit-commands/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,5 @@
}
}
}
},
"devDependencies": {
"globby": "^8.0.1",
"ts-node": "^6.0.2"
}
}
1 change: 1 addition & 0 deletions test/command/fixtures/explicit-commands/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import FooBaz from './commands/foo/baz'
export default {
'foo:bar': FooBar,
'foo:baz': FooBaz,
'foo:alias': FooBar,
}
10 changes: 10 additions & 0 deletions test/command/fixtures/single-cmd-cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "single-cmd-cli",
"version": "0.0.0",
"description": "Single Command CLI",
"private": true,
"oclif": {
"default": ".",
"commands": "./dist"
}
}
9 changes: 9 additions & 0 deletions test/command/fixtures/single-cmd-cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// eslint-disable-next-line node/no-unpublished-import
import {Command} from '../../../../../src/index'

export default class FooBar extends Command {
public static description = 'Description of single command CLI.'
public async run(): Promise<void> {
this.log('hello world!')
}
}
7 changes: 7 additions & 0 deletions test/command/fixtures/single-cmd-cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDirs": ["./src"]
},
"include": ["./src/**/*"]
}
4 changes: 0 additions & 4 deletions test/command/fixtures/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,5 @@
}
}
}
},
"devDependencies": {
"globby": "^8.0.1",
"ts-node": "^6.0.2"
}
}
38 changes: 38 additions & 0 deletions test/command/single-command-cli.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {expect} from 'chai'
import {resolve} from 'node:path'
import {SinonSandbox, SinonStub, createSandbox} from 'sinon'
import stripAnsi from 'strip-ansi'

import {run, ux} from '../../src/index'

describe('single command cli', () => {
let sandbox: SinonSandbox
let stdoutStub: SinonStub

beforeEach(() => {
sandbox = createSandbox()
stdoutStub = sandbox.stub(ux.write, 'stdout')
})

afterEach(() => {
sandbox.restore()
})

it('should show help for commands', async () => {
await run(['--help'], resolve(__dirname, 'fixtures/single-cmd-cli/package.json'))
expect(stdoutStub.args.map((a) => stripAnsi(a[0])).join('')).to.equal(`Description of single command CLI.
USAGE
$ single-cmd-cli .
DESCRIPTION
Description of single command CLI.
`)
})

it('should run command', async () => {
await run([], resolve(__dirname, 'fixtures/single-cmd-cli/package.json'))
expect(stdoutStub.firstCall.firstArg).to.equal('hello world!\n')
})
})

0 comments on commit f825dc5

Please sign in to comment.