-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathbuild.js
More file actions
439 lines (372 loc) · 13.5 KB
/
build.js
File metadata and controls
439 lines (372 loc) · 13.5 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
const { join, dirname, basename, extname, sep, posix } = require('path')
const fs = require('fs/promises')
const ignoreWalk = require('ignore-walk')
const yaml = require('yaml')
const parseFrontMatter = require('front-matter')
const checkNav = require('./check-nav.js')
const { DOC_EXT, ...transform } = require('./index.js')
// Helper to check if a directory exists
const dirExists = async (path) => {
try {
const stat = await fs.stat(path)
return stat.isDirectory()
} catch {
return false
}
}
// Helper to read docs from a section directory
const readSectionDocs = async (contentPath, section, orderedUrls) => {
const sectionPath = join(contentPath, section)
if (!await dirExists(sectionPath)) {
return []
}
const files = await fs.readdir(sectionPath)
const docFiles = files.filter(f => f.endsWith(DOC_EXT))
// If no doc files exist, return empty array
/* istanbul ignore if - defensive check for empty directories */
if (docFiles.length === 0) {
return []
}
// Parse each doc file to get title and description from frontmatter
const docs = await Promise.all(
docFiles.map(async (file) => {
const content = await fs.readFile(join(sectionPath, file), 'utf-8')
const { attributes } = parseFrontMatter(content)
const name = basename(file, DOC_EXT)
return {
title: attributes.title,
url: `/${section}/${name}`,
description: attributes.description,
name,
}
})
)
// Preserve order from orderedUrls, append any new files at the end sorted alphabetically
const orderedDocs = []
const docsByUrl = new Map(docs.map(d => [d.url, d]))
// First, add docs in the order they appear in orderedUrls
for (const url of orderedUrls) {
const doc = docsByUrl.get(url)
if (doc) {
orderedDocs.push(doc)
docsByUrl.delete(url)
}
}
return orderedDocs.map(({ name, ...rest }) => rest)
}
// Generate nav.yml from the filesystem
const generateNav = async (contentPath, navPath) => {
const docsCommandsPath = join(contentPath, 'commands')
// Read all command files
const commandFiles = await dirExists(docsCommandsPath) ? await fs.readdir(docsCommandsPath) : []
const commandDocs = commandFiles.filter(f => f.endsWith(DOC_EXT))
// Parse each command file to get title and description
const allCommands = await Promise.all(
commandDocs.map(async (file) => {
const content = await fs.readFile(join(docsCommandsPath, file), 'utf-8')
const { attributes } = parseFrontMatter(content)
const name = basename(file, DOC_EXT)
const title = (attributes.title || name).replace(/^npm-/, 'npm ')
return {
title,
url: `/commands/${name}`,
description: attributes.description || '',
name,
}
})
)
// Sort commands: npm first, then alphabetically, npx last
const npm = allCommands.find(c => c.name === 'npm')
const npx = allCommands.find(c => c.name === 'npx')
const others = allCommands
.filter(c => c.name !== 'npm' && c.name !== 'npx')
.sort((a, b) => a.name.localeCompare(b.name))
// Remove the name field
const commands = [npm, ...others, npx].filter(Boolean).map(({ name, ...rest }) => rest)
// Hardcoded order for configuring-npm section (only urls - title/description come from frontmatter)
const configuringNpmOrder = [
'/configuring-npm/install',
'/configuring-npm/folders',
'/configuring-npm/npmrc',
'/configuring-npm/npm-shrinkwrap-json',
'/configuring-npm/package-json',
'/configuring-npm/package-lock-json',
]
// Hardcoded order for using-npm section (only urls - title/description come from frontmatter)
const usingNpmOrder = [
'/using-npm/registry',
'/using-npm/package-spec',
'/using-npm/config',
'/using-npm/logging',
'/using-npm/scope',
'/using-npm/scripts',
'/using-npm/workspaces',
'/using-npm/orgs',
'/using-npm/dependency-selectors',
'/using-npm/developers',
'/using-npm/removal',
]
// Read actual docs from configuring-npm and using-npm directories
const configuringNpmDocs = await readSectionDocs(contentPath, 'configuring-npm', configuringNpmOrder)
const usingNpmDocs = await readSectionDocs(contentPath, 'using-npm', usingNpmOrder)
// Build the navigation structure - only include sections with content
const navData = []
if (commands.length > 0) {
navData.push({
title: 'CLI Commands',
shortName: 'Commands',
url: '/commands',
children: commands,
})
}
if (configuringNpmDocs.length > 0) {
navData.push({
title: 'Configuring npm',
shortName: 'Configuring',
url: '/configuring-npm',
children: configuringNpmDocs,
})
}
if (usingNpmDocs.length > 0) {
navData.push({
title: 'Using npm',
shortName: 'Using',
url: '/using-npm',
children: usingNpmDocs,
})
}
const prefix = `# This is the navigation for the documentation pages; it is not used
# directly within the CLI documentation. Instead, it will be used
# for the https://docs.npmjs.com/ site.
`
await fs.writeFile(navPath, `${prefix}\n${yaml.stringify(navData, { indent: 2, indentSeq: false })}`, 'utf-8')
}
// Auto-generate doc templates for commands without docs
const autoGenerateMissingDocs = async (contentPath, navPath, commandsPath = null) => {
commandsPath = commandsPath || join(__dirname, '../../lib/commands')
const docsCommandsPath = join(contentPath, 'commands')
// Get all commands from commandsPath directory
let commands
try {
const cmdListPath = join(commandsPath, '..', 'utils', 'cmd-list.js')
const cmdList = require(cmdListPath)
commands = cmdList.commands
} catch {
// Fall back to reading command files from commandsPath
const cmdFiles = await fs.readdir(commandsPath)
commands = cmdFiles
.filter(f => f.endsWith('.js'))
.map(f => basename(f, '.js'))
}
// Get existing doc files
const existingDocs = await fs.readdir(docsCommandsPath)
const documentedCommands = existingDocs
.filter(f => f.startsWith('npm-') && f.endsWith(DOC_EXT))
.map(f => f.replace('npm-', '').replace(DOC_EXT, ''))
// Find commands without docs
const missingDocs = commands.filter(cmd => !documentedCommands.includes(cmd))
// Generate docs for missing commands
const newEntries = []
for (const cmd of missingDocs) {
const Command = require(join(commandsPath, `${cmd}.js`))
const description = Command.description || `The ${cmd} command`
const docPath = join(docsCommandsPath, `npm-${cmd}${DOC_EXT}`)
const template = `---
title: npm-${cmd}
section: 1
description: ${description}
---
### Synopsis
<!-- AUTOGENERATED USAGE DESCRIPTIONS -->
### Description
${description}
### Configuration
<!-- AUTOGENERATED CONFIG DESCRIPTIONS -->
### See Also
* [npm help config](/commands/npm-config)
`
await fs.writeFile(docPath, template, 'utf-8')
// Track new entry for nav update
newEntries.push({
title: `npm ${cmd}`,
url: `/commands/npm-${cmd}`,
description,
})
}
// Update nav.yml if there are new entries
if (newEntries.length > 0) {
const navContent = await fs.readFile(navPath, 'utf-8')
const navData = yaml.parse(navContent)
// Find CLI Commands section
let commandsSection = navData.find(s => s.title === 'CLI Commands')
if (!commandsSection) {
// Create CLI Commands section if it doesn't exist
commandsSection = {
title: 'CLI Commands',
shortName: 'Commands',
url: '/commands',
children: [],
}
navData.unshift(commandsSection)
}
if (!commandsSection.children) {
commandsSection.children = []
}
// Add new entries that don't already exist
for (const entry of newEntries) {
const exists = commandsSection.children.some(c => c.url === entry.url)
if (!exists) {
commandsSection.children.push(entry)
}
}
// Sort children: npm first, then alphabetically, npx last
const npm = commandsSection.children.find(c => c.title === 'npm')
const npx = commandsSection.children.find(c => c.title === 'npx')
const others = commandsSection.children
.filter(c => c.title !== 'npm' && c.title !== 'npx')
.sort((a, b) => a.title.localeCompare(b.title))
commandsSection.children = [npm, ...others, npx].filter(Boolean)
// Write updated nav
const prefix = `# This is the navigation for the documentation pages; it is not used
# directly within the CLI documentation. Instead, it will be used
# for the https://docs.npmjs.com/ site.
`
await fs.writeFile(navPath, `${prefix}\n${yaml.stringify(navData, { indent: 2, indentSeq: false })}`, 'utf-8')
}
}
const mkDirs = async (paths) => {
const uniqDirs = [...new Set(paths.map((p) => dirname(p)))]
return Promise.all(uniqDirs.map((d) => fs.mkdir(d, { recursive: true })))
}
const rmAll = (...dirs) => Promise.all(dirs.map((d) => fs.rm(d, { recursive: true, force: true })))
const readDocs = (path) => ignoreWalk({ path }).then(ps => ps.filter(p => extname(p) === DOC_EXT))
const readMd = (path) => fs.readFile(path, 'utf-8').then(parseFrontMatter)
const readHtml = (path) => fs.readFile(path, 'utf-8')
const readYaml = (path) => fs.readFile(path, 'utf-8').then(yaml.parse)
const makeTransforms = (...args) => (src, trs) => trs.reduce((acc, tr) => tr(acc, ...args), src)
const pAll = async (obj) => {
const entries = Object.entries(obj)
const results = await Promise.all(entries.map(e => e[1]))
return results.reduce((acc, res, index) => {
acc[entries[index][0]] = res
return acc
}, {})
}
const run = async (opts) => {
const {
content, template, nav, man, html, md,
skipAutoGenerate, skipGenerateNav, commandLoader,
} = opts
// Auto-generate docs for commands without documentation
if (!skipAutoGenerate) {
await autoGenerateMissingDocs(content, nav)
}
// Generate nav.yml from filesystem
if (!skipGenerateNav) {
await generateNav(content, nav)
}
await rmAll(man, html, md)
const [contentPaths, navFile, options] = await Promise.all([
readDocs(content),
readYaml(nav),
pAll({
template: readHtml(template),
// these deps are esm only so we have to import them once we
// are inside our main async function
unified: import('unified').then(r => r.unified),
remarkParse: import('remark-parse').then(r => r.default),
remarkGfm: import('remark-gfm').then(r => r.default),
remarkRehype: import('remark-rehype').then(r => r.default),
rehypeStringify: import('rehype-stringify').then(r => r.default),
remarkMan: import('remark-man').then(r => r.default),
}),
])
const sources = await Promise.all(contentPaths.map(async (childPath) => {
const name = basename(childPath, DOC_EXT)
const fullPath = join(content, childPath)
const fullName = join(dirname(childPath), name).split(sep).join(posix.sep)
const { body, attributes: data, frontmatter } = await readMd(fullPath)
return {
body,
data,
frontmatter,
name,
fullName,
childPath,
}
}))
const entriesByType = sources.reduce((acc, {
body,
data,
frontmatter,
name,
childPath,
fullName,
}) => {
const applyTransforms = makeTransforms({
path: childPath,
commandLoader,
data: {
...data,
github_repo: 'npm/cli',
github_branch: 'latest',
github_path: 'docs/lib/content',
},
frontmatter,
...options,
})
const transformedSrc = applyTransforms(body, [
transform.version,
...(fullName.startsWith('commands/')
? [transform.usage, transform.params]
: []),
...(fullName === 'using-npm/config'
? [transform.shorthands, transform.config]
: []),
])
const aliases = [
fullName === 'configuring-npm/package-json' && 'configuring-npm/npm-json',
fullName === 'configuring-npm/folders' && 'configuring-npm/npm-global',
].filter(Boolean)
if (data.section) {
const manSource = applyTransforms(transformedSrc, [
transform.helpLinks,
transform.man,
])
// Man page aliases are only the basename since the man pages have no hierarchy
acc.man.push(...[name, ...aliases.map(a => basename(a))]
.map((p) => applyTransforms(p, [transform.manPath]))
.map((manPath) => ({
path: manPath,
fullPath: join(man, manPath),
src: manSource,
}))
)
}
// html docs are used for npm help on Windows
const htmlSource = applyTransforms(transformedSrc, [transform.html])
acc.html.push(...[fullName, ...aliases].map((htmlPath) => ({
path: `${htmlPath}.html`,
fullPath: join(html, `${htmlPath}.html`),
src: htmlSource,
})))
// Markdown pages don't get aliases here. These are used to build the website so any redirects
// for these pages are applied in npm/documentation. Not ideal but there are also many more
// redirects that we would never apply to man or html docs pages
const mdSource = applyTransforms(transformedSrc, [transform.md])
acc.md.push({
path: childPath,
fullPath: join(md, childPath),
src: mdSource,
})
return acc
}, { man: [], html: [], md: [] })
const docEntries = Object.values(entriesByType).flat()
await mkDirs(docEntries.map(({ fullPath }) => fullPath))
await Promise.all(docEntries.map(({ fullPath, src }) => fs.writeFile(fullPath, src, 'utf-8')))
checkNav(navFile, entriesByType.md.map(({ path }) => path), DOC_EXT)
return docEntries
}
module.exports = run
module.exports.generateNav = generateNav
module.exports.autoGenerateMissingDocs = autoGenerateMissingDocs