diff options
author | Luke Karrys <luke@lukekarrys.com> | 2022-03-30 18:32:22 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-30 18:32:22 +0300 |
commit | a59fd2cb863245fce56f96c90ac854e62c5c4d6f (patch) | |
tree | f1421497144abae5a2d0190f9d06adc3e4b5da36 /docs | |
parent | cc0a2ec9999b956ea654deaf68fd49ae4bf1a1c0 (diff) |
deps: @npmcli/template-oss@3.2.2 (#4639)
- add some basic tests for docs
- make dockhand script work on windows
- cleanup main CI to match template-oss
- add a git status check for cli ci tests
- use resetdeps for ci steps
Diffstat (limited to 'docs')
-rw-r--r-- | docs/.gitignore | 4 | ||||
-rw-r--r-- | docs/bin/config.json | 5 | ||||
-rw-r--r-- | docs/bin/dockhand.js (renamed from docs/dockhand.js) | 78 | ||||
-rw-r--r-- | docs/bin/template.html (renamed from docs/template.html) | 0 | ||||
-rw-r--r-- | docs/config.json | 5 | ||||
-rw-r--r-- | docs/package.json | 36 | ||||
-rw-r--r-- | docs/test/index.js | 36 |
7 files changed, 97 insertions, 67 deletions
diff --git a/docs/.gitignore b/docs/.gitignore index 27a0f6c59..823857bf9 100644 --- a/docs/.gitignore +++ b/docs/.gitignore @@ -16,9 +16,7 @@ !/CHANGELOG* !/.eslintrc.js !/.gitignore -!/config.json +!/bin/ !/content/ -!/dockhand.js !/nav.yml !/package.json -!/template.html diff --git a/docs/bin/config.json b/docs/bin/config.json new file mode 100644 index 000000000..c9f183e4e --- /dev/null +++ b/docs/bin/config.json @@ -0,0 +1,5 @@ +{ + "github_repo": "npm/cli", + "github_branch": "latest", + "github_path": "docs/content" +} diff --git a/docs/dockhand.js b/docs/bin/dockhand.js index d1e64f935..4b359e1b6 100644 --- a/docs/dockhand.js +++ b/docs/bin/dockhand.js @@ -7,22 +7,27 @@ const cmark = require('cmark-gfm') const mdx = require('@mdx-js/mdx') const mkdirp = require('mkdirp') const jsdom = require('jsdom') -const npm = require('../lib/npm.js') +const npm = require('../../lib/npm.js') -const config = require('./config.json') +const run = async function (rootDir) { + const dir = (...p) => path.join(rootDir, '..', ...p) -const docsRoot = __dirname -const inputRoot = path.join(docsRoot, 'content') -const outputRoot = path.join(docsRoot, 'output') + 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 template = fs.readFileSync('template.html').toString() - -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) { @@ -30,7 +35,7 @@ const run = async function () { } } -run() +run(__dirname) function ensureNavigationComplete (navPaths, fsPaths) { const unmatchedNav = {} @@ -41,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] @@ -50,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' @@ -59,8 +66,8 @@ function ensureNavigationComplete (navPaths, fsPaths) { if (missingNav.length > 0) { message += '\nThe following path(s) exist on disk but are not present in nav.yml:\n\n' - for (const nav of missingNav) { - message += ` ${nav}\n` + for (const n of missingNav) { + message += ` ${n}\n` } } @@ -74,27 +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 () { - const navFilename = path.join(docsRoot, 'nav.yml') - const nav = yaml.parse(fs.readFileSync(navFilename).toString(), 'utf8') - - 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) } @@ -103,24 +99,20 @@ 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) } } @@ -128,8 +120,8 @@ async function walkFilesystem (root, dirRelative) { 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`) diff --git a/docs/template.html b/docs/bin/template.html index be3bafd61..be3bafd61 100644 --- a/docs/template.html +++ b/docs/bin/template.html diff --git a/docs/config.json b/docs/config.json deleted file mode 100644 index 08df95ae4..000000000 --- a/docs/config.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "github_repo": "npm/cli", - "github_branch": "latest", - "github_path": "docs/content" -} diff --git a/docs/package.json b/docs/package.json index 85a14a269..9bd06f094 100644 --- a/docs/package.json +++ b/docs/package.json @@ -4,14 +4,13 @@ "version": "1.0.0", "private": true, "scripts": { - "build": "node dockhand", + "build": "node bin/dockhand", "lint": "eslint \"**/*.js\"", "postlint": "template-oss-check", "template-oss-apply": "template-oss-apply --force", "lintfix": "npm run lint -- --fix", "preversion": "npm test", - "postversion": "npm publish", - "prepublishOnly": "git push origin --follow-tags", + "postversion": "git push origin --follow-tags", "snap": "tap", "test": "tap", "posttest": "npm run lint" @@ -24,7 +23,9 @@ "devDependencies": { "@mdx-js/mdx": "^1.6.22", "@npmcli/eslint-config": "^3.0.1", - "@npmcli/template-oss": "3.2.1", + "@npmcli/fs": "^2.1.0", + "@npmcli/promise-spawn": "^1.3.2", + "@npmcli/template-oss": "3.2.2", "cmark-gfm": "^0.9.0", "jsdom": "^18.1.0", "marked-man": "^0.7.0", @@ -32,26 +33,31 @@ "yaml": "^1.10.0" }, "author": "GitHub Inc.", + "license": "ISC", "files": [ + "bin/", "content/", - "config.json", - "dockhand.js", - "nav.yml", - "template.html" + "nav.yml" ], "engines": { - "node": "^12.13.0 || ^14.15.0 || >=16.0.0" + "node": ">=16.0.0" + }, + "tap": { + "statements": 81, + "branches": 72, + "functions": 80, + "lines": 81 }, "templateOSS": { "//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.", - "workspaceRepo": false, "distPaths": [ + "bin/", "content/", - "config.json", - "dockhand.js", - "nav.yml", - "template.html" + "nav.yml" + ], + "ciVersions": [ + "16" ], - "version": "3.2.1" + "version": "3.2.2" } } diff --git a/docs/test/index.js b/docs/test/index.js index ee0fee2b7..fab9748a3 100644 --- a/docs/test/index.js +++ b/docs/test/index.js @@ -1,3 +1,37 @@ const t = require('tap') +const spawn = require('@npmcli/promise-spawn') +const fs = require('@npmcli/fs') +const { resolve, join } = require('path') +const which = require('which').sync -t.ok(1) +const cwd = resolve(__dirname, '..') +const output = join(cwd, 'output') + +const rmOutput = () => fs.rm(output, { recursive: true, force: true }).catch(() => {}) + +const spawnNpm = (...args) => { + // remove npm config when spawning so config set by test commands don't interfere + const env = Object.entries(process.env) + .filter(([k]) => k.toLowerCase() !== 'npm_config_ignore_scripts') + + return spawn(which('npm'), args, { + env: Object.fromEntries(env), + stdioString: true, + cwd, + }) +} + +t.before(() => spawnNpm('rebuild', 'cmark-gfm')) +t.beforeEach(() => rmOutput()) + +t.test('docs', async (t) => { + t.rejects(() => fs.stat(output)) + const docs = await spawnNpm('run', 'build') + t.equal(docs.code, 0) + t.ok((await fs.stat(output)).isDirectory()) +}) + +t.test('files used by documention repo', async (t) => { + t.ok((await fs.stat(join(cwd, 'content'))).isDirectory()) + t.ok((await fs.stat(join(cwd, 'nav.yml'))).isFile()) +}) |