forked from adr/adr-log
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·87 lines (76 loc) · 2.73 KB
/
cli.js
File metadata and controls
executable file
·87 lines (76 loc) · 2.73 KB
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
#!/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'],
alias: {h: 'help'}
});
if (!args.d && !args.i && (args._.length==0) || args.h) {
process.stderr.write([
'Usage: adr-log [-d <directory>] [-i] <input>',
'',
' 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.)',
'',
' -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 defaultAdrLogFile = 'index.md';
var adrLogFile = args._[0] || adrLogDir + '/' + defaultAdrLogFile;
var adrLogFileName = require('path').parse(adrLogFile).base;
console.log("adr log file:", adrLogFile);
console.log("adr log dir:", adrLogDir);
var headings = '';
const globPattern = '!(' + adrLogFileName.slice(0,-3) + '*).md';
console.log("glob pattern:", globPattern);
var filenames = glob.sync(globPattern, {cwd: adrLogDir});
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, adrLogDir);
if (args.i) {
fs.writeFileSync(adrLogFile, newLogString);
} else {
process.stdout.write(newLogString);
}