Welcome to mirror list, hosted at ThFree Co, Russian Federation.

config-doc.js « scripts - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5014bcdc46c195f4c39db275b9c875bcce565c48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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(' '), 'en') ||
        shorta.localeCompare(shortb, 'en')
    })
    .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`)