UNPKG

3.33 kBJavaScriptView Raw
1#!/usr/bin/env node
2'use strict'
3
4process.on('unhandledRejection', function (reason, p) {
5 console.log('Possibly Unhandled Rejection at: Promise ', p, ' reason: ',
6 reason)
7})
8
9const meow = require('meow')
10const processFiles = require('./process-files')
11const normalizeNewline = require('normalize-newline')
12const relative = require('relative')
13const normalizePath = require('normalize-path')
14const chalk = require('chalk')
15const fs = require('fs')
16const path = require('path')
17const test = require('tape')
18const tapDiff = require('tap-diff')
19const readPkgUp = require('read-pkg-up')
20const resolve = require('resolve')
21const mos = require('.')
22const initCustomPlugins = require('./init-custom-plugins')
23
24const 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
50const highlightPath = chalk.bgBlack.yellow
51
52const isTest = ~['test', 't'].indexOf((cli.input[0] || '').toLowerCase())
53
54const cwd = process.cwd()
55
56readPkgUp({ 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
117function getRelativePath (filePath) {
118 return relative(cwd, filePath)
119}