Skip to content

Commit

Permalink
docs: special case for npx
Browse files Browse the repository at this point in the history
Closes npm#4189
  • Loading branch information
manekinekko committed Feb 24, 2022
1 parent d238424 commit 36e0230
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
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 -->
<!-- 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) {
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) {
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)
}

0 comments on commit 36e0230

Please sign in to comment.