forked from DavidWells/markdown-magic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
84 lines (72 loc) · 2.48 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
#!/usr/bin/env node
const program = require('commander')
const pkg = require('./package.json')
const markdownMagic = require('./index')
const findUp = require('find-up')
const defaultFileName = 'README.md'
const defaultConfigPath = './markdown.config.js'
const loadConfig = require('./cli-utils').loadConfig
var filePaths = defaultFileName
var callbackFunction = defaultCallback // eslint-disable-line
var ignorePath // eslint-disable-line
// start commander.js
program
.version(pkg.version)
.option('-p, --path [path]', `Define path to markdown (single path or glob). Default ${defaultFileName}`, parsePaths, defaultFileName)
.option('-i, --ignore [path]', '(Optional) Define path to ignore', parseIgnorePaths, defaultConfigPath)
.option('-c, --config [path]', '(Optional) Define config file path. If you have custom transforms or a callback you will want to specify this option', null, defaultConfigPath)
// .option('-cb, --callback [path]', 'Define path', parsePaths, defaultFileName)
.parse(process.argv)
const configFile = getConfigFilepath()
const foundConfig = (configFile) ? loadConfig(configFile) : false
if (foundConfig && foundConfig.callback) {
callbackFunction = foundConfig.callback
}
if (!foundConfig) {
// console.log('No markdown magic config set using {empty object}')
}
const configuration = foundConfig || {}
function parsePaths(path, defaultValue) {
if (path) {
filePaths = path
}
}
function parseIgnorePaths(path, defaultValue) {
if (path) {
ignorePath = path.split(',').map((p) => {
const fp = p.trim()
if (fp.match(/\bnode_modules\b/)) {
// exact node_module match. Ignore entire DIR
return '!node_modules/**'
}
if (!fp.match(/^!/)) {
return `!${fp}`
}
return fp
})
}
}
// process default
if (program.path) {
filePaths = program.path
}
if (ignorePath) {
// console.log('ignore path', ignorePath)
filePaths = [filePaths].concat(ignorePath)
}
// console.log('filePaths', filePaths)
// console.log('configuration', configuration)
// console.log('callbackFunction', callbackFunction)
console.log('Starting markdown-magic', filePaths)
markdownMagic(filePaths, configuration, callbackFunction)
function defaultCallback(err, msg) {
if (err) {
console.log('Error:', err)
}
if (msg) {
console.log('Files processed. markdown-magic Finished! ⊂◉‿◉つ')
}
}
function getConfigFilepath() {
return program.config || findUp.sync(defaultConfigPath) || findUp.sync('md-magic.config.js')
}