UNPKG

2.54 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const program = require('commander')
4const pkg = require('./package.json')
5const markdownMagic = require('./index')
6const findUp = require('find-up')
7const defaultFileName = 'README.md'
8const defaultConfigPath = './markdown.config.js'
9const loadConfig = require('./cli-utils').loadConfig
10
11var filePaths = defaultFileName
12var callbackFunction = defaultCallback // eslint-disable-line
13var ignorePath // eslint-disable-line
14
15// start commander.js
16program
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 // .option('-cb, --callback [path]', 'Define path', parsePaths, defaultFileName)
22 .parse(process.argv)
23
24const configFile = getConfigFilepath()
25const foundConfig = (configFile) ? loadConfig(configFile) : false
26
27if (foundConfig && foundConfig.callback) {
28 callbackFunction = foundConfig.callback
29}
30if (!foundConfig) {
31 // console.log('No markdown magic config set using {empty object}')
32}
33const configuration = foundConfig || {}
34
35function parsePaths(path, defaultValue) {
36 if (path) {
37 filePaths = path
38 }
39}
40
41function 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 // exact node_module match. Ignore entire DIR
47 return '!node_modules/**'
48 }
49 if (!fp.match(/^!/)) {
50 return `!${fp}`
51 }
52 return fp
53 })
54 }
55}
56
57// process default
58if (program.path) {
59 filePaths = program.path
60}
61
62if (ignorePath) {
63 // console.log('ignore path', ignorePath)
64 filePaths = [filePaths].concat(ignorePath)
65}
66
67// console.log('filePaths', filePaths)
68// console.log('configuration', configuration)
69// console.log('callbackFunction', callbackFunction)
70console.log('Starting markdown-magic', filePaths)
71markdownMagic(filePaths, configuration, callbackFunction)
72
73function 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
82function getConfigFilepath() {
83 return program.config || findUp.sync(defaultConfigPath) || findUp.sync('md-magic.config.js')
84}