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 resolve = require('resolve')
|
10 | const cwd = process.cwd()
|
11 |
|
12 | let localCLI
|
13 | try {
|
14 | localCLI = resolve.sync('mos/lib/cli', { basedir: cwd })
|
15 | } catch (err) {
|
16 | localCLI = __filename
|
17 | }
|
18 |
|
19 | if (localCLI && localCLI !== __filename) {
|
20 | console.log('Using local install of mos')
|
21 | require(localCLI)
|
22 | return
|
23 | }
|
24 |
|
25 | const updateNotifier = require('update-notifier')
|
26 | const meow = require('meow')
|
27 | const processFiles = require('./process-files')
|
28 | const normalizeNewline = require('normalize-newline')
|
29 | const relative = require('relative')
|
30 | const normalizePath = require('normalize-path')
|
31 | const chalk = require('chalk')
|
32 | const fs = require('fs')
|
33 | const path = require('path')
|
34 | const test = require('tape')
|
35 | const tapDiff = require('@zkochan/tap-diff')
|
36 | const readPkgUp = require('@zkochan/read-pkg-up')
|
37 | const mos = require('.')
|
38 | const initCustomPlugins = require('./init-custom-plugins')
|
39 |
|
40 | const cli = meow([
|
41 | 'Usage',
|
42 | ' mos [test] [files]',
|
43 | '',
|
44 | 'Options',
|
45 | ' --init Add mos to your project',
|
46 | ' --help, -h Display usage',
|
47 | ' --version, -v Display version',
|
48 | ' --tap Generate TAP output when testing markdown files',
|
49 | '',
|
50 | ' -x="<exclude-pattern>"',
|
51 | ' Exclude pattern',
|
52 | '',
|
53 | 'Examples',
|
54 | ' mos',
|
55 | ' mos test',
|
56 | ' mos test README.md',
|
57 | ' mos --init',
|
58 | ' mos --init README.md',
|
59 | '',
|
60 | 'Tips:',
|
61 | ' Add `mos test` to your `scripts.test` property in `package.json`',
|
62 | ].join('\n'), {
|
63 | alias: {
|
64 | help: 'h',
|
65 | version: 'v',
|
66 | },
|
67 | })
|
68 |
|
69 | updateNotifier({pkg: cli.pkg}).notify()
|
70 |
|
71 | if (cli.flags.init) {
|
72 | require('mos-init')()
|
73 | return
|
74 | }
|
75 |
|
76 | const highlightPath = chalk.bgBlack.yellow
|
77 |
|
78 | const isTest = ~['test', 't'].indexOf((cli.input[0] || '').toLowerCase())
|
79 |
|
80 | readPkgUp({ cwd })
|
81 | .then(result => {
|
82 | const pkgPlugins = Object
|
83 | .keys(result.pkg.dependencies || {})
|
84 | .concat(Object.keys(result.pkg.devDependencies || {}))
|
85 | .filter(dep => dep.indexOf('mos-plugin-') === 0)
|
86 | .map(dep => resolve.sync(dep, { basedir: cwd }))
|
87 | .map(normalizePath)
|
88 | .map(require)
|
89 |
|
90 | const processor = initCustomPlugins(mos())
|
91 | .use(pkgPlugins)
|
92 |
|
93 | const mdExtensions = ['markdown', 'mdown', 'mkdn', 'mkd', 'md']
|
94 | const files = cli.input[isTest ? 1 : 0]
|
95 | const pattern = files
|
96 | ? path.resolve(cwd, files)
|
97 | : path.resolve(cwd, `{/**/,/}*.{${mdExtensions.join()}}`)
|
98 | const ignorePattern = cli.flags.x
|
99 | ? path.resolve(cwd, cli.flags.x)
|
100 | : null
|
101 |
|
102 | if (isTest) {
|
103 | if (cli.flags.tap !== true) {
|
104 | test.createStream()
|
105 | .pipe(tapDiff())
|
106 | .pipe(process.stdout)
|
107 | }
|
108 | test('markdown', t => {
|
109 | processFiles({
|
110 | processor,
|
111 | pattern,
|
112 | ignorePattern,
|
113 | afterEachRender (opts) {
|
114 | const relativePath = normalizePath(getRelativePath(opts.filePath))
|
115 | t.equal(normalizeNewline(opts.newMD), normalizeNewline(opts.currentMD), relativePath)
|
116 | },
|
117 | })
|
118 | .then(() => t.end())
|
119 | .catch(err => { throw err })
|
120 | })
|
121 | } else {
|
122 | processFiles({
|
123 | processor,
|
124 | pattern,
|
125 | ignorePattern,
|
126 | afterEachRender (opts) {
|
127 | if (normalizeNewline(opts.newMD) !== normalizeNewline(opts.currentMD)) {
|
128 | fs.writeFileSync(opts.filePath, opts.newMD, {
|
129 | encoding: 'utf8',
|
130 | })
|
131 | const relativePath = normalizePath(getRelativePath(opts.filePath))
|
132 | console.log('updated', highlightPath(relativePath))
|
133 | }
|
134 | },
|
135 | })
|
136 | .catch(err => { throw err })
|
137 | }
|
138 | })
|
139 | .catch(err => { throw err })
|
140 |
|
141 | function getRelativePath (filePath) {
|
142 | return relative(cwd, filePath)
|
143 | }
|