Skip to content

Commit

Permalink
fixup! chore: @npmcli/[email protected]
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Mar 30, 2022
1 parent b997953 commit db0f5aa
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 45 deletions.
70 changes: 32 additions & 38 deletions docs/bin/dockhand.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,33 @@ const mkdirp = require('mkdirp')
const jsdom = require('jsdom')
const npm = require('../../lib/npm.js')

const dir = __dirname
const docsRoot = path.resolve(dir, '..')
const inputRoot = path.join(docsRoot, 'content')
const outputRoot = path.join(docsRoot, 'output')
const run = async function (rootDir) {
const dir = (...p) => path.join(rootDir, '..', ...p)

const config = require(path.join(dir, 'config.json'))
const template = fs.readFileSync(path.join(dir, 'template.html')).toString()
const nav = yaml.parse(fs.readFileSync(path.join(docsRoot, 'nav.yml')).toString(), 'utf8')
const config = require(dir('bin', 'config.json'))
const template = fs.readFileSync(dir('bin', 'template.html'), 'utf-8')
const nav = yaml.parse(fs.readFileSync(dir('nav.yml'), 'utf-8'))

const run = async function () {
try {
const navPaths = await getNavigationPaths()
const fsPaths = await renderFilesystemPaths()

if (!ensureNavigationComplete(navPaths, fsPaths)) {
const navPaths = getNavigationPaths(nav)
const fsPaths = await renderFilesystemPaths({
input: dir('content'),
output: dir('output'),
config,
template,
})

const navErrors = ensureNavigationComplete(navPaths, fsPaths)
if (navErrors) {
console.error(navErrors)
process.exit(1)
}
} catch (error) {
console.error(error)
}
}

run()
run(__dirname)

function ensureNavigationComplete (navPaths, fsPaths) {
const unmatchedNav = {}
Expand All @@ -42,7 +46,8 @@ function ensureNavigationComplete (navPaths, fsPaths) {
}

for (let fsPath of fsPaths) {
fsPath = '/' + fsPath.replace(/\.md$/, '')
fsPath = path.sep + fsPath.replace(/\.md$/, '')
fsPath = fsPath.split(path.sep).join(path.posix.sep)

if (unmatchedNav[fsPath]) {
delete unmatchedNav[fsPath]
Expand All @@ -51,8 +56,9 @@ function ensureNavigationComplete (navPaths, fsPaths) {
}
}

const missingNav = Object.keys(unmatchedNav).sort()
const missingFs = Object.keys(unmatchedFs).sort()
const toKeys = (v) => Object.keys(v).sort().map((p) => p.split(path.posix.sep).join(path.sep))
const missingNav = toKeys(unmatchedNav)
const missingFs = toKeys(unmatchedFs)

if (missingNav.length > 0 || missingFs.length > 0) {
let message = 'Error: documentation navigation (nav.yml) does not match filesystem.\n'
Expand All @@ -75,24 +81,16 @@ function ensureNavigationComplete (navPaths, fsPaths) {

message += '\nUpdate nav.yml to ensure that all files are listed in the appropriate place.'

console.error(message)

return false
return message
}

return true
}

function getNavigationPaths () {
return walkNavigation(nav)
}

function walkNavigation (entries) {
function getNavigationPaths (entries) {
const paths = []

for (const entry of entries) {
if (entry.children) {
paths.push(...walkNavigation(entry.children))
paths.push(...getNavigationPaths(entry.children))
} else {
paths.push(entry.url)
}
Expand All @@ -101,33 +99,29 @@ function walkNavigation (entries) {
return paths
}

async function renderFilesystemPaths () {
return await walkFilesystem(inputRoot)
}

async function walkFilesystem (root, dirRelative) {
async function renderFilesystemPaths ({ input, output, ...opts }, dirRelative = null) {
const paths = []

const dirPath = dirRelative ? path.join(root, dirRelative) : root
const dirPath = dirRelative ? path.join(input, dirRelative) : input
const children = fs.readdirSync(dirPath)

for (const childFilename of children) {
const childRelative = dirRelative ? path.join(dirRelative, childFilename) : childFilename
const childPath = path.join(root, childRelative)
const childPath = path.join(input, childRelative)

if (fs.lstatSync(childPath).isDirectory()) {
paths.push(...(await walkFilesystem(root, childRelative)))
paths.push(...(await renderFilesystemPaths({ input, output, ...opts }, childRelative)))
} else {
await renderFile(childRelative)
await renderFile(input, output, childRelative, opts)
paths.push(childRelative)
}
}

return paths
}

async function renderFile (childPath) {
const inputPath = path.join(inputRoot, childPath)
async function renderFile (root, outputRoot, childPath, { template, config }) {
const inputPath = path.join(root, childPath)

if (!inputPath.match(/\.md$/)) {
console.log(`warning: unknown file type ${inputPath}, ignored`)
Expand Down
9 changes: 5 additions & 4 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"private": true,
"scripts": {
"build": "node bin/dockhand",
"prebuild": "node .. rebuild cmark-gfm --loglevel=silly",
"prebuild": "node .. rebuild cmark-gfm",
"lint": "eslint \"**/*.js\"",
"postlint": "template-oss-check",
"template-oss-apply": "template-oss-apply --force",
Expand Down Expand Up @@ -34,6 +34,7 @@
"yaml": "^1.10.0"
},
"author": "GitHub Inc.",
"license": "ISC",
"files": [
"bin/",
"content/",
Expand All @@ -43,10 +44,10 @@
"node": "^12.13.0 || ^14.15.0 || >=16.0.0"
},
"tap": {
"statements": 83,
"statements": 81,
"branches": 72,
"functions": 92,
"lines": 83
"functions": 80,
"lines": 81
},
"templateOSS": {
"//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",
Expand Down
15 changes: 12 additions & 3 deletions docs/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,26 @@ const { resolve, join } = require('path')

const cwd = resolve(__dirname, '..')
const output = join(cwd, 'output')
const cli = require.resolve(resolve(cwd, '..'))

// remove npm config when spawning so config set by test commands don't interfere
const env = Object.fromEntries(Object.entries(process.env).filter(([k]) => !k.startsWith('npm_')))
const env = Object.fromEntries(
Object.entries(process.env).filter(([k]) => !k.toLowerCase().startsWith('npm_config_'))
)

const rm = (d) => fs.rm(d, { recursive: true, force: true }).catch(() => {})

t.test('docs', async (t) => {
await rm(output)
t.teardown(() => rm(output))

await rm(output)
t.rejects(() => fs.stat(output))

const docs = await spawn('npm', ['run', 'build'], { cwd, env })
const docs = await spawn('node', [cli, 'run', 'build'], {
cwd,
env,
stdioString: true,
})

t.equal(docs.code, 0)
t.ok((await fs.stat(output)).isDirectory())
Expand Down
1 change: 1 addition & 0 deletions package-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
},
"docs": {
"version": "1.0.0",
"license": "ISC",
"devDependencies": {
"@mdx-js/mdx": "^1.6.22",
"@npmcli/eslint-config": "^3.0.1",
Expand Down

0 comments on commit db0f5aa

Please sign in to comment.