markdown-magic 3.7.0 → 4.0.1

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.
Files changed (131) hide show
  1. package/cli.js +1 -1
  2. package/package.json +22 -26
  3. package/src/{cli.js → cli-run.js} +0 -0
  4. package/src/{cli.test.js → cli-run.test.js} +1 -1
  5. package/src/defaults.js +2 -2
  6. package/src/index.js +26 -20
  7. package/src/index.test.js +2 -2
  8. package/src/process-contents.js +2 -458
  9. package/src/transforms/code/index.js +6 -6
  10. package/src/transforms/fileTree.js +3 -3
  11. package/src/transforms/index.js +21 -21
  12. package/src/transforms/install.js +3 -3
  13. package/src/transforms/sectionToc.js +4 -4
  14. package/src/transforms/toc.js +5 -5
  15. package/src/transforms/wordCount.js +3 -2
  16. package/src/utils/fs.js +22 -0
  17. package/src/utils/fs.test.js +2 -2
  18. package/src/utils/index.js +21 -0
  19. package/src/utils/regex-timeout.js +1 -1
  20. package/src/utils/text.js +103 -11
  21. package/types/_tests/config.d.ts +4 -0
  22. package/types/_tests/config.d.ts.map +1 -0
  23. package/types/_tests/errors.test.d.ts +2 -0
  24. package/types/_tests/errors.test.d.ts.map +1 -0
  25. package/types/_tests/fixtures/js/simple.d.ts +8 -0
  26. package/types/_tests/fixtures/js/simple.d.ts.map +1 -0
  27. package/types/_tests/fixtures/local-code-file-lines.d.ts +1 -0
  28. package/types/_tests/fixtures/local-code-file-lines.d.ts.map +1 -0
  29. package/types/_tests/fixtures/local-code-file.d.ts +2 -0
  30. package/types/_tests/fixtures/local-code-file.d.ts.map +1 -0
  31. package/types/_tests/fixtures/local-code-id.d.ts +3 -0
  32. package/types/_tests/fixtures/local-code-id.d.ts.map +1 -0
  33. package/types/_tests/transforms-toc.test.d.ts +2 -0
  34. package/types/_tests/transforms-toc.test.d.ts.map +1 -0
  35. package/types/_tests/transforms.test.d.ts +2 -0
  36. package/types/_tests/transforms.test.d.ts.map +1 -0
  37. package/types/_tests/utils/diff.d.ts +3 -0
  38. package/types/_tests/utils/diff.d.ts.map +1 -0
  39. package/types/cli.d.ts +3 -0
  40. package/types/cli.d.ts.map +1 -0
  41. package/types/src/argparse/argparse.d.ts +33 -0
  42. package/types/src/argparse/argparse.d.ts.map +1 -0
  43. package/types/src/argparse/argparse.test.d.ts +2 -0
  44. package/types/src/argparse/argparse.test.d.ts.map +1 -0
  45. package/types/src/argparse/index.d.ts +3 -0
  46. package/types/src/argparse/index.d.ts.map +1 -0
  47. package/types/src/argparse/splitOutsideQuotes.d.ts +2 -0
  48. package/types/src/argparse/splitOutsideQuotes.d.ts.map +1 -0
  49. package/types/src/argparse/splitOutsideQuotes.test.d.ts +2 -0
  50. package/types/src/argparse/splitOutsideQuotes.test.d.ts.map +1 -0
  51. package/types/src/cli-run.d.ts +4 -0
  52. package/types/src/cli-run.d.ts.map +1 -0
  53. package/types/src/cli-run.test.d.ts +2 -0
  54. package/types/src/cli-run.test.d.ts.map +1 -0
  55. package/types/src/defaults.d.ts +5 -0
  56. package/types/src/defaults.d.ts.map +1 -0
  57. package/types/src/globparse.d.ts +18 -0
  58. package/types/src/globparse.d.ts.map +1 -0
  59. package/types/src/globparse.test.d.ts +2 -0
  60. package/types/src/globparse.test.d.ts.map +1 -0
  61. package/types/src/index.d.ts +189 -0
  62. package/types/src/index.d.ts.map +1 -0
  63. package/types/src/index.test.d.ts +2 -0
  64. package/types/src/index.test.d.ts.map +1 -0
  65. package/types/src/process-contents.d.ts +3 -0
  66. package/types/src/process-contents.d.ts.map +1 -0
  67. package/types/src/transforms/code/index.d.ts +35 -0
  68. package/types/src/transforms/code/index.d.ts.map +1 -0
  69. package/types/src/transforms/code/resolve-github-file.d.ts +24 -0
  70. package/types/src/transforms/code/resolve-github-file.d.ts.map +1 -0
  71. package/types/src/transforms/code/resolve-github-file.test.d.ts +2 -0
  72. package/types/src/transforms/code/resolve-github-file.test.d.ts.map +1 -0
  73. package/types/src/transforms/file.d.ts +3 -0
  74. package/types/src/transforms/file.d.ts.map +1 -0
  75. package/types/src/transforms/fileTree.d.ts +35 -0
  76. package/types/src/transforms/fileTree.d.ts.map +1 -0
  77. package/types/src/transforms/index.d.ts +9 -0
  78. package/types/src/transforms/index.d.ts.map +1 -0
  79. package/types/src/transforms/install.d.ts +25 -0
  80. package/types/src/transforms/install.d.ts.map +1 -0
  81. package/types/src/transforms/remote.d.ts +3 -0
  82. package/types/src/transforms/remote.d.ts.map +1 -0
  83. package/types/src/transforms/sectionToc.d.ts +3 -0
  84. package/types/src/transforms/sectionToc.d.ts.map +1 -0
  85. package/types/src/transforms/toc.d.ts +31 -0
  86. package/types/src/transforms/toc.d.ts.map +1 -0
  87. package/types/src/transforms/wordCount.d.ts +3 -0
  88. package/types/src/transforms/wordCount.d.ts.map +1 -0
  89. package/types/src/types.d.ts +11 -0
  90. package/types/src/types.d.ts.map +1 -0
  91. package/types/src/utils/details.d.ts +3 -0
  92. package/types/src/utils/details.d.ts.map +1 -0
  93. package/types/src/utils/format-md.d.ts +2 -0
  94. package/types/src/utils/format-md.d.ts.map +1 -0
  95. package/types/src/utils/fs.d.ts +29 -0
  96. package/types/src/utils/fs.d.ts.map +1 -0
  97. package/types/src/utils/fs.test.d.ts +2 -0
  98. package/types/src/utils/fs.test.d.ts.map +1 -0
  99. package/types/src/utils/hash-file.d.ts +2 -0
  100. package/types/src/utils/hash-file.d.ts.map +1 -0
  101. package/types/src/utils/index.d.ts +25 -0
  102. package/types/src/utils/index.d.ts.map +1 -0
  103. package/types/src/utils/load-config.d.ts +2 -0
  104. package/types/src/utils/load-config.d.ts.map +1 -0
  105. package/types/src/utils/logs.d.ts +11 -0
  106. package/types/src/utils/logs.d.ts.map +1 -0
  107. package/types/src/utils/regex-timeout.d.ts +2 -0
  108. package/types/src/utils/regex-timeout.d.ts.map +1 -0
  109. package/types/src/utils/regex.d.ts +7 -0
  110. package/types/src/utils/regex.d.ts.map +1 -0
  111. package/types/src/utils/remoteRequest.d.ts +3 -0
  112. package/types/src/utils/remoteRequest.d.ts.map +1 -0
  113. package/types/src/utils/sortOrder.d.ts +3 -0
  114. package/types/src/utils/sortOrder.d.ts.map +1 -0
  115. package/types/src/utils/string-break.d.ts +3 -0
  116. package/types/src/utils/string-break.d.ts.map +1 -0
  117. package/types/src/utils/syntax.d.ts +77 -0
  118. package/types/src/utils/syntax.d.ts.map +1 -0
  119. package/types/src/utils/text.d.ts +146 -0
  120. package/types/src/utils/text.d.ts.map +1 -0
  121. package/types/src/utils/text.test.d.ts +2 -0
  122. package/types/src/utils/text.test.d.ts.map +1 -0
  123. package/types/src/utils/toc.d.ts +67 -0
  124. package/types/src/utils/toc.d.ts.map +1 -0
  125. package/types/src/utils/toposort.d.ts +7 -0
  126. package/types/src/utils/toposort.d.ts.map +1 -0
  127. package/README.md +0 -700
  128. package/src/block-parser-js.test.js +0 -171
  129. package/src/block-parser.js +0 -405
  130. package/src/block-parser.test.js +0 -481
  131. package/src/process-file.js +0 -66
@@ -1,481 +0,0 @@
1
- const { test } = require('uvu')
2
- const assert = require('uvu/assert')
3
- const { parseBlocks } = require('./block-parser')
4
- const { deepLog } = require('./utils/logs')
5
-
6
- const md = `<h1 id="jdjdj">Netlify + FaunaDB &nbsp;&nbsp;&nbsp;
7
- <a href="https://app.netlify.com/start/deploy?repository=https://github.com/netlify/netlify-faunadb-example&stack=fauna">
8
- <img src="https://www.netlify.com/img/deploy/button.svg">
9
- </a>
10
- </h1>
11
-
12
- <\!-- XYZ:START functionName foo={{ rad: 'yellow' }} -->
13
- nice
14
- <\!-- XYZ:END -->
15
-
16
- <\!-- XYZ:START {functionName} foo={{ rad: 'blue' }} -->
17
- nice
18
- <\!-- XYZ:END -->
19
-
20
-
21
- <\!-- XYZ:START {functionName} foo={{ rad: 'red' }} -->
22
- nice
23
- <\!-- XYZ:END -->
24
-
25
-
26
- <\!-- XYZ:START [wootName] foo=['one', 'two'] -->
27
- nice
28
- <\!-- XYZ:END -->
29
-
30
-
31
- <\!-- XYZ:START -->
32
- lol
33
- <\!-- XYZ:END -->
34
-
35
- <!-- XYZ:START funky-key-here kldskjfjslfjs -->
36
- lol
37
- <!-- XYZ:END -->
38
-
39
- <\!-- xyz:start (lowerCase) foo=['one', 'two'] heading=false -->
40
- nice
41
- <\!-- XYZ:END -->
42
-
43
-
44
- <\!-- XYZ:START(cool)
45
- width={999}
46
- height={{111}}
47
- numberAsString="12345"
48
- great={["scoot", "sco ot", 'scooo ttt']}
49
- nice={{ value: nice, cool: "true" }}
50
- soclose=[jdjdjd, hdhfhfhffh]
51
- rad="boss"
52
- cool=true notCool=false
53
- nooooo={[one, two, 3, 4]}
54
- numberZero=0,
55
- xyz=999,
56
- nope=false,
57
- // comment
58
- yes={true} -->
59
-
60
- actual content
61
-
62
- <\!-- XYZ:END -->
63
-
64
-
65
- <img src="https://www.netlify.com/img/deploy/button.svg"/>
66
-
67
-
68
- <img src="https://www.hehhehehehe.com/img/deploy/button.svg" />
69
-
70
-
71
- <\!-- XYZ:START(cool) xxx
72
- hhddh=cool -->
73
- wowow
74
- whatever we want 
75
- <\!-- XYZ:END -->
76
-
77
-
78
- <\!-- XYZ:START(hhh) -->
79
- xyz
80
- <\!-- XYZ:END -->
81
-
82
-
83
- <\!-- XYZ:START(cool) isCool -->
84
- nice
85
- <\!-- XYZ:END -->
86
-
87
-
88
- <button 
89
- great={[one, two, 3, 4]}
90
- >
91
- wow
92
- </button>
93
-
94
-
95
- <\!-- XYZ:START(niceeeee)
96
- xxx
97
- // comment here
98
- hhddh=cool
99
- -->
100
- contents
101
- <\!-- XYZ:END -->
102
-
103
- <button 
104
- width={999}
105
- great={["scoot", "scoot"]}
106
- nice={{ value: nice, cool: true }}
107
- rad="boss"
108
- cool=true 
109
- nope=false 
110
- what='xnxnx'
111
- isLoading 
112
- src="https://user-images.githubusercontent.com/532272/123136878-46f1a300-d408-11eb-82f2-ad452498457b.jpg"
113
- >
114
- coooooll
115
- </button>
116
-
117
-
118
- <hr />
119
-
120
-
121
- <br />
122
-
123
-
124
- <ReactComponent>lolol</ReactComponent>
125
-
126
-
127
- <ReactComponent width={123} lol={["no", "cool"]}>
128
- lolol
129
- </ReactComponent>
130
-
131
-
132
- <OtherComponent width={123} lol={["no", "cool"]} nice={{ value: "nice", cool: true }}>
133
- lolol
134
- </OtherComponent>
135
-
136
-
137
- <table style="width:100%">
138
- <tr>
139
- <th>Firstname</th>
140
- <th>Lastname</th>
141
- <th>Age</th>
142
- </tr>
143
- <tr>
144
- <td>Jill</td>
145
- <td>Smith</td>
146
- <td>50</td>
147
- </tr>
148
- <tr>
149
- <td>Eve</td>
150
- <td>Jackson</td>
151
- <td>94</td>
152
- </tr>
153
- </table>
154
-
155
-
156
- <div>
157
- <p>
158
- <img align="right" isLoading={false} width="250" src="https://user-images.githubusercontent.com/532272/123136878-46f1a300-d408-11eb-82f2-ad452498457b.jpg" />
159
- </p>
160
- <p>
161
- cool
162
- </p>
163
- <div>
164
-
165
-
166
- <p>
167
- <img align="left" width="250" src="https://user-images.githubusercontent.com/532272/123136889-4953fd00-d408-11eb-8a3e-f82f1d073298.jpg" />
168
- </p>
169
-
170
-
171
- Add a little magic to your markdown
172
-
173
-
174
- ## About
175
-
176
-
177
- <img align="right" width="200" height="183" src="https://cloud.githubusercontent.com/assets/532272/21507867/3376e9fe-cc4a-11e6-9350-7ec4f680da36.gif" />Markdown magic uses comment blocks in markdown files to automatically sync or transform its contents.
178
-
179
-
180
- Markdown magic uses comment blocks in markdown files to automatically sync or transform its contents. <img align="right" width="200" height="183" src="https://cloud.githubusercontent.com/assets/532272/21507867/3376e9fe-cc4a-11e6-9350-7ec4f680da36.gif" />
181
- `
182
-
183
- test('verify parser', () => {
184
- const parsedValue = parseBlocks(md, {
185
- open: 'XYZ:START',
186
- close: 'XYZ:END'
187
- })
188
- /*
189
- console.log('parsedValue')
190
- deepLog(parsedValue)
191
- // process.exit(1)
192
- /** */
193
- assert.equal(typeof parsedValue, 'object')
194
- assert.equal(parsedValue.blocks.length, 11)
195
- })
196
-
197
- test('inline parser', () => {
198
- const inlineOne = `<!--XYZ:START functionName foo={{ rad: 'bar' }}-->99<!--XYZ:END-->`
199
- const one = parseBlocks(inlineOne, {
200
- open: 'XYZ:START',
201
- close: 'XYZ:END'
202
- })
203
- /*
204
- console.log('inline one')
205
- deepLog(one)
206
- /** */
207
- const values = [
208
- {
209
- type: 'functionName',
210
- options: { foo: { rad: 'bar' } },
211
- location: 66
212
- }
213
- ]
214
- values.forEach((val, i) => {
215
- const stub = val
216
- const currentItem = one.blocks[i]
217
- assert.equal(stub.type, currentItem.type, `${stub.type} ${i} transform`)
218
- assert.equal(stub.options, currentItem.options, `${stub.type} ${i} options`)
219
- })
220
-
221
- const inlineTwo = ` <!-- XYZ:START transformX foo=111 -->99<!-- XYZ:END -->`
222
- const two = parseBlocks(inlineTwo, {
223
- open: 'XYZ:START',
224
- close: 'XYZ:END'
225
- })
226
- /*
227
- console.log('inline two ───────────────────────')
228
- deepLog(two)
229
- /** */
230
- const valuesTwo = [
231
- {
232
- type: 'transformX',
233
- options: { foo: 111 },
234
- location: 55
235
- }
236
- ]
237
- valuesTwo.forEach((val, i) => {
238
- const stub = val
239
- const currentItem = two.blocks[i]
240
- assert.equal(stub.type, currentItem.type, `${stub.type} ${i} transform`)
241
- assert.equal(stub.options, currentItem.options, `${stub.type} ${i} options`)
242
- })
243
- })
244
-
245
- const fnBlocks = `
246
- <!-- XYZ:START functionName foo={{ rad: 'yellow' }} -->
247
- nice
248
- <!-- XYZ:END -->
249
-
250
- <!-- XYZ:START {functionName} foo={{ rad: 'blue' }} -->
251
- nice
252
- <!-- XYZ:END -->
253
-
254
- <!-- XYZ:START (functionName) foo={{ rad: 'red' }} -->
255
- nice
256
- <!-- XYZ:END -->
257
-
258
- <!-- XYZ:START [functionName] foo={{ rad: 'purple' }} -->
259
- nice
260
- <!-- XYZ:END -->
261
-
262
- <!-- XYZ:START {{functionName}} foo={{ rad: 'black' }} -->
263
- nice
264
- <!-- XYZ:END -->
265
-
266
- <!-- XYZ:START ((functionName)) foo={{ rad: 'white' }} -->
267
- nice
268
- <!-- XYZ:END -->
269
-
270
- <!-- XYZ:START [[functionName]] foo={{ rad: 'orange' }} -->
271
- nice
272
- <!-- XYZ:END -->
273
- `
274
-
275
- test('Handles function names', () => {
276
- const parsedValue = parseBlocks(fnBlocks, {
277
- open: 'XYZ:START',
278
- close: 'XYZ:END'
279
- })
280
- /*
281
- console.log('fn names')
282
- deepLog(parsedValue)
283
- /** */
284
- assert.equal(Array.isArray(parsedValue.blocks), true)
285
- assert.equal(parsedValue.blocks.length, 7)
286
-
287
- const values = [
288
- {
289
- type: 'functionName',
290
- options: { foo: { rad: 'yellow' } },
291
- location: 78
292
- },
293
- {
294
- type: 'functionName',
295
- options: { foo: { rad: 'blue' } },
296
- location: 157
297
- },
298
- {
299
- type: 'functionName',
300
- options: { foo: { rad: 'red' } },
301
- location: 235
302
- },
303
- {
304
- type: 'functionName',
305
- options: { foo: { rad: 'purple' } },
306
- location: 316
307
- },
308
- {
309
- type: 'functionName',
310
- options: { foo: { rad: 'black' } },
311
- location: 398
312
- },
313
- {
314
- type: 'functionName',
315
- options: { foo: { rad: 'white' } },
316
- location: 480
317
- },
318
- {
319
- type: 'functionName',
320
- options: { foo: { rad: 'orange' } },
321
- location: 563
322
- }
323
- ]
324
-
325
- values.forEach((val, i) => {
326
- const stub = val
327
- const currentItem = parsedValue.blocks[i]
328
- assert.equal(stub.type, currentItem.type, `${stub.type} ${i} transform`)
329
- assert.equal(stub.options, currentItem.options, `${stub.type} ${i} options`)
330
- })
331
- })
332
-
333
-
334
- test('different function names', () => {
335
- const backwardCompat = `
336
- <!-- XYZ:START functionName foo={{ rad: 'yellow' }} -->
337
- nice
338
- <!-- XYZ:END -->
339
-
340
- <!-- XYZ:START lol width={999}
341
- height={{111}}
342
- numberAsString="12345"
343
- great={["scoot", "sco ot", 'scooo ttt']}
344
- nope=false -->
345
- Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer vitae mauris arcu, eu pretium nisi. Praesent fringilla ornare ullamcorper. Pellentesque diam orci, sodales in blandit ut, placerat quis felis. Vestibulum at sem massa, in tempus nisi. Vivamus ut fermentum odio. Etiam porttitor faucibus volutpat. Vivamus vitae mi ligula, non hendrerit urna. Suspendisse potenti. Quisque eget massa a massa semper mollis.
346
- <!-- XYZ:END -->
347
-
348
- <!-- XYZ:START (CODE:src=./relative/path/to/code.js&lines=22-44) -->
349
- This content will be dynamically replaced with code from the file lines 22 through 44
350
- <!-- XYZ:END -->
351
- `
352
- const parsedValue = parseBlocks(backwardCompat, {
353
- open: 'XYZ:START',
354
- close: 'XYZ:END'
355
- })
356
- /*
357
- console.log('backwardCompat')
358
- deepLog(parsedValue)
359
- /** */
360
- const answers = [
361
- {
362
- type: 'functionName',
363
- options: { foo: { rad: 'yellow' } },
364
- },
365
- {
366
- type: 'lol',
367
- options: {
368
- width: 999,
369
- height: 111,
370
- numberAsString: '12345',
371
- great: [ 'scoot', 'sco ot', 'scooo ttt' ],
372
- nope: false
373
- },
374
- },
375
- {
376
- type: 'CODE',
377
- options: { src: './relative/path/to/code.js', lines: '22-44' },
378
- },
379
- ]
380
- parsedValue.blocks.forEach((transform, i) => {
381
- const stub = answers[i]
382
- assert.equal(transform.type, stub.type, `type: ${stub.type} at index ${i}`)
383
- assert.equal(transform.options, stub.options, `options: ${stub.type} at index ${i}`)
384
- })
385
- })
386
-
387
- const defaultOpts = {
388
- syntax: 'md',
389
- open: 'DOCS:START',
390
- close: 'DOCS:END',
391
- }
392
-
393
- const mdText = `
394
- Very nice
395
-
396
- <!-- DOCS:START(TOC) foo={{ rad: 'orange' }} ------>
397
- ok
398
- <!-- DOCS:END -->
399
-
400
- <!-- DOCS:START (CODE:src=./relative/path/to/code.js&lines=22-44) -->
401
- This content will be dynamically replaced with code from the file lines 22 through 44
402
- <!-- DOCS:END -->
403
- `
404
-
405
- test('Parse md blocks', () => {
406
- const parsedValue = parseBlocks(mdText, defaultOpts)
407
- /*
408
- deepLog(parsedValue)
409
- /** */
410
- assert.equal(parsedValue.blocks, [
411
- {
412
- index: 1,
413
- type: 'TOC',
414
- options: { foo: { rad: 'orange' } },
415
- context: { isMultiline: true },
416
- open: {
417
- value: "<!-- DOCS:START(TOC) foo={{ rad: 'orange' }} ------>\n",
418
- start: 12,
419
- end: 65
420
- },
421
- content: { value: 'ok', start: 65, end: 67, indentation: 0 },
422
- close: { value: '\n<!-- DOCS:END -->', start: 67, end: 85 },
423
- block: {
424
- indentation: '',
425
- lines: [ 4, 6 ],
426
- start: 12,
427
- end: 85,
428
- rawArgs: "foo={{ rad: 'orange' }}",
429
- rawContent: 'ok',
430
- value: "<!-- DOCS:START(TOC) foo={{ rad: 'orange' }} ------>\n" +
431
- 'ok\n' +
432
- '<!-- DOCS:END -->'
433
- }
434
- },
435
- {
436
- index: 2,
437
- type: 'CODE',
438
- options: { src: './relative/path/to/code.js', lines: '22-44' },
439
- context: { isMultiline: true, isLegacy: true },
440
- open: {
441
- value: '<!-- DOCS:START (CODE:src=./relative/path/to/code.js&lines=22-44) -->\n',
442
- start: 87,
443
- end: 157
444
- },
445
- content: {
446
- value: 'This content will be dynamically replaced with code from the file lines 22 through 44',
447
- start: 157,
448
- end: 242,
449
- indentation: 0
450
- },
451
- close: { value: '\n<!-- DOCS:END -->', start: 242, end: 260 },
452
- block: {
453
- indentation: '',
454
- lines: [ 8, 10 ],
455
- start: 87,
456
- end: 260,
457
- rawArgs: 'src=./relative/path/to/code.js&lines=22-44',
458
- rawContent: 'This content will be dynamically replaced with code from the file lines 22 through 44',
459
- value: '<!-- DOCS:START (CODE:src=./relative/path/to/code.js&lines=22-44) -->\n' +
460
- 'This content will be dynamically replaced with code from the file lines 22 through 44\n' +
461
- '<!-- DOCS:END -->'
462
- }
463
- }
464
- ], 'Array contains details')
465
- })
466
-
467
- test('Returns empty array', () => {
468
- assert.equal(parseBlocks('', defaultOpts).blocks, [])
469
- assert.equal(parseBlocks(' ', defaultOpts).blocks, [])
470
- assert.equal(parseBlocks(`
471
-
472
-
473
- `, defaultOpts).blocks, [])
474
- assert.equal(parseBlocks(`
475
- # No block in here
476
-
477
- nope
478
- `, defaultOpts).blocks, [])
479
- })
480
-
481
- test.run()
@@ -1,66 +0,0 @@
1
- const path = require('path')
2
- const isValidFile = require('is-valid-path')
3
- const { readFile, writeFile } = require('./utils/fs')
4
- const { processContents } = require('./process-contents')
5
-
6
- async function processFile(opts = {}) {
7
- const { content, syntax, outputPath, dryRun, patterns, output = {}, applyTransformsToSource } = opts
8
- const outputDir = output.directory || opts.outputDir
9
-
10
- let srcPath = opts.srcPath
11
- if (srcPath && content) {
12
- throw new Error(`Can't set both "srcPath" & "content"`)
13
- }
14
- let fileContents
15
- if (content) {
16
- const isFile = isValidFile(content)
17
- srcPath = (isFile) ? content : undefined
18
- fileContents = (!isFile) ? content : undefined
19
- }
20
-
21
- if (!fileContents) {
22
- fileContents = await readFile(srcPath || content, 'utf8')
23
- }
24
-
25
- let syntaxType = syntax
26
- if (srcPath && !syntaxType) {
27
- syntaxType = path.extname(srcPath).replace(/^\./, '')
28
- }
29
- // console.log('processFile order', srcPath)
30
-
31
- const result = await processContents(fileContents, {
32
- ...opts,
33
- outputPath,
34
- srcPath,
35
- syntax: syntaxType,
36
- })
37
- /*
38
- console.log('result', result)
39
- /** */
40
-
41
- if (dryRun) {
42
- return result
43
- }
44
-
45
- /* If it's changed or its a new file to write */
46
- if (result.isChanged || result.isNewPath) {
47
- let cleanContents = result.updatedContents
48
- if (result.stripComments && patterns.openPattern && patterns.closePattern) {
49
- cleanContents = result.updatedContents.replace(patterns.openPattern, '').replace(patterns.closePattern, '')
50
- }
51
- if (outputDir || (srcPath !== outputPath)) {
52
- // console.log(`- Update output file: ${outputPath}`)
53
- await writeFile(outputPath, cleanContents)
54
- }
55
- if (applyTransformsToSource) {
56
- // console.log(`- Update source file: ${srcPath}`)
57
- await writeFile(srcPath, result.updatedContents)
58
- }
59
- }
60
-
61
- return result
62
- }
63
-
64
- module.exports = {
65
- processFile
66
- }