1
|
|
|
const processFile = require('./processFile.js').default |
2
|
|
|
const glob = require('glob-gitignore').glob |
3
|
|
|
const ignore = require('ignore') |
4
|
|
|
const chalk = require('chalk') |
5
|
|
|
const debug = require('debug')('mdprepare') |
6
|
|
|
const fs = require('fs') |
7
|
|
|
const minimist = require('minimist') |
8
|
|
|
|
9
|
|
|
export function cli (args) { |
10
|
|
|
const argv = minimist(args.slice(2), {'boolean': ['clear', 'help']}) |
11
|
|
|
debug('arguments from minimist:', argv) |
12
|
|
|
|
13
|
|
|
if (argv.help) { |
14
|
|
|
printHelp() |
15
|
|
|
return |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
let ig = ignore() |
19
|
|
|
try { |
20
|
|
|
ig.add(fs.readFileSync('./.gitignore').toString()) |
21
|
|
|
} catch (err) { |
22
|
|
|
// nothing to do if no .gitignore is present |
23
|
|
|
} |
24
|
|
|
ig.add('node_modules') |
25
|
|
|
if (argv.hasOwnProperty('ignore')) { ig.add(argv.ignore) } |
26
|
|
|
|
27
|
|
|
let options = {cwd: process.cwd(), ignore: ig} |
28
|
|
|
|
29
|
|
|
let pattern |
30
|
|
|
if (argv._.length === 0) { pattern = './**/*.md' } else { pattern = argv._[0] } |
31
|
|
|
|
32
|
|
|
debug('starting glob(' + pattern + ', ' + JSON.stringify(options)) |
33
|
|
|
glob(pattern, options) |
34
|
|
|
.then(files => { |
35
|
|
|
debug('found ' + files.length + ' files') |
36
|
|
|
let tStart, tTaken, ms |
37
|
|
|
for (let i = 0; i < files.length; i++) { |
38
|
|
|
tStart = process.hrtime() |
39
|
|
|
process.stdout.write(files[i] + chalk.cyan(' ...processing')) |
40
|
|
|
processFile(files[i], argv.clear) |
41
|
|
|
tTaken = process.hrtime(tStart) |
42
|
|
|
ms = Math.round(tTaken[0] * 1000 + tTaken[1] / 1000000) |
43
|
|
|
process.stdout.write('\x1B[0G' + files[i] + chalk.green(' (' + ms + ' ms) \r\n')) |
44
|
|
|
} |
45
|
|
|
console.log('processed ' + files.length + ' files') |
|
|
|
|
46
|
|
|
return 0 |
47
|
|
|
}) |
48
|
|
|
.catch((err) => { |
49
|
|
|
debug('glob failed: ' + err) |
50
|
|
|
console.error(err) |
51
|
|
|
return err |
52
|
|
|
}) |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
function printHelp () { |
56
|
|
|
console.info(chalk.cyan(' mdprepare processes markdown files and acts on any mdpInsert commands within them\n\n') + |
57
|
|
|
' Usage: ' + chalk.whiteBright('mdprepare GLOB [options]') + chalk.gray(' process the files specified\n\n') + |
58
|
|
|
' GLOB ' + chalk.gray('Glob expression representing the files to be processed\n') + |
59
|
|
|
' Options:\n' + |
60
|
|
|
' --ignore GLOB\n' + |
61
|
|
|
chalk.grey(' GLOB represents a list of files to be ignored\n') + |
62
|
|
|
' --clear\n' + |
63
|
|
|
chalk.gray(' clears the contents in the files specified rather than inserting\n')) |
64
|
|
|
} |
65
|
|
|
|