forked from adr/adr-log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·108 lines (97 loc) · 3.54 KB
/
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
#!/usr/bin/env node
require('console-stamp')(console, {
pattern: 'HH:MM:ss',
include: ['log', 'info', 'warn', 'error', 'fatal'],
//level: 'log', // development
level: 'info', // production
colors: {
stamp: 'yellow',
label: 'yellow',
metadata: 'green'
}
});
const os = require('os');
var fs = require('fs');
var toc = require('./index.js');
var utils = require('./lib/utils');
var glob = require('glob');
var path = require('path');
var args = utils.minimist(process.argv.slice(2), {
boolean: ['i'],
string: ['d'],
string: ['e'],
string: ['p'],
string: ['b'],
alias: {h: 'help'}
});
if (!args.d && !args.i && !args.p && (args._.length==0) || args.h) {
process.stderr.write([
'Usage: adr-log [-d <directory>] [-i <input>] [-p <path_prefix>]',
'',
' input: The markdown file to contain the table of contents.',
' If no <input> file is specified, an index.md file containing the log is created in the current directory.',
'',
' -i: Edit the <input> file directly, injecting the log at <!-- adrlog -->.',
' Using only the -i flag, the tool will scan the current working directory for all *.md files and',
' inject the resulting adr-log into the default index.md file.',
' (Without this flag, the default is to print the log to stdout.)',
'',
' -d: Scans the given <directory> for .md files.',
' (Without this flag, the current working directory is chosen as default.)',
'',
' -e Exclude any files matching the given <pattern>',
'',
' -p: Path prefix for each ADR file path written in log',
' (Default is empty)',
'',
' -b Change the character used to for bullets',
' Supports: asterisk, dash, plus',
' (Default is asterisk)',
'',
' -h: Shows how to use this program',
''
].join(os.EOL));
process.exit(1);
}
if (args.i && args._[0] === '-') {
process.stderr.write('adr-log: you cannot use -i with "-" (stdin) for input');
process.exit(1);
}
var defaultAdrLogDir = path.resolve(process.cwd());
var adrLogDir = args.d || defaultAdrLogDir;
var adrPathPrefix = args.p || '';
var adrBulletStyle = args.b || '';
var defaultAdrLogFile = 'index.md';
var adrLogFile = args._[0] || adrLogDir + '/' + defaultAdrLogFile;
var adrLogFileName = path.parse(adrLogFile).base;
var tocDir = path.dirname(adrLogFile);
console.log("adr log file:", adrLogFile);
console.log("adr log dir:", adrLogDir);
var headings = '';
const globPattern = '**/*.md';
console.log("glob pattern:", globPattern);
var filenames = glob.sync(globPattern, {cwd: adrLogDir});
filenames = filenames.filter(filename => filename !== adrLogFile.replace(adrLogDir + '/', ''));
if (args.e) {
var excluded = glob.sync(args.e, {cwd: adrLogDir});
console.log("excluded: ", excluded);
filenames = filenames.filter(filename => !excluded.includes(filename));
}
console.log("filenames:", filenames);
// determine log entries
for (const filename of filenames) {
console.log("add filename:", filename);
headings += utils.headify(filename + '\n');
}
var existingLogString;
if (fs.existsSync(adrLogFile)) {
existingLogString = fs.readFileSync(adrLogFile, 'utf8');
} else {
existingLogString = '<!-- adrlog -->' + os.EOL + os.EOL + '<!-- adrlogstop -->' + os.EOL;
}
var newLogString = toc.insertAdrToc(existingLogString, headings, {pathPrefix: adrPathPrefix, dir: adrLogDir, bulletStyle: adrBulletStyle, tocDir});
if (args.i) {
fs.writeFileSync(adrLogFile, newLogString);
} else {
process.stdout.write(newLogString);
}