| const localeCompare = require('@isaacs/string-locale-compare')('en')
|
| const { join, basename, resolve } = require('path')
|
| const transformHTML = require('./transform-html.js')
|
| const { version } = require('../../lib/npm.js')
|
| const { aliases } = require('../../lib/utils/cmd-list')
|
| const { shorthands, definitions } = require('@npmcli/config/lib/definitions')
|
|
|
| const DOC_EXT = '.md'
|
|
|
| const TAGS = {
|
| CONFIG: '<!-- AUTOGENERATED CONFIG DESCRIPTIONS -->',
|
| USAGE: '<!-- AUTOGENERATED USAGE DESCRIPTIONS -->',
|
| SHORTHANDS: '<!-- AUTOGENERATED CONFIG SHORTHANDS -->',
|
| }
|
|
|
| const assertPlaceholder = (src, path, placeholder) => {
|
| if (!src.includes(placeholder)) {
|
| throw new Error(
|
| `Cannot replace ${placeholder} in ${path} due to missing placeholder`
|
| )
|
| }
|
| return placeholder
|
| }
|
|
|
|
|
| const defaultCommandLoader = (name) => {
|
| return require(`../../lib/commands/${name}`)
|
| }
|
|
|
|
|
| const getCommand = (name, commandLoader = defaultCommandLoader) => {
|
| return commandLoader(name)
|
| }
|
|
|
|
|
| const resolveDefinitions = (command) => {
|
|
|
| if (command.definitions && Object.keys(command.definitions).length > 0) {
|
| return command.definitions
|
| }
|
|
|
|
|
| if (command.params) {
|
| const resolved = {}
|
| for (const param of command.params) {
|
| if (definitions[param]) {
|
| resolved[param] = definitions[param]
|
| }
|
| }
|
| return resolved
|
| }
|
|
|
| return {}
|
| }
|
|
|
| const getCommandByDoc = (docFile, docExt, commandLoader = defaultCommandLoader) => {
|
|
|
|
|
|
|
| const name = basename(docFile, docExt).replace('npm-', '')
|
|
|
| if (name === 'npm') {
|
| return {
|
| name,
|
| definitions: [],
|
| usage: 'npm',
|
| }
|
| }
|
|
|
|
|
|
|
|
|
| const srcName = name === 'npx' ? 'exec' : name
|
| const command = getCommand(srcName, commandLoader)
|
| const { usage = [''], workspaces } = command
|
| const usagePrefix = name === 'npx' ? 'npx' : `npm ${name}`
|
|
|
|
|
| const commandDefs = resolveDefinitions(command)
|
| const resolvedDefs = {}
|
| for (const [key, def] of Object.entries(commandDefs)) {
|
| resolvedDefs[key] = def
|
|
|
| if (def.exclusive) {
|
| for (const e of def.exclusive) {
|
| if (!resolvedDefs[e] && definitions[e]) {
|
| resolvedDefs[e] = definitions[e]
|
| }
|
| }
|
| }
|
| }
|
|
|
| return {
|
| name,
|
| workspaces,
|
| definitions: name === 'npx' ? {} : resolvedDefs,
|
| usage: usage?.map(u => `${usagePrefix} ${u}`.trim()).join('\n'),
|
| }
|
| }
|
|
|
| const replaceVersion = (src) => src.replace(/@VERSION@/g, version)
|
|
|
| const replaceUsage = (src, { path, commandLoader }) => {
|
| const replacer = assertPlaceholder(src, path, TAGS.USAGE)
|
| const { usage, name, workspaces } = getCommandByDoc(path, DOC_EXT, commandLoader)
|
|
|
| const synopsis = []
|
|
|
| if (usage) {
|
| synopsis.push('```bash', usage)
|
|
|
| const cmdAliases = Object.keys(aliases).reduce((p, c) => {
|
| if (aliases[c] === name) {
|
| p.push(c)
|
| }
|
| return p
|
| }, [])
|
|
|
| if (cmdAliases.length === 1) {
|
| synopsis.push('', `alias: ${cmdAliases[0]}`)
|
| } else if (cmdAliases.length > 1) {
|
| synopsis.push('', `aliases: ${cmdAliases.join(', ')}`)
|
| }
|
|
|
| synopsis.push('```')
|
| }
|
|
|
| if (!workspaces) {
|
| if (synopsis.length) {
|
| synopsis.push('')
|
| }
|
| synopsis.push('Note: This command is unaware of workspaces.')
|
| }
|
|
|
| return src.replace(replacer, synopsis.join('\n'))
|
| }
|
|
|
|
|
| const generateFlagsTable = (definitionPool) => {
|
| const rows = Object.keys(definitionPool).map((n) => {
|
| const def = definitionPool[n]
|
| const flags = [`\`--${def.key}\``]
|
| if (def.alias) {
|
| flags.push(...def.alias.map(a => `\`--${a}\``))
|
| }
|
| if (def.short) {
|
| flags.push(`\`-${def.short}\``)
|
| }
|
| const flagsStr = flags.join(', ')
|
| let defaultVal = def.defaultDescription
|
| if (!defaultVal) {
|
| defaultVal = String(def.default)
|
| }
|
| let typeVal = def.typeDescription || String(def.type)
|
| if (def.required) {
|
| typeVal = `${typeVal} (required)`
|
| }
|
| const desc = (def.description || '').replace(/\n/g, ' ').trim()
|
| return `| ${flagsStr} | ${defaultVal} | ${typeVal} | ${desc} |`
|
| })
|
|
|
| return [
|
| '| Flag | Default | Type | Description |',
|
| '| --- | --- | --- | --- |',
|
| ...rows,
|
| ].join('\n')
|
| }
|
|
|
| const replaceDefinitions = (src, { path, commandLoader }) => {
|
| const { definitions: commandDefs, name } = getCommandByDoc(path, DOC_EXT, commandLoader)
|
|
|
| let subcommands = {}
|
| try {
|
| const command = getCommand(name, commandLoader)
|
| subcommands = command.subcommands || {}
|
| } catch {
|
|
|
| }
|
|
|
|
|
| if (Object.keys(commandDefs).length === 0 && Object.keys(subcommands).length === 0) {
|
| return src
|
| }
|
|
|
|
|
| const replacer = assertPlaceholder(src, path, TAGS.CONFIG)
|
|
|
|
|
| if (Object.keys(subcommands).length > 0) {
|
| const subcommandSections = Object.entries(subcommands).map(([subName, SubCommand]) => {
|
| const subUsage = SubCommand.usage || []
|
| const subDefs = resolveDefinitions(SubCommand)
|
|
|
| const parts = [`### \`npm ${name} ${subName}\``, '']
|
|
|
| if (SubCommand.description) {
|
| parts.push(SubCommand.description, '')
|
| }
|
|
|
|
|
| if (subUsage.length > 0) {
|
| parts.push('#### Synopsis', '', '```bash')
|
| subUsage.forEach(u => {
|
| parts.push(`npm ${name} ${subName} ${u}`.trim())
|
| })
|
| parts.push('```', '')
|
| }
|
|
|
|
|
| if (Object.keys(subDefs).length > 0) {
|
| parts.push('#### Flags', '')
|
| parts.push(generateFlagsTable(subDefs), '')
|
| }
|
|
|
| return parts.join('\n')
|
| })
|
|
|
| return src.replace(replacer, subcommandSections.join('\n'))
|
| }
|
|
|
|
|
|
|
| const paramDescriptions = Object.values(commandDefs)
|
| .map(def => def.describe())
|
|
|
| return src.replace(replacer, paramDescriptions.join('\n\n'))
|
| }
|
|
|
| const replaceConfig = (src, { path }) => {
|
| const replacer = assertPlaceholder(src, path, TAGS.CONFIG)
|
|
|
|
|
| |
| |
|
|
| const sort = ([keya, { deprecated: depa }], [keyb, { deprecated: depb }]) => {
|
| return depa && !depb ? 1
|
| : !depa && depb ? -1
|
| : localeCompare(keya, keyb)
|
| }
|
|
|
| const allConfig = Object.entries(definitions).sort(sort)
|
| .map(([, def]) => def.describe())
|
| .join('\n\n')
|
|
|
| return src.replace(replacer, allConfig)
|
| }
|
|
|
| const replaceShorthands = (src, { path }) => {
|
| const replacer = assertPlaceholder(src, path, TAGS.SHORTHANDS)
|
|
|
| const sh = Object.entries(shorthands)
|
| .sort(([shorta, expansiona], [shortb, expansionb]) =>
|
|
|
| localeCompare(expansiona.join(' '), expansionb.join(' ')) || localeCompare(shorta, shortb)
|
| )
|
| .map(([short, expansion]) => {
|
|
|
|
|
| const dash = short.length === 1 ? '-' : '--'
|
| return `* \`${dash}${short}\`: \`${expansion.join(' ')}\``
|
| })
|
|
|
| return src.replace(replacer, sh.join('\n'))
|
| }
|
|
|
| const replaceHelpLinks = (src) => {
|
|
|
| return src.replace(
|
| /\[`?([\w\s-]+)`?\]\(\/(?:commands|configuring-npm|using-npm)\/(?:[\w\s-]+)\)/g,
|
| (_, p1) => {
|
| const term = p1.replace(/npm\s/g, '').replace(/\s+/g, ' ').trim()
|
| const help = `npm help ${term.includes(' ') ? `"${term}"` : term}`
|
| return help
|
| }
|
| )
|
| }
|
|
|
| const transformMan = (src, { data, unified, remarkParse, remarkMan }) => unified()
|
| .use(remarkParse)
|
| .use(remarkMan, { version: `NPM@${version}` })
|
| .processSync(`# ${data.title}(${data.section}) - ${data.description}\n\n${src}`)
|
| .toString()
|
|
|
| const manPath = (name, { data }) => join(`man${data.section}`, `${name}.${data.section}`)
|
|
|
| const transformMd = (src, { frontmatter }) => ['---', frontmatter, '---', '', src].join('\n')
|
|
|
| module.exports = {
|
| DOC_EXT,
|
| TAGS,
|
| paths: {
|
| content: resolve(__dirname, 'content'),
|
| nav: resolve(__dirname, 'content', 'nav.yml'),
|
| template: resolve(__dirname, 'template.html'),
|
| man: resolve(__dirname, '..', '..', 'man'),
|
| html: resolve(__dirname, '..', 'output'),
|
| md: resolve(__dirname, '..', 'content'),
|
| },
|
| usage: replaceUsage,
|
| definitions: replaceDefinitions,
|
| config: replaceConfig,
|
| shorthands: replaceShorthands,
|
| version: replaceVersion,
|
| helpLinks: replaceHelpLinks,
|
| man: transformMan,
|
| manPath: manPath,
|
| md: transformMd,
|
| html: transformHTML,
|
| }
|
|
|