Skip to content

Commit

Permalink
fix: clean up usage output
Browse files Browse the repository at this point in the history
Removed usage lib, rolled logic into base-command.js
Cleaned up usage output to be less redundant
  • Loading branch information
wraithgar committed Mar 23, 2022
1 parent b9fc0ed commit 67b44a7
Show file tree
Hide file tree
Showing 17 changed files with 111 additions and 395 deletions.
48 changes: 32 additions & 16 deletions lib/base-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

const { relative } = require('path')

const usageUtil = require('./utils/usage.js')
const ConfigDefinitions = require('./utils/config/definitions.js')
const getWorkspaces = require('./workspaces/get-workspaces.js')

const cmdAliases = require('./utils/cmd-list').aliases

class BaseCommand {
constructor (npm) {
this.wrapWidth = 80
Expand All @@ -25,28 +26,43 @@ class BaseCommand {
}

get usage () {
let usage = [
`npm ${this.constructor.name}\n`,
`${this.constructor.description}\n`,
`Usage:\n`,
].join('\n')
const usage = [
`${this.constructor.description}`,
'',
'Usage:',
]

if (!this.constructor.usage) {
usage = `${usage}npm ${this.constructor.name}`
usage.push(`npm ${this.constructor.name}`)
} else {
usage = `${usage}${this.constructor.usage
.map(u => `npm ${this.constructor.name} ${u}`)
.join('\n')}`
usage.push(...this.constructor.usage.map(u => `npm ${this.constructor.name} ${u}`))
}

if (this.constructor.params) {
usage = `${usage}\n\nOptions:\n${this.wrappedParams}`
usage.push('')
usage.push('Options:')
usage.push(this.wrappedParams)
}

// Mostly this just appends aliases, this could be more clear
usage = usageUtil(this.constructor.name, usage)
usage = `${usage}\n\nRun "npm help ${this.constructor.name}" for more info`
return usage
const aliases = Object.keys(cmdAliases).reduce((p, c) => {
if (cmdAliases[c] === this.constructor.name) {
p.push(c)
}
return p
}, [])

if (aliases.length === 1) {
usage.push('')
usage.push(`alias: ${aliases.join(', ')}`)
} else if (aliases.length > 1) {
usage.push('')
usage.push(`aliases: ${aliases.join(', ')}`)
}

usage.push('')
usage.push(`Run "npm help ${this.constructor.name}" for more info`)

return usage.join('\n')
}

get wrappedParams () {
Expand All @@ -69,7 +85,7 @@ class BaseCommand {
if (prefix) {
prefix += '\n\n'
}
return Object.assign(new Error(`\nUsage: ${prefix}${this.usage}`), {
return Object.assign(new Error(`\n${prefix}${this.usage}`), {
code: 'EUSAGE',
})
}
Expand Down
25 changes: 0 additions & 25 deletions lib/utils/usage.js

This file was deleted.

Loading

0 comments on commit 67b44a7

Please sign in to comment.