1 | #!/usr/bin/env node
|
2 |
|
3 | const program = require('commander')
|
4 | const pkg = require('./package.json')
|
5 | const markdownMagic = require('./index')
|
6 | const findUp = require('find-up')
|
7 | const defaultFileName = 'README.md'
|
8 | const defaultConfigPath = './markdown.config.js'
|
9 | const loadConfig = require('./cli-utils').loadConfig
|
10 |
|
11 | var filePaths = defaultFileName
|
12 | var callbackFunction = defaultCallback
|
13 | var ignorePath
|
14 |
|
15 |
|
16 | program
|
17 | .version(pkg.version)
|
18 | .option('-p, --path [path]', `Define path to markdown (single path or glob). Default ${defaultFileName}`, parsePaths, defaultFileName)
|
19 | .option('-i, --ignore [path]', '(Optional) Define path to ignore', parseIgnorePaths, defaultConfigPath)
|
20 | .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)
|
21 |
|
22 | .parse(process.argv)
|
23 |
|
24 | const configFile = getConfigFilepath()
|
25 | const foundConfig = (configFile) ? loadConfig(configFile) : false
|
26 |
|
27 | if (foundConfig && foundConfig.callback) {
|
28 | callbackFunction = foundConfig.callback
|
29 | }
|
30 | if (!foundConfig) {
|
31 |
|
32 | }
|
33 | const configuration = foundConfig || {}
|
34 |
|
35 | function parsePaths(path, defaultValue) {
|
36 | if (path) {
|
37 | filePaths = path
|
38 | }
|
39 | }
|
40 |
|
41 | function parseIgnorePaths(path, defaultValue) {
|
42 | if (path) {
|
43 | ignorePath = path.split(',').map((p) => {
|
44 | const fp = p.trim()
|
45 | if (fp.match(/\bnode_modules\b/)) {
|
46 |
|
47 | return '!node_modules/**'
|
48 | }
|
49 | if (!fp.match(/^!/)) {
|
50 | return `!${fp}`
|
51 | }
|
52 | return fp
|
53 | })
|
54 | }
|
55 | }
|
56 |
|
57 |
|
58 | if (program.path) {
|
59 | filePaths = program.path
|
60 | }
|
61 |
|
62 | if (ignorePath) {
|
63 |
|
64 | filePaths = [filePaths].concat(ignorePath)
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | console.log('Starting markdown-magic', filePaths)
|
71 | markdownMagic(filePaths, configuration, callbackFunction)
|
72 |
|
73 | function defaultCallback(err, msg) {
|
74 | if (err) {
|
75 | console.log('Error:', err)
|
76 | }
|
77 | if (msg) {
|
78 | console.log('Files processed. markdown-magic Finished! ⊂◉‿◉つ')
|
79 | }
|
80 | }
|
81 |
|
82 | function getConfigFilepath() {
|
83 | return program.config || findUp.sync(defaultConfigPath) || findUp.sync('md-magic.config.js')
|
84 | }
|