markdown-magic 3.3.0 → 3.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/cli.js CHANGED
@@ -1,184 +1,37 @@
1
-
2
1
  const path = require('path')
3
- const isGlob = require('is-glob')
4
- const isValidFilePath = require('is-valid-path')
5
2
  const { loadConfig } = require('./utils/load-config')
6
3
  const { findUp } = require('./utils/fs')
7
4
  const { markdownMagic } = require('./')
8
5
  const { parse } = require('oparser')
9
- const { deepLog } = require('./utils/logs')
10
- const { REGEX_REGEX, escapeRegexString } = require('./utils/regex')
11
- const { getFirstCharacter, isUpperCase } = require('./utils/text')
6
+ const { getGlobGroupsFromArgs } = require('./globparse')
7
+ // const { deepLog } = require('./utils/logs')
8
+ // const { uxParse } = require('./argparse/argparse')
12
9
  const argv = process.argv.slice(2)
13
10
  const cwd = process.cwd()
14
11
  const defaultConfigPath = 'md.config.js'
15
12
 
16
- const ARRAY_REGEX = /^\[(.*)\]$/
17
- const NOT_OBJECT_LIKE = /^{[^:,]*}/
18
- const IS_KEY_VALUE_NON_ARRAY = /^([A-Za-z0-9_]*)=\s?([^\[\{]]*)([^\[\{]*)$/
19
-
20
- function isArrayLike(str) {
21
- if (typeof str !== 'string') return false
22
- return Boolean(ARRAY_REGEX.test(str))
23
- }
24
-
25
13
  async function getBaseDir(opts = {}) {
26
14
  const { currentDir = cwd } = opts
27
15
  const gitDir = await findUp(currentDir, '.git')
28
16
  return (gitDir) ? path.dirname(gitDir) : currentDir
29
17
  }
30
18
 
31
- function stringLooksLikeFile(value) {
32
- return !value.match(/^-+/) // isn't option looking
33
- && isValidFilePath(value)
34
- && (
35
- path.basename(value).indexOf('.') > -1 // has period
36
- || getFirstCharacter(value) === '_' // starts with _
37
- // || isUpperCase(value) // is all caps
38
- )
19
+ function findSingleDashStrings(arr) {
20
+ return arr.filter(str => str.match(/^-[^-]/))
39
21
  }
40
22
 
41
- function getValue(val) {
42
- return (val[1] !== '=') ? val[1] : val[0]
43
- }
44
-
45
- function trimLeadingDashes(value) {
46
- return value.replace(/^-+/, '')
47
- }
48
-
49
- function removeNodeModules(value = '') {
50
- if (typeof value !== 'string') {
51
- return true
52
- }
53
- return !value.match(/node_modules\//)
54
- }
55
-
56
- function coerceStringToRegex(str) {
57
- const isRegex = str.match(REGEX_REGEX)
58
- if (!isRegex) {
59
- return str
60
- }
61
- const [ _match, pattern, flags ] = isRegex
62
- return new RegExp(escapeRegexString(pattern), flags)
63
- }
64
-
65
- function convertToArray(str = '') {
66
- const { fixedArray } = parse(`fixedArray=${str}`)
67
- return (fixedArray || [])
68
- }
69
-
70
- function addValue(value, currentCollection) {
71
- if (isArrayLike(value)) {
72
- const array = convertToArray(value)
73
- // uniquify returned array
74
- return Array.from(new Set(currentCollection.concat(array)))
75
- }
76
- if (currentCollection.indexOf(value) > -1) {
77
- return currentCollection
78
- }
79
- return currentCollection.concat(value)
80
- }
81
-
82
- /*
83
- node ./cli.js test/fixtures/md/**.md debug --transform test/fixtures/md/transform-**.md 1233535235 = hahah funky=hi --ignore test/fixtures/output/**.md --lol --whatever test/fixtures/md/syntax-**.md --foo=bar --fun lol.md what=no.md x 'xno.md' what
84
- */
85
- function getGlobGroupsFromArgs(args, opts = {}) {
86
- const globKeys = opts.globKeys || []
87
- let preceding = ['', '']
88
- let collection = []
89
- let globGroups = []
90
- let reserved = []
91
- let otherOpts = []
92
- let /** @type {string|undefined} */ cacheKey = ''
93
- // console.log('args', args)
94
- for (let i = 0; i < args.length; i++) {
95
- const isLastArg = (args.length - 1) === i
96
- const arg = args[i]
97
- const prevArg = args[i - 1]
98
- const looksLikeFile = stringLooksLikeFile(arg) // @TODO verify file exists?
99
-
100
- // console.log('arg', arg)
101
- // console.log('looksLikeFile', looksLikeFile)
102
- // console.log('collection', collection)
103
-
104
- // console.log('cacheKey', cacheKey)
105
-
106
- if (looksLikeFile && typeof cacheKey !== 'undefined') {
107
- // @TODO verify file exists?
108
- collection.push(arg)
109
- } else if (arg.match(/^-+/) && !arg.match(/=/)) {
110
- cacheKey = arg
111
- // console.log('cacheKey', cacheKey)
112
- // console.log('collection', collection)
113
- // console.log('arg', arg)
114
- if (collection.length) {
115
- const val = getValue(preceding)
116
- // console.log('val', val)
117
- reserved.push(val)
118
- globGroups.push({
119
- key: trimLeadingDashes(val),
120
- rawKey: val,
121
- values: collection.filter(removeNodeModules).map((x) => coerceStringToRegex(x))
122
- })
123
- collection = []
124
- }
125
- preceding = [ prevArg, arg ]
126
- } else if (cacheKey && arg.match(/=/)) {
127
- // console.log('FOOO', arg)
128
- cacheKey = undefined
129
- }
130
-
131
- const cleanKey = trimLeadingDashes(cacheKey || '')
132
- const isDefinedGlobKey = globKeys.includes(cleanKey) || globKeys.includes(cacheKey)
133
- // console.log(`isDefinedGlobKey: ${cleanKey}`, isDefinedGlobKey)
134
- if (isDefinedGlobKey && (isGlob(arg) || looksLikeFile)) {
135
- /*
136
- console.log('ADD collection.push', arg)
137
- /** */
138
- collection = addValue(arg, collection)
139
- } else {
140
- if (!cacheKey && isGlob(arg) && !arg.match(/=/) || (arg.match(NOT_OBJECT_LIKE) && !isArrayLike(arg))) {
141
- // console.log('GLOB MATCH', arg, isGlob(arg))
142
- collection = addValue(arg, collection)
143
- } else if (!looksLikeFile) {
144
- /* Pass through non matching args */
145
- /*
146
- console.log('ADD otherOpts', arg)
147
- /** */
148
- if (arg.match(IS_KEY_VALUE_NON_ARRAY)) {
149
- // quote quote wrap
150
- otherOpts.push(arg.replace(IS_KEY_VALUE_NON_ARRAY, '$1="$2$3"'))
151
- } else {
152
- otherOpts.push(arg)
153
- }
154
-
155
- }
156
- }
157
-
158
- // console.log('end', collection)
159
- if (isLastArg && collection.length) {
160
- // console.log('isLastArg', isLastArg)
161
- const val = getValue(preceding)
162
- reserved.push(val)
163
- globGroups.push({
164
- key: trimLeadingDashes(val),
165
- rawKey: val,
166
- values: collection.filter(removeNodeModules).map((x) => coerceStringToRegex(x)),
167
- // last: 'last'
168
- })
169
- collection = []
170
- }
171
- }
172
-
173
- return {
174
- globGroups,
175
- otherOpts: otherOpts.filter((opt) => !reserved.includes(opt))
176
- }
177
- }
178
-
179
- async function runCli(options = {}) {
23
+ async function runCli(options = {}, rawArgv) {
180
24
  let configFile
181
25
  let opts = {}
26
+
27
+ /*
28
+ const result = uxParse(rawArgv)
29
+ console.log('───────────────────────────────')
30
+ deepLog(result)
31
+ process.exit(1)
32
+ // process.exit(1)
33
+ /** */
34
+
182
35
  /*
183
36
  console.log('argv', argv)
184
37
  console.log('options', options)
@@ -199,7 +52,71 @@ async function runCli(options = {}) {
199
52
  console.log('globGroups', globGroups)
200
53
  console.log('globParse', globParse)
201
54
  // deepLog(globParse)
55
+ process.exit(1)
202
56
  /** */
57
+ /* Parse for weird CLI inputs */
58
+
59
+ /* Handle -- and - flags */
60
+ let newArray = []
61
+ for (let i = 0; i < otherOpts.length; i++) {
62
+ const curr = otherOpts[i]
63
+ const prev = newArray[i - 1]
64
+ const next = otherOpts[i + 1] || ''
65
+ const isLast = otherOpts.length === i + 1
66
+ // console.log('curr', curr)
67
+ // console.log('prev', prev)
68
+ if (curr.match(/^-+/)) {
69
+ const cleanX = curr.replace(/^-+/, '')
70
+ if (next.match(/^-+/) || isLast) {
71
+ newArray.push(cleanX + '= true ')
72
+ continue
73
+ }
74
+ // If the current option is the last option, don't add an equal sign
75
+ const equal = (cleanX.indexOf('=') === -1 || isLast) ? '=' : ' '
76
+ const final = cleanX + equal
77
+ newArray.push(final)
78
+ continue
79
+ }
80
+ if (prev && prev.match(/=\s?$/) && (curr.match(/^\s?=/) || curr.trim() === '=')) {
81
+ continue
82
+ }
83
+ newArray.push(curr + ' ')
84
+ }
85
+
86
+ const optString = newArray.join('')
87
+ const extraParse = parse(optString)
88
+ const singleDashStrings = findSingleDashStrings(otherOpts).map((x) => x.replace(/^-+/, ''))
89
+ // console.log('singleDashStrings', singleDashStrings)
90
+ // console.log('before options', options)
91
+ // console.log('before extraParse', extraParse)
92
+
93
+ const STRIP_SINGLE_DASH_OPTIONS = true
94
+ if (STRIP_SINGLE_DASH_OPTIONS && singleDashStrings.length) {
95
+ for (let i = 0; i < singleDashStrings.length; i++) {
96
+ const word = singleDashStrings[i]
97
+ // Loop over all letters of single dash options -word and remove any corresponding letter: true
98
+ for (let j = 0; j < word.length; j++) {
99
+ const letter = word[j]
100
+ if (options[letter]) {
101
+ delete options[letter]
102
+ }
103
+ }
104
+ }
105
+ // console.log('after options', options)
106
+ }
107
+
108
+ if (extraParse.test) {
109
+ /*
110
+ console.log('optStringArr', newArray)
111
+ console.log('optString', optString)
112
+ console.log('otherOpts strings', otherOpts)
113
+ console.log('nicely handed CLI args')
114
+ console.log('extraParse', extraParse)
115
+ process.exit(1)
116
+ /** */
117
+ }
118
+
119
+
203
120
  if (globGroups.length) {
204
121
  const globGroupByKey = globGroups.reduce((acc, curr, i) => {
205
122
  acc[curr.key] = globGroups[i]
@@ -228,36 +145,6 @@ async function runCli(options = {}) {
228
145
  /** */
229
146
  }
230
147
 
231
- /* Parse for weird CLI inputs */
232
-
233
- /* Handle -- and - flags */
234
- let newArray = []
235
- for (let i = 0; i < otherOpts.length; i++) {
236
- const curr = otherOpts[i]
237
- const prev = newArray[i - 1]
238
- if (curr.match(/^-+/)) {
239
- const cleanX = curr.replace(/^-+/, '')
240
- const equal = (cleanX.indexOf('=') === -1) ? '=' : ' '
241
- newArray.push(cleanX + equal)
242
- continue
243
- }
244
- if (prev && prev.match(/=\s?$/) && (curr.match(/^\s?=/) || curr.trim() === '=')) {
245
- continue
246
- }
247
- newArray.push(curr + ' ')
248
- }
249
-
250
- const optString = newArray.join('')
251
- const extraParse = parse(optString)
252
- /*
253
- console.log('optStringArr', newArray)
254
- console.log('optString', optString)
255
- console.log('otherOpts strings', otherOpts)
256
- console.log('nicely handed CLI args')
257
- console.log('extraParse', extraParse)
258
- process.exit(1)
259
- /** */
260
-
261
148
  if (extraParse.file) {
262
149
  options.files = options.files.concat(extraParse.file)
263
150
  delete extraParse.file
@@ -307,6 +194,7 @@ async function runCli(options = {}) {
307
194
  mergedConfig.outputDir = mergedConfig.output || mergedConfig.outputDir
308
195
  }
309
196
  /*
197
+ console.log('rawArgv', rawArgv)
310
198
  console.log('mergedConfig', mergedConfig)
311
199
  process.exit(1)
312
200
  // return