-
Notifications
You must be signed in to change notification settings - Fork 4
/
ploc-cli.js
executable file
·122 lines (112 loc) · 4.18 KB
/
ploc-cli.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env node
var fs = require('fs');
var glob = require('glob');
var ploc = require('./ploc.js');
// Util for parsing arguments: We reuse and extend ploc attributes here.
ploc.utils.parseCliArgs = require('minimist');
ploc.opts.cliArgs = {
string: ["in", "out", "tocStyles"],
boolean: ["help", "autoHeaderIds"],
alias: {
i: "in",
o: "out",
t: "toc",
h: "help",
d: "debug"
},
default: { in: "**/*.pks",
out: "{folder}{file}.md",
toc: ploc.opts.minItemsForToc
}
};
// cli help text.
ploc.opts.cliHelp = [
'',
'Usage: ploc [options]',
'',
'-i, --in: The glob pattern for the code files to read.',
' (default is "' + ploc.opts.cliArgs.default.in + '")',
'',
'-o, --out: The pattern for the doc files to write.',
' (default is "' + ploc.opts.cliArgs.default.out + '")',
' {folder} = in file path with trailing directory separator',
' {file} = in file name without extension',
'',
'-t, --toc: How many items (methods including object/package name) the',
' code must have before a TOC is included.',
' (default is ' + ploc.opts.cliArgs.default.toc + ')',
'',
'--tocStyles: Inline styles to use for the TOC. If provided, the TOC',
' is generated as a HTML unordered list instead of a',
' Markdown list to be able to use the styles.',
'',
'--autoHeaderIds: Boolean - if present the headers are generated in HTML',
' format instead of Markdown to be able to integrate the IDs.',
'',
'-h, --help: Command line help.',
'',
'-d, --debug: Write CLI arguments to console.',
'',
'Example 1: npx ploc --in "**/*.pks" --out {folder}{file}.md',
'Example 2: npx ploc --out docs/{file}.md',
'Example 3: npx ploc -i "**/*.*(pks|sql)" -o docs/{file}.md -t 5',
'Example 4: npx ploc --in "src/*.pks" --out docs/{file}.md --autoHeaderIds --tocStyles "float: right;"',
'https://blog.npmjs.org/post/162869356040/introducing-npx-an-npm-package-runner',
'',
].join('\n');
// Utility to calculate one out file path from an in file path and an out file pattern.
ploc.utils.getOutFilePath = function (inFilePath, outFilePattern) {
var folder, file, match;
var regexp = /(.*(?:\\|\/)+)?((.*)(\.([^?\s]*)))\??(.*)?/i;
/*
This regex is taken from https://regexr.com/3dns9 and splits a URL it its components:
- $1: folder path
- $2: file name(including extension)
- $3: file name without extension
- $4: extension
- $5: extension without dot sign
- $6: variables
*/
// Extract folder and file from inFilePath for replacements of {folder} and {file} in outFilePattern.
match = inFilePath.match(regexp);
folder = match[1] || '';
file = match[3];
// Do the final replacements and return.
return outFilePattern.replace('{folder}', folder).replace('{file}', file);
}
// Utility to process all files for the provided in and out file patterns.
ploc.utils.files2docs = function (inFilePattern, outFilePattern) {
var outFilePath;
var options = {
matchBase: false
};
glob(inFilePattern, options, function (err, files) {
if (err) throw err;
files.forEach(function (inFilePath) {
outFilePath = ploc.utils.getOutFilePath(inFilePath, outFilePattern);
console.log(inFilePath + ' => ' + outFilePath);
fs.writeFileSync(
outFilePath,
'<!-- DO NOT EDIT THIS FILE DIRECTLY - it is generated from source file ' + inFilePath + ' -->\n' +
'<!-- markdownlint-disable MD003 MD012 MD024 MD033 -->\n\n' +
ploc.getDoc(fs.readFileSync(inFilePath, 'utf8'))
);
});
})
};
// Parse cli arguments.
var args = ploc.utils.parseCliArgs(process.argv.slice(2), ploc.opts.cliArgs);
// Save args for TOC as these are used internally by ploc.getDoc.
ploc.opts.autoHeaderIds = args.autoHeaderIds;
ploc.opts.minItemsForToc = args.toc;
ploc.opts.tocStyles = args.tocStyles;
// Print help, if options -h or --help were provided.
if (args.help) {
console.log(ploc.opts.cliHelp);
} else {
// Otherwise create the documents.
if (args.debug) {
console.log("CLI arguments:\n", args);
}
ploc.utils.files2docs(args.in, args.out)
}