Skip to content

Commit

Permalink
docs: auto-generate synopsis sections
Browse files Browse the repository at this point in the history
Fixes npm#4189
  • Loading branch information
manekinekko committed Feb 18, 2022
1 parent 20c83fa commit 58963ed
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
4 changes: 4 additions & 0 deletions docs/content/commands/npm-init.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ description: Create a package.json file

### Synopsis

<!-- AUTOGENERATED USAGE DESCRIPTIONS START -->

```bash
npm init [--yes|-y|--scope]
npm init <@scope> (same as `npm exec <@scope>/create`)
npm init [<@scope>/]<name> (same as `npm exec [<@scope>/]create-<name>`)
npm init [-w <dir>] [args...]
```

<!-- AUTOGENERATED USAGE DESCRIPTIONS END -->

### Description

`npm init <initializer>` can be used to set up a new or existing npm
Expand Down
26 changes: 21 additions & 5 deletions scripts/config-doc-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ const configDoc = process.argv[2]
const commandFile = process.argv[3]

// Note: commands without params skip this whole process.
const { params } = require(resolve(commandFile))
const { params, usage } = require(resolve(commandFile))

const describeAll = () =>
params.map(name => definitions[name].describe()).join(
const describeAll = (content) =>
content.map(name => definitions[name].describe()).join(
'\n\n<!-- automatically generated, do not edit manually -->\n' +
'<!-- see lib/utils/config/definitions.js -->\n\n'
)

const addBetweenTags = (doc, startTag, endTag, body) => {
const startSplit = doc.split(startTag)
console.log(startSplit)

if (startSplit.length !== 2) {
throw new Error('Did not find exactly one start tag')
}
Expand All @@ -42,14 +44,28 @@ const addBetweenTags = (doc, startTag, endTag, body) => {
const addDescriptions = doc => {
const startTag = '<!-- AUTOGENERATED CONFIG DESCRIPTIONS START -->'
const endTag = '<!-- AUTOGENERATED CONFIG DESCRIPTIONS END -->'
return addBetweenTags(doc, startTag, endTag, describeAll())
return addBetweenTags(doc, startTag, endTag, describeAll(params))
}

const addUsageDescriptions = doc => {
const startTag = '<!-- AUTOGENERATED USAGE DESCRIPTIONS START -->'
const endTag = '<!-- AUTOGENERATED USAGE DESCRIPTIONS END -->'
return addBetweenTags(doc, startTag, endTag, describeAll(usage))
}

// always write SOMETHING so that Make sees the file is up to date.
const doc = readFileSync(configDoc, 'utf8')
const hasTag = doc.includes('<!-- AUTOGENERATED CONFIG DESCRIPTIONS START -->')
const newDoc = params && hasTag ? addDescriptions(doc) : doc
const hasUsageTag = doc.includes('<!-- AUTOGENERATED SYNOPSIS DESCRIPTIONS START -->')

let newDoc = params && hasTag ? addDescriptions(doc) : doc
newDoc = usage && hasUsageTag ? addUsageDescriptions(newDoc) : newDoc

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

if (usage && !hasUsageTag) {
console.error('WARNING: did not find usage description section', configDoc)
}
writeFileSync(configDoc, newDoc)

0 comments on commit 58963ed

Please sign in to comment.