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

docs: auto-generate npm usage for each command #4450

Merged
merged 7 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions docs/content/commands/npm-exec.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ description: Run a command from a local or remote npm package
<!-- see lib/commands/exec.js -->

```bash
npx -- <pkg>[@<version>] [args...]
npx --package=<pkg>[@<version>] -- <cmd> [args...]
npx -c '<cmd> [args...]'
npx --package=foo -c '<cmd> [args...]'
npm exec -- <pkg>[@<version>] [args...]
npm exec --package=<pkg>[@<version>] -- <cmd> [args...]
npm exec -c '<cmd> [args...]'
npm exec --package=foo -c '<cmd> [args...]'

alias: x
```
Expand Down
13 changes: 13 additions & 0 deletions docs/content/commands/npx.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ description: Run a command from a local or remote npm package
### Synopsis

<!-- AUTOGENERATED USAGE DESCRIPTIONS START -->
wraithgar marked this conversation as resolved.
Show resolved Hide resolved
<!-- automatically generated, do not edit manually -->
<!-- see lib/commands/exec.js -->

```bash
npx -- <pkg>[@<version>] [args...]
npx --package=<pkg>[@<version>] -- <cmd> [args...]
npx -c '<cmd> [args...]'
npx --package=foo -c '<cmd> [args...]'
```

<!-- automatically generated, do not edit manually -->
<!-- see lib/commands/exec.js -->

<!-- AUTOGENERATED USAGE DESCRIPTIONS END -->

### Description
Expand Down
42 changes: 24 additions & 18 deletions scripts/config-doc-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,31 @@ const describeAll = (content) =>

const describeUsage = ({ usage }) => {
const synopsis = []
const commandName = commandFile.split('/').pop().split('.')[0]

// Grab the command name from the *.md filename
// NOTE: We cannot use the name property command file because in the case of
// `npx` the file being used is `lib/commands/exec.js`
const commandName = configDoc.split('/').pop().split('.')[0].replace('npm-', '')
synopsis.push('\n```bash')

if (commandName) {
let baseCommand = `npm ${commandName}`

// special case for `npx`
if (commandName === 'exec') {
baseCommand = 'npx'
}

if (!usage) {
synopsis.push(baseCommand)
// special case for `npx`:
// `npx` is not technically a command in and of itself,
// so it just needs the usage and parameters of npm exec, and none of the aliases
if (commandName === 'npx') {
synopsis.push(usage.map(usageInfo => `npx ${usageInfo}`).join('\n'))
} else {
synopsis.push(usage.map(usageInfo => `${baseCommand} ${usageInfo}`).join('\n'))
}

const aliases = usageFn(commandName, '').trim()
if (aliases) {
synopsis.push(`\n${aliases}`)
const baseCommand = `npm ${commandName}`
if (!usage) {
synopsis.push(baseCommand)
} else {
synopsis.push(usage.map(usageInfo => `${baseCommand} ${usageInfo}`).join('\n'))
}

const aliases = usageFn(commandName, '').trim()
if (aliases) {
synopsis.push(`\n${aliases}`)
}
}
} else {
console.error(`could not determine command name from ${commandFile}`)
Expand Down Expand Up @@ -106,19 +111,20 @@ try {
const hasTag = doc.includes(TAGS.CONFIG.START)
const hasUsageTag = doc.includes(TAGS.USAGE.START)

if (params.length) {
if (params?.length) {
manekinekko marked this conversation as resolved.
Show resolved Hide resolved
let newDoc = hasTag ? addDescriptions(doc) : doc
newDoc = hasUsageTag ? addUsageDescriptions(newDoc) : newDoc

if (!hasTag) {
console.error('WARNING: did not find config description section', configDoc)
}

if (usage.length && !hasUsageTag) {
if (usage?.length && !hasUsageTag) {
wraithgar marked this conversation as resolved.
Show resolved Hide resolved
console.error('WARNING: did not find usage description section', configDoc)
}
writeFileSync(configDoc, newDoc)
}
} catch (err) {
console.error(`WARNING: file cannot be open: ${configDoc}`)
console.error(err)
}