Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: show aliasOf if applicable #545

Merged
merged 3 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
/package-lock.json
/tmp
node_modules
/test/**/yarn.lock
/test/**/node_modules

oclif.lock
oclif.manifest.json
oclif.manifest.json
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
"@commitlint/config-conventional": "^17.8.1",
"@oclif/plugin-help": "^5.2.20",
"@oclif/prettier-config": "^0.2.1",
"@oclif/test": "^3.1.14",
"@types/chai": "^4.3.11",
"@types/mocha": "^10.0.6",
"@types/node": "^18",
"@types/sinon": "^17.0.3",
"chai": "^4.4.1",
"commitlint": "^17.8.1",
"eslint": "^8.56.0",
Expand All @@ -29,13 +29,14 @@
"oclif": "^4.4.3",
"prettier": "^3.2.5",
"shx": "^0.3.4",
"sinon": "^17.0.1",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
},
"engines": {
"node": ">=18.0.0"
},
"exports": "././lib/index.js",
"exports": "./lib/index.js",
"files": [
"/lib",
"/oclif.manifest.json",
Expand Down
49 changes: 33 additions & 16 deletions src/commands/which.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import {Command, ux} from '@oclif/core'
import {Command, Errors, toConfiguredId, ux} from '@oclif/core'

type WhichResult = {
aliasOf?: string
plugin: string
}

export default class Which extends Command {
static description = 'Show which plugin a command is in.'
static enableJsonFlag = true

static examples = [
{
command: '<%= config.bin %> <%= command.id %> help',
Expand All @@ -11,28 +18,38 @@ export default class Which extends Command {

static strict = false

async run(): Promise<void> {
async run(): Promise<WhichResult> {
const {argv} = await this.parse(Which)

if (argv.length === 0) {
throw new Error('"which" expects a command name. Try something like "which your:command:here" ')
throw new Errors.CLIError('"which" expects a command name', {
suggestions: [`Provide a command id like this, "${this.config.bin} which your:command:here"`],
})
}

let command = argv
// if argv is length 1 and is a string, split it by the topicSeparator (e.g. "my:command" => ["my", "command"], "my command" => ["my", "command"])
// otherwise, use argv as is (e.g. ["my", "command"] => ["my", "command"])
const commandParts =
argv.length === 1 && typeof argv[0] === 'string' ? argv[0].split(this.config.topicSeparator) : argv

const cmd = this.config.findCommand(commandParts.join(':'), {must: true})

const isAlias = cmd.aliases.includes(commandParts.join(':'))

const result: WhichResult = {plugin: cmd.pluginName ?? 'unknown'}

if (isAlias) {
const possible = this.config.commands.find((c) => c.aliases.includes(cmd.id) && c.id !== cmd.id)
if (possible) {
result.aliasOf = toConfiguredId(possible?.id, this.config)
}
}

if (argv.length === 1 && typeof argv[0] === 'string') {
// If this if statement is true then the command to find was passed in as a single string, e.g. `mycli which "my command"`
// So we must use the topicSeparator to split it into an array
command = argv[0].split(this.config.topicSeparator)
if (!this.jsonEnabled()) {
ux.styledHeader(commandParts.join(this.config.topicSeparator))
ux.styledObject(result)
}

const cmd = this.config.findCommand(command.join(':'), {must: true})
ux.styledHeader(command.join(this.config.topicSeparator))
ux.styledObject(
{
plugin: cmd.pluginName,
},
['plugin'],
)
return result
}
}
65 changes: 53 additions & 12 deletions test/commands/which.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,59 @@
import {expect, test} from '@oclif/test'
import {Config, ux} from '@oclif/core'
import {expect} from 'chai'
import {dirname, join} from 'node:path'
import {fileURLToPath} from 'node:url'
import {SinonSandbox, createSandbox} from 'sinon'

const root = join(dirname(fileURLToPath(import.meta.url)), '..', 'fixtures/test-plugin')
console.log(root)
describe('which', () => {
test
.stdout()
.command(['which', 'which'])
.it('which which', ctx => {
expect(ctx.stdout).to.contain('@oclif/plugin-which')
let sandbox: SinonSandbox
let config: Config

beforeEach(async () => {
sandbox = createSandbox()
sandbox.stub(ux.write, 'stdout')
config = await Config.load({root})
})

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

it('should return plugin name for colon separate command', async () => {
const {plugin} = await config.runCommand<{plugin: string}>('which', ['foo:bar'])
expect(plugin).to.equal('test-plugin')
})

it('should return plugin name for quoted space separate command', async () => {
const {plugin} = await config.runCommand<{plugin: string}>('which', ['foo bar'])
expect(plugin).to.equal('test-plugin')
})

it('should return plugin name for unquoted space separate command', async () => {
const {plugin} = await config.runCommand<{plugin: string}>('which', ['foo', 'bar'])
expect(plugin).to.equal('test-plugin')
})

it('should return plugin name and aliasOf for alias', async () => {
const {aliasOf, plugin} = await config.runCommand<{aliasOf: string; plugin: string}>('which', ['foo:alias'])
expect(plugin).to.equal('test-plugin')
expect(aliasOf).to.equal('foo bar')
})

it('should not return aliasOf if not an alias', async () => {
const {aliasOf, plugin} = await config.runCommand<{aliasOf: string; plugin: string}>('which', ['foo:bar'])
expect(plugin).to.equal('test-plugin')
expect(aliasOf).to.be.undefined
})

test
.stdout()
.command(['which'])
.catch(error => {
expect(error.message).to.contain('"which" expects a command name. Try something like "which your:command:here" ')
it('should throw error if no command is provided', async () => {
try {
await config.runCommand('which')
throw new Error('expected error')
} catch (error) {
const {message} = error as Error
expect(message).to.contain('"which" expects a command name')
}
})
.it('checks arg was provided')
})
13 changes: 13 additions & 0 deletions test/fixtures/test-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "test-plugin",
"version": "0.0.0",
"private": true,
"oclif": {
"commands": "./src/commands",
"topicSeparator": " ",
"plugins": [
"@oclif/plugin-which"
]
},
"main": "./src/index.js"
}
9 changes: 9 additions & 0 deletions test/fixtures/test-plugin/src/commands/foo/bar.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const Command = {
aliases: ['foo:alias', 'foo:bar:alias', 'bar'],
description: 'foo bar description',
run() {
console.log('running Bar')
},
}

module.exports = Command
Loading
Loading