-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
executable file
·80 lines (70 loc) · 3.75 KB
/
index.mjs
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
#!/usr/bin/env node
import glob from 'glob'
import * as matter from 'gray-matter'
import fs from 'fs'
import minimist from 'minimist'
import { pathToFileURL } from 'url'
const options = minimist(process.argv.slice(2))
options.prefix = options.prefix || options.p
options.exclude = [].concat(options.exclude || [])
options.input = [].concat(options.input || []).concat([].concat(options.i || []))
options.export = [].concat(options.export || []).concat([].concat(options.x || []))
options.help = [].concat(options.help || []).concat([].concat(options.h || []))
if (!options.input.length && !options.help.length) throw new Error('No input glob specified.')
export function help () {
return `
Version: 0.0.5
Main: A tool designed to super-charge your code-base's documentation.
-i, --input <glob> Glob of files to parse. (You may use multiple -i flags)
-p, --prefix <prefix> Prefix to add to all titles.
-x, --export <module> A module used by doczen, executed left to right.
-h, --help <module> Gets help for the specified module.
-t <file> Uses an exported "transform" function to preprocess documentation before it is used.
--exclude <glob> Glob of files to ignore. (You may use multiple --exclude flags)
`.replace(/\n[ ]+/g, '\n')
}
async function main () {
if (options.help.length) {
console.log(help())
for (const item of options.help) {
if (typeof item !== 'string') continue
const defaultValue = () => `No help function available for "${item}".\n`
if (fs.existsSync(`./${item}.js`)) console.log((await (await import(pathToFileURL(`${item}.js`).pathname))?.help ?? defaultValue)())
else if (fs.existsSync(`./${item}.mjs`)) console.log((await (await import(pathToFileURL(`${item}.mjs`).pathname))?.help ?? defaultValue)())
else console.log((await (await import(`./${item}.mjs`))?.help ?? defaultValue)())
}
return
}
const files = options.input.flatMap(input => glob.sync(input, { ignore: options.exclude }))
const firstExportIndex = process.argv.findIndex(i => i === '-x')
const transform = process.argv.find((i, x) => (x < firstExportIndex || firstExportIndex === -1) && process.argv[x - 1] === '-t')
const transformFunc = transform ? (await import(pathToFileURL(transform).pathname)).transform : i => i
const results = files.map(file => {
const { data, content } = matter.default(fs.readFileSync(file, 'utf8'))
return transformFunc({
source: pathToFileURL(file).pathname,
relativeSource: file,
title: data.title,
content,
data
})
})
.filter(i => i && i.title)
.map(i => ({
...i,
title: options.prefix && i.title ? `${options.prefix}/${i.title}` : i.title
}))
for (const item of options.export) {
// find the item in the arguments
const index = process.argv.findIndex((i, x) => i === item && process.argv[x - 1] === '-x')
let nextIndex = process.argv.findIndex((i, x) => i === '-x' && x > index)
if (nextIndex === -1) nextIndex = process.argv.length
const exportOptions = minimist(process.argv.slice(index + 1, nextIndex))
const transformFunc = exportOptions.t ? (await import(pathToFileURL(exportOptions.t).pathname)).transform : i => i
const arr = results.map(i => transformFunc(i)).filter(i => i)
if (fs.existsSync(`./${item}.js`)) await (await import(pathToFileURL(`${item}.js`).pathname)).run(arr, exportOptions)
else if (fs.existsSync(`./${item}.mjs`)) await (await import(pathToFileURL(`${item}.mjs`).pathname)).run(arr, exportOptions)
else await (await import(`./${item}.mjs`)).run(arr, exportOptions)
}
}
main()