1 | #!/usr/bin/env node
|
2 | 'use strict'
|
3 |
|
4 | process.on('unhandledRejection', function (reason, p) {
|
5 | console.log('Possibly Unhandled Rejection at: Promise ', p, ' reason: ',
|
6 | reason)
|
7 | })
|
8 |
|
9 | const meow = require('meow')
|
10 | const processFiles = require('./process-files')
|
11 | const normalizeNewline = require('normalize-newline')
|
12 | const relative = require('relative')
|
13 | const normalizePath = require('normalize-path')
|
14 | const chalk = require('chalk')
|
15 | const fs = require('fs')
|
16 | const path = require('path')
|
17 | const test = require('tape')
|
18 | const tapDiff = require('tap-diff')
|
19 | const readPkgUp = require('read-pkg-up')
|
20 | const resolve = require('resolve')
|
21 | const mos = require('.')
|
22 | const initCustomPlugins = require('./init-custom-plugins')
|
23 |
|
24 | const cli = meow([
|
25 | 'Usage',
|
26 | ' mos [test] [files]',
|
27 | '',
|
28 | 'Options',
|
29 | ' --help, -h Display usage',
|
30 | ' --version, -v Display version',
|
31 | ' --tap Generate TAP output when testing markdown files',
|
32 | '',
|
33 | ' -x="<exclude-pattern>"',
|
34 | ' Exclude pattern',
|
35 | '',
|
36 | 'Examples',
|
37 | ' mos',
|
38 | ' mos test',
|
39 | ' mos test README.md',
|
40 | '',
|
41 | 'Tips:',
|
42 | ' Add `mos test` to your `scripts.test` property in `package.json`',
|
43 | ].join('\n'), {
|
44 | alias: {
|
45 | help: 'h',
|
46 | version: 'v',
|
47 | },
|
48 | })
|
49 |
|
50 | const highlightPath = chalk.bgBlack.yellow
|
51 |
|
52 | const isTest = ~['test', 't'].indexOf((cli.input[0] || '').toLowerCase())
|
53 |
|
54 | const cwd = process.cwd()
|
55 |
|
56 | readPkgUp({ cwd })
|
57 | .then(result => {
|
58 | const pkgPlugins = Object
|
59 | .keys(result.pkg.dependencies || {})
|
60 | .concat(Object.keys(result.pkg.devDependencies || {}))
|
61 | .filter(dep => dep.indexOf('mos-plugin-') === 0)
|
62 | .map(dep => resolve.sync(dep, { basedir: cwd }))
|
63 | .map(normalizePath)
|
64 | .map(require)
|
65 |
|
66 | const processor = initCustomPlugins(mos())
|
67 | .use(pkgPlugins)
|
68 |
|
69 | const mdExtensions = ['markdown', 'mdown', 'mkdn', 'mkd', 'md']
|
70 | const files = cli.input[isTest ? 1 : 0]
|
71 | const pattern = files
|
72 | ? path.resolve(cwd, files)
|
73 | : path.resolve(cwd, `{/**/,/}*.{${mdExtensions.join()}}`)
|
74 | const ignorePattern = cli.flags.x
|
75 | ? path.resolve(cwd, cli.flags.x)
|
76 | : null
|
77 |
|
78 | if (isTest) {
|
79 | if (cli.flags.tap !== true) {
|
80 | test.createStream()
|
81 | .pipe(tapDiff())
|
82 | .pipe(process.stdout)
|
83 | }
|
84 | test('markdown', t => {
|
85 | processFiles({
|
86 | processor,
|
87 | pattern,
|
88 | ignorePattern,
|
89 | afterEachRender (opts) {
|
90 | const relativePath = normalizePath(getRelativePath(opts.filePath))
|
91 | t.equal(normalizeNewline(opts.newMD), normalizeNewline(opts.currentMD), relativePath)
|
92 | },
|
93 | })
|
94 | .then(() => t.end())
|
95 | .catch(err => { throw err })
|
96 | })
|
97 | } else {
|
98 | processFiles({
|
99 | processor,
|
100 | pattern,
|
101 | ignorePattern,
|
102 | afterEachRender (opts) {
|
103 | if (normalizeNewline(opts.newMD) !== normalizeNewline(opts.currentMD)) {
|
104 | fs.writeFileSync(opts.filePath, opts.newMD, {
|
105 | encoding: 'utf8',
|
106 | })
|
107 | const relativePath = normalizePath(getRelativePath(opts.filePath))
|
108 | console.log('updated', highlightPath(relativePath))
|
109 | }
|
110 | },
|
111 | })
|
112 | .catch(err => { throw err })
|
113 | }
|
114 | })
|
115 | .catch(err => { throw err })
|
116 |
|
117 | function getRelativePath (filePath) {
|
118 | return relative(cwd, filePath)
|
119 | }
|