Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions docs/lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,109 @@ const parseFrontMatter = require('front-matter')
const checkNav = require('./check-nav.js')
const { DOC_EXT, ...transform } = require('./index.js')

// Auto-generate doc templates for commands without docs
const autoGenerateMissingDocs = async (contentPath, navPath) => {
const commandsPath = join(__dirname, '../../lib/commands')
const docsCommandsPath = join(contentPath, 'commands')

// Get all command files
const commandFiles = await fs.readdir(commandsPath)
const commands = commandFiles
.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
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')
}

// Update nav.yml if there are new commands
if (missingDocs.length > 0) {
const navContent = await fs.readFile(navPath, 'utf-8')
const navData = yaml.parse(navContent)

// Find the CLI Commands section
const commandsSection = navData.find(section => section.title === 'CLI Commands')
if (commandsSection && commandsSection.children) {
// Get existing command entries
const existingEntries = new Set(
commandsSection.children
.map(child => child.url?.replace('/commands/npm-', ''))
.filter(Boolean)
)

// Add missing commands to the children array
for (const cmd of missingDocs) {
if (!existingEntries.has(cmd)) {
const Command = require(join(commandsPath, `${cmd}.js`))
const description = Command.description || `The ${cmd} command`

commandsSection.children.push({
title: `npm ${cmd}`,
url: `/commands/npm-${cmd}`,
description: description,
})
}
}

// Sort children alphabetically by title
commandsSection.children.sort((a, b) => {
if (a.title === 'npm') {
return -1
}
if (b.title === 'npm') {
return 1
}
return a.title.localeCompare(b.title)
})

// Write updated nav.yml
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\n${yaml.stringify(navData)}`, '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 })))
Expand All @@ -29,6 +132,9 @@ const pAll = async (obj) => {
}

const run = async ({ content, template, nav, man, html, md }) => {
// Auto-generate docs for commands without documentation
await autoGenerateMissingDocs(content, nav)

await rmAll(man, html, md)
const [contentPaths, navFile, options] = await Promise.all([
readDocs(content),
Expand Down
21 changes: 21 additions & 0 deletions docs/lib/content/commands/npm-get.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: npm-get
section: 1
description: Get a value from the npm configuration
---

### Synopsis

<!-- AUTOGENERATED USAGE DESCRIPTIONS -->

### Description

Get a value from the npm configuration

### Configuration

<!-- AUTOGENERATED CONFIG DESCRIPTIONS -->

### See Also

* [npm help config](/commands/npm-config)
21 changes: 21 additions & 0 deletions docs/lib/content/commands/npm-ll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: npm-ll
section: 1
description: List installed packages
---

### Synopsis

<!-- AUTOGENERATED USAGE DESCRIPTIONS -->

### Description

List installed packages

### Configuration

<!-- AUTOGENERATED CONFIG DESCRIPTIONS -->

### See Also

* [npm help config](/commands/npm-config)
21 changes: 21 additions & 0 deletions docs/lib/content/commands/npm-set.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: npm-set
section: 1
description: Set a value in the npm configuration
---

### Synopsis

<!-- AUTOGENERATED USAGE DESCRIPTIONS -->

### Description

Set a value in the npm configuration

### Configuration

<!-- AUTOGENERATED CONFIG DESCRIPTIONS -->

### See Also

* [npm help config](/commands/npm-config)
Loading
Loading