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 24, 2022
1 parent 53c2f43 commit 5649c30
Show file tree
Hide file tree
Showing 18 changed files with 125 additions and 399 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.

18 changes: 14 additions & 4 deletions scripts/config-doc-command.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { definitions } = require('../lib/utils/config/index.js')
const usageFn = require('../lib/utils/usage.js')
const cmdAliases = require('../lib/utils/cmd-list').aliases
const { writeFileSync, readFileSync } = require('fs')
const { resolve } = require('path')

Expand Down Expand Up @@ -52,9 +52,19 @@ const describeUsage = ({ usage }) => {
synopsis.push(usage.map(usageInfo => `${baseCommand} ${usageInfo}`).join('\n'))
}

const aliases = usageFn(commandName, '').trim()
if (aliases) {
synopsis.push(`\n${aliases}`)
const aliases = Object.keys(cmdAliases).reduce((p, c) => {
if (cmdAliases[c] === commandName) {
p.push(c)
}
return p
}, [])

if (aliases.length === 1) {
synopsis.push('')
synopsis.push(`alias: ${aliases[0]}`)
} else if (aliases.length > 1) {
synopsis.push('')
synopsis.push(`aliases: ${aliases.join(', ')}`)
}
}
} else {
Expand Down
Loading

0 comments on commit 5649c30

Please sign in to comment.