diff options
author | isaacs <i@izs.me> | 2021-03-16 02:53:36 +0300 |
---|---|---|
committer | isaacs <i@izs.me> | 2021-03-18 21:58:08 +0300 |
commit | 68db12440fc9e851563b2c2acd85499776a2a63b (patch) | |
tree | 254e9b47ca13cf701aaf7406462d9b0e3de10262 /scripts | |
parent | 6598bfe8697439e827d84981f8504febca64a55a (diff) |
Auto-generate 'npm help 7 config' from actual definitions
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/config-doc.js | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/scripts/config-doc.js b/scripts/config-doc.js new file mode 100644 index 000000000..9bb462889 --- /dev/null +++ b/scripts/config-doc.js @@ -0,0 +1,50 @@ +const { shorthands, describeAll } = require('../lib/utils/config/index.js') +const { writeFileSync, readFileSync } = require('fs') +const { resolve } = require('path') +const configDoc = resolve(__dirname, '../docs/content/using-npm/config.md') + +const addBetweenTags = (doc, startTag, endTag, body) => { + const startSplit = doc.split(startTag) + if (startSplit.length !== 2) + throw new Error('Did not find exactly one start tag') + + const endSplit = startSplit[1].split(endTag) + if (endSplit.length !== 2) + throw new Error('Did not find exactly one end tag') + + return [ + startSplit[0], + startTag, + '\n<!-- automatically generated, do not edit manually -->\n', + body, + '\n\n', + endTag, + endSplit[1], + ].join('') +} + +const addDescriptions = doc => { + const startTag = '<!-- AUTOGENERATED CONFIG DESCRIPTIONS START -->' + const endTag = '<!-- AUTOGENERATED CONFIG DESCRIPTIONS END -->' + return addBetweenTags(doc, startTag, endTag, describeAll()) +} + +const addShorthands = doc => { + const startTag = '<!-- AUTOGENERATED CONFIG SHORTHANDS START -->' + const endTag = '<!-- AUTOGENERATED CONFIG SHORTHANDS END -->' + const body = Object.entries(shorthands) + .sort(([shorta, expansiona], [shortb, expansionb]) => { + // sort by what they're short FOR + return expansiona.join(' ').localeCompare(expansionb.join(' ')) || + shorta.localeCompare(shortb) + }) + .map(([short, expansion]) => { + const dash = short.length === 1 ? '-' : '--' + return `* \`${dash}${short}\`: \`${expansion.join(' ')}\`` + }).join('\n') + return addBetweenTags(doc, startTag, endTag, body) +} + +const doc = readFileSync(configDoc, 'utf8') +writeFileSync(configDoc, addDescriptions(addShorthands(doc))) +console.log(`updated docs/content/using-npm/config.md`) |