Skip to content

Commit

Permalink
snapshot test wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Oct 6, 2022
1 parent 2751b41 commit 5ed2bcc
Show file tree
Hide file tree
Showing 36 changed files with 2,635 additions and 5,298 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is automatically added by @npmcli/template-oss. Do not edit.

name: CI - docs
name: CI - @npmcli/docs

on:
workflow_dispatch:
Expand Down Expand Up @@ -119,7 +119,7 @@ jobs:
- name: Add Problem Matcher
run: echo "::add-matcher::.github/matchers/tap.json"
- name: Test
run: node . test --ignore-scripts -w docs
run: node . test --ignore-scripts -w @npmcli/docs
- name: Check Git Status
if: matrix && matrix.platform.os != 'windows-latest'
run: node scripts/git-dirty.js
38 changes: 22 additions & 16 deletions DEPENDENCIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ graph LR;
npm-->npmcli-arborist["@npmcli/arborist"];
npm-->npmcli-ci-detect["@npmcli/ci-detect"];
npm-->npmcli-config["@npmcli/config"];
npm-->npmcli-docs["@npmcli/docs"];
npm-->npmcli-eslint-config["@npmcli/eslint-config"];
npm-->npmcli-fs["@npmcli/fs"];
npm-->npmcli-git["@npmcli/git"];
Expand Down Expand Up @@ -192,6 +193,10 @@ graph LR;
npmcli-config-->proc-log;
npmcli-config-->read-package-json-fast;
npmcli-config-->semver;
npmcli-docs-->ignore-walk;
npmcli-docs-->npmcli-eslint-config["@npmcli/eslint-config"];
npmcli-docs-->npmcli-fs["@npmcli/fs"];
npmcli-docs-->npmcli-template-oss["@npmcli/template-oss"];
npmcli-fs-->semver;
npmcli-git-->npm-pick-manifest;
npmcli-git-->npmcli-promise-spawn["@npmcli/promise-spawn"];
Expand Down Expand Up @@ -387,18 +392,6 @@ graph LR;
detab-->repeat-string;
dezalgo-->asap;
dezalgo-->wrappy;
docs-->cmark-gfm;
docs-->front-matter;
docs-->ignore-walk;
docs-->jsdom;
docs-->marked-man;
docs-->mdx-js-mdx["@mdx-js/mdx"];
docs-->mkdirp;
docs-->npmcli-eslint-config["@npmcli/eslint-config"];
docs-->npmcli-fs["@npmcli/fs"];
docs-->npmcli-template-oss["@npmcli/template-oss"];
docs-->tap;
docs-->yaml;
domexception-->webidl-conversions;
encoding-->iconv-lite;
end-of-stream-->once;
Expand Down Expand Up @@ -700,7 +693,6 @@ graph LR;
npm-->cli-columns;
npm-->cli-table3;
npm-->columnify;
npm-->docs;
npm-->fastest-levenshtein;
npm-->fs-minipass;
npm-->glob;
Expand Down Expand Up @@ -744,6 +736,7 @@ graph LR;
npm-->npmcli-arborist["@npmcli/arborist"];
npm-->npmcli-ci-detect["@npmcli/ci-detect"];
npm-->npmcli-config["@npmcli/config"];
npm-->npmcli-docs["@npmcli/docs"];
npm-->npmcli-eslint-config["@npmcli/eslint-config"];
npm-->npmcli-fs["@npmcli/fs"];
npm-->npmcli-git["@npmcli/git"];
Expand Down Expand Up @@ -850,6 +843,19 @@ graph LR;
npmcli-config-->semver;
npmcli-config-->walk-up-path;
npmcli-disparity-colors-->ansi-styles;
npmcli-docs-->cmark-gfm;
npmcli-docs-->front-matter;
npmcli-docs-->ignore-walk;
npmcli-docs-->isaacs-string-locale-compare["@isaacs/string-locale-compare"];
npmcli-docs-->jsdom;
npmcli-docs-->marked-man;
npmcli-docs-->mdx-js-mdx["@mdx-js/mdx"];
npmcli-docs-->mkdirp;
npmcli-docs-->npmcli-eslint-config["@npmcli/eslint-config"];
npmcli-docs-->npmcli-fs["@npmcli/fs"];
npmcli-docs-->npmcli-template-oss["@npmcli/template-oss"];
npmcli-docs-->tap;
npmcli-docs-->yaml;
npmcli-fs-->gar-promisify["@gar/promisify"];
npmcli-fs-->semver;
npmcli-git-->lru-cache;
Expand Down Expand Up @@ -1105,6 +1111,6 @@ packages higher up the chain.
- pacote, libnpmaccess, libnpmhook, libnpmorg, libnpmsearch, libnpmteam, npm-profile
- npm-registry-fetch, libnpmversion
- @npmcli/git, make-fetch-happen, @npmcli/config, init-package-json
- @npmcli/installed-package-contents, @npmcli/map-workspaces, cacache, npm-pick-manifest, @npmcli/run-script, read-package-json, @npmcli/query, readdir-scoped-modules, promzard
- npm-bundled, read-package-json-fast, @npmcli/fs, unique-filename, @npmcli/promise-spawn, npm-install-checks, npm-package-arg, npm-packlist, normalize-package-data, @npmcli/package-json, bin-links, nopt, npmlog, parse-conflict-json, dezalgo, read
- npm-normalize-package-bin, @npmcli/name-from-folder, json-parse-even-better-errors, semver, @npmcli/move-file, fs-minipass, infer-owner, ssri, unique-slug, hosted-git-info, proc-log, validate-npm-package-name, @npmcli/node-gyp, ignore-walk, minipass-fetch, cmd-shim, read-cmd-shim, write-file-atomic, abbrev, are-we-there-yet, gauge, wrappy, treeverse, @npmcli/eslint-config, @npmcli/template-oss, minify-registry-metadata, @npmcli/disparity-colors, @npmcli/ci-detect, mute-stream, ini, npm-audit-report, npm-user-validate
- @npmcli/docs, @npmcli/installed-package-contents, @npmcli/map-workspaces, cacache, npm-pick-manifest, @npmcli/run-script, read-package-json, @npmcli/query, readdir-scoped-modules, promzard
- @npmcli/fs, npm-bundled, read-package-json-fast, unique-filename, @npmcli/promise-spawn, npm-install-checks, npm-package-arg, npm-packlist, normalize-package-data, @npmcli/package-json, bin-links, nopt, npmlog, parse-conflict-json, dezalgo, read
- semver, ignore-walk, @npmcli/eslint-config, @npmcli/template-oss, npm-normalize-package-bin, @npmcli/name-from-folder, json-parse-even-better-errors, @npmcli/move-file, fs-minipass, infer-owner, ssri, unique-slug, hosted-git-info, proc-log, validate-npm-package-name, @npmcli/node-gyp, minipass-fetch, cmd-shim, read-cmd-shim, write-file-atomic, abbrev, are-we-there-yet, gauge, wrappy, treeverse, minify-registry-metadata, @npmcli/disparity-colors, @npmcli/ci-detect, mute-stream, ini, npm-audit-report, npm-user-validate
1 change: 0 additions & 1 deletion docs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
!/lib/
!/LICENSE*
!/map.js
!/nav.yml
!/package.json
!/README*
!/scripts/
Expand Down
11 changes: 2 additions & 9 deletions docs/bin/build.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
const { resolve, join } = require('path')
const run = require('../lib/build.js')

const rootDir = resolve(__dirname, '..')
const { paths } = require('../lib/index')

run({
verify: true,
content: join(rootDir, 'lib', 'content'),
template: join(rootDir, 'lib', 'template.html'),
nav: join(rootDir, 'nav.yml'),
man: resolve(rootDir, '..', 'man'),
html: join(rootDir, 'output'),
md: join(rootDir, 'content'),
...paths,
})
.then((res) => console.log(`Wrote ${res.length} files`))
.catch((err) => {
Expand Down
6 changes: 1 addition & 5 deletions docs/lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ const parseFrontMatter = require('front-matter')
const mdx = require('@mdx-js/mdx')

const checkNav = require('./check-nav.js')
const transform = require('./transform.js')

const DOC_EXT = '.md'
const { DOC_EXT, ...transform } = require('./index.js')

const mkDirs = async (paths) => {
const uniqDirs = [...new Set(paths.map((p) => dirname(p)))]
Expand Down Expand Up @@ -63,7 +61,6 @@ const run = async ({ content, verify, template, nav, man, html, md }) => {
github_branch: 'latest',
github_path: 'docs/content',
},
docExt: DOC_EXT,
frontmatter,
template: templateFile,
})
Expand All @@ -80,7 +77,6 @@ const run = async ({ content, verify, template, nav, man, html, md }) => {

if (data.section) {
const manSrc = applyTransforms(transformedSrc, [
transform.placeholders,
transform.helpLinks,
transform.man,
])
Expand Down
File renamed without changes.
169 changes: 169 additions & 0 deletions docs/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
const marked = require('marked-man')
const localeCompare = require('@isaacs/string-locale-compare')('en')
const { join, basename, resolve } = require('path')
const transformHTML = require('./transform-html.js')
const { version } = require('../../lib/npm.js')
const { aliases } = require('../../lib/utils/cmd-list')
const { shorthands, definitions } = require('../../lib/utils/config/index.js')

const DOC_EXT = '.md'

const TAGS = {
CONFIG: '<!-- AUTOGENERATED CONFIG DESCRIPTIONS -->',
USAGE: '<!-- AUTOGENERATED USAGE DESCRIPTIONS -->',
SHORTHANDS: '<!-- AUTOGENERATED CONFIG SHORTHANDS -->',
}

const assertPlaceholder = (src, path, placeholder) => {
if (!src.includes(placeholder)) {
throw new Error(
`Cannot replace ${placeholder} in ${path} due to missing placeholder`
)
}
return placeholder
}

const getCommandByDoc = (docFile, docExt) => {
// Grab the command name from the *.md filename
// NOTE: We cannot use the name property command file because in the case of
// `npx` the file being used is `lib/commands/exec.js`
const name = basename(docFile, docExt).replace('npm-', '')

if (name === 'npm') {
return {
name,
params: null,
usage: 'npm',
}
}

// special case for `npx`:
// `npx` is not technically a command in and of itself,
// so it just needs the usage of npm exex
const srcName = name === 'npx' ? 'exec' : name
const { params, usage = [''] } = require(`../../lib/commands/${srcName}`)
const usagePrefix = name === 'npx' ? 'npx' : `npm ${name}`

return {
name,
params: name === 'npx' ? null : params,
usage: usage.map(u => `${usagePrefix} ${u}`.trim()).join('\n'),
}
}

const replaceVersion = (src) => src.replace(/@VERSION@/g, version)

const replaceUsage = (src, { path }) => {
const replacer = assertPlaceholder(src, path, TAGS.USAGE)
const { usage, name } = getCommandByDoc(path, DOC_EXT)

const synopsis = ['```bash', usage]

const cmdAliases = Object.keys(aliases).reduce((p, c) => {
if (aliases[c] === name) {
p.push(c)
}
return p
}, [])

if (cmdAliases.length === 1) {
synopsis.push('')
synopsis.push(`alias: ${cmdAliases[0]}`)
} else if (cmdAliases.length > 1) {
synopsis.push('')
synopsis.push(`aliases: ${cmdAliases.join(', ')}`)
}

synopsis.push('```')

return src.replace(replacer, synopsis.join('\n'))
}

const replaceParams = (src, { path }) => {
const { params } = getCommandByDoc(path, DOC_EXT)
const replacer = params && assertPlaceholder(src, path, TAGS.CONFIG)

if (!params) {
return src
}

const paramsConfig = params.map((n) => definitions[n].describe())

return src.replace(replacer, paramsConfig.join('\n\n'))
}

const replaceConfig = (src, { path }) => {
const replacer = assertPlaceholder(src, path, TAGS.CONFIG)

// sort not-deprecated ones to the top
/* istanbul ignore next - typically already sorted in the definitions file,
* but this is here so that our help doc will stay consistent if we decide
* to move them around. */
const sort = ([keya, { deprecated: depa }], [keyb, { deprecated: depb }]) => {
return depa && !depb ? 1
: !depa && depb ? -1
: localeCompare(keya, keyb)
}

const allConfig = Object.entries(definitions).sort(sort)
.map(([_, def]) => def.describe())
.join('\n\n')

return src.replace(replacer, allConfig)
}

const replaceShorthands = (src, { path }) => {
const replacer = assertPlaceholder(src, path, TAGS.SHORTHANDS)

const sh = Object.entries(shorthands)
.sort(([shorta, expansiona], [shortb, expansionb]) =>
// sort by what they're short FOR
localeCompare(expansiona.join(' '), expansionb.join(' ')) || localeCompare(shorta, shortb)
)
.map(([short, expansion]) => {
// XXX: this is incorrect. we have multicharacter flags like `-iwr` that
// can only be set with a single dash
const dash = short.length === 1 ? '-' : '--'
return `* \`${dash}${short}\`: \`${expansion.join(' ')}\``
})

return src.replace(replacer, sh.join('\n'))
}

const replaceHelpLinks = (src) => {
const replacer = (_, p1) => 'npm help ' + p1.replace(/npm /, '')
return src
.replace(/\[([^\]]+)\]\(\/commands\/([^)]+)\)/g, replacer)
.replace(/\[([^\]]+)\]\(\/configuring-npm\/([^)]+)\)/g, replacer)
.replace(/\[([^\]]+)\]\(\/using-npm\/([^)]+)\)/g, replacer)
}

const transformMan = (src, { data }) =>
marked(`# ${data.title}(${data.section}) - ${data.description}\n\n${src}`)

const manPath = (name, { data }) => join(`man${data.section}`, `${name}.${data.section}`)

const transformMd = (src, { frontmatter }) => ['---', frontmatter, '---', '', src].join('\n')

module.exports = {
DOC_EXT,
TAGS,
paths: {
content: resolve(__dirname, 'content'),
nav: resolve(__dirname, 'content', 'nav.yml'),
template: resolve(__dirname, 'template.html'),
man: resolve(__dirname, '..', '..', 'man'),
html: resolve(__dirname, '..', 'output'),
md: resolve(__dirname, '..', 'content'),
},
usage: replaceUsage,
params: replaceParams,
config: replaceConfig,
shorthands: replaceShorthands,
version: replaceVersion,
helpLinks: replaceHelpLinks,
man: transformMan,
manPath: manPath,
md: transformMd,
html: transformHTML,
}
44 changes: 0 additions & 44 deletions docs/lib/npm.js

This file was deleted.

Loading

0 comments on commit 5ed2bcc

Please sign in to comment.