Skip to content

Commit

Permalink
feat(cli): support command usage
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanSalt committed Jun 18, 2022
1 parent 9301407 commit 7fa53fb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
24 changes: 15 additions & 9 deletions addons/cli/src/main/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,31 @@ export interface CommandContext {

export interface CommandModule {
command: string,
usage?: string,
handler: (context: CommandContext, event: IpcMainInvokeEvent) => any,
}

function getCommandModule(command: string, commands: CommandModule[]) {
const settings = commas.settings.useSettings()
const aliases = settings['cli.command.aliases'] ?? {}
while (aliases[command]) {
command = aliases[command].trim()
}
return commands.find(item => item.command === command)
}

async function executeCommand(
event: IpcMainInvokeEvent,
inputContext: CommandContext,
commands: CommandModule[],
aliases: Record<string, string>,
) {
const [subcommand, ...argv] = inputContext.argv
let command = subcommand
while (aliases[command]) {
command = aliases[command].trim()
}
const controller = commands.find(item => item.command === command)
if (!controller) {
return executeCommand(event, { ...inputContext, argv: ['help'] }, commands, {})
const mod = getCommandModule(subcommand, commands)
if (!mod) {
return executeCommand(event, { ...inputContext, argv: ['help'] }, commands)
}
const context = { ...inputContext, argv }
const stdout = await controller.handler(context, event)
const stdout = await mod.handler(context, event)
return typeof stdout === 'string' ? stdout : undefined
}

Expand All @@ -56,6 +61,7 @@ function useExternalURLCommands() {
}

export {
getCommandModule,
executeCommand,
useExternalURLCommands,
}
22 changes: 18 additions & 4 deletions addons/cli/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as commas from 'commas:api/main'
import { app, BrowserWindow } from 'electron'
import { random } from 'lodash'
import type { CommandModule } from './command'
import { executeCommand, useExternalURLCommands } from './command'
import { getCommandModule, executeCommand, useExternalURLCommands } from './command'

declare module '../../../../src/typings/settings' {
export interface Settings {
Expand All @@ -30,8 +30,7 @@ export default () => {

const commands: CommandModule[] = commas.context.getCollection('cli')
commas.ipcMain.handle('cli', (event, context) => {
const aliases = settings['cli.command.aliases'] ?? {}
return executeCommand(event, context, commands, aliases)
return executeCommand(event, context, commands)
})

/** {@link https://github.com/npm/cli/blob/latest/lib/utils/npm-usage.js#L39-L55} */
Expand Down Expand Up @@ -62,7 +61,8 @@ export default () => {

commas.context.provide('cli', {
command: 'help',
handler() {
usage: '[command]',
handler({ argv }) {
/** {@link https://patorjk.com/software/taag/#p=display&f=ANSI%20Shadow&t=COMMAS} */
const ansi = `
██████╗! ██████╗ !███╗ ███╗!███╗ ███╗! █████╗ !███████╗
Expand All @@ -88,6 +88,14 @@ export default () => {
})
.join('\n')

const helpingCommand = argv[0]
const manual = helpingCommand ? getCommandModule(argv[0], commands) : undefined
if (manual) {
return `${ansi}
Usage: commas ${helpingCommand}${manual.usage ? ' ' + manual.usage : ''}
`
}

return `${ansi}
Usage: commas <command>
Expand All @@ -106,22 +114,26 @@ where <command> is one of:

commas.context.provide('cli', {
command: 'run',
usage: '<...command-with-args>',
handler({ argv }, event) {
event.sender.send('open-tab', {
// TODO: shell quote
command: argv.join(' '),
})
},
})

commas.context.provide('cli', {
command: 'edit',
usage: '<file>',
handler({ argv, cwd }, event) {
event.sender.send('open-code-editor', path.join(cwd, argv[0]))
},
})

commas.context.provide('cli', {
command: 'select',
usage: '<nth-tab>',
handler({ argv }, event) {
const index = Number.parseInt(argv[0], 10)
if (!Number.isNaN(index)) {
Expand Down Expand Up @@ -154,6 +166,7 @@ where <command> is one of:

commas.context.provide('cli', {
command: 'roll',
usage: '[n-times]',
handler({ argv }) {
let length = Number.parseInt(argv[0], 10)
if (Number.isNaN(length)) {
Expand All @@ -166,6 +179,7 @@ where <command> is one of:

commas.context.provide('cli', {
command: 'preview',
usage: '[file]',
handler({ argv, cwd }, event) {
const frame = BrowserWindow.fromWebContents(event.sender)
if (!frame) return
Expand Down
1 change: 1 addition & 0 deletions addons/power-mode/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default () => {

commas.context.provide('cli', {
command: 'power',
usage: '[off]',
async handler({ argv }, event) {
const [status] = argv
const enabled = status !== 'off'
Expand Down
1 change: 1 addition & 0 deletions addons/proxy/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default () => {
// Server
commas.context.provide('cli', {
command: 'whistle',
usage: '<command> [options]',
async handler({ argv }) {
try {
const { stdout } = await whistle(quote(argv))
Expand Down

0 comments on commit 7fa53fb

Please sign in to comment.