-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcli.js
executable file
·126 lines (112 loc) · 3.15 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env node
import path from 'path';
import fs from 'fs';
import fg from 'fast-glob';
import meow from 'meow';
import makeDir from 'make-dir';
import updateNotifier from 'update-notifier';
import posthtml from 'posthtml';
import outResolve from './out-resolve';
import cfgResolve from './cfg-resolve';
import pluginResolve from './plugin-resolve';
const package_ = require('../package.json');
updateNotifier({pkg: package_}).notify();
const cli = meow(`
Usage: posthtml <patterns>
Options:
--output -o Output File or Folder
--config -c Path to config file
--use -u PostHTML plugin name
--root -r Mirror the directory structure relative to this path in the output directory(default: .)
--allInOutput -a Save the nesting structure for output
--help -h CLI Help
--version -v CLI Version
Examples:
$ posthtml input.html
$ posthtml input.html -o output.html
$ posthtml inputFolder/*.html !unicorn.html
$ posthtml input.html -o output.html -c posthtml.js
$ posthtml input.html -o output.html -u posthtml-bem --posthtml-bem.elemPrefix __
$ posthtml inputFolder/*.html -o outputFolder
$ posthtml inputFolder/**/*.html -o outputFolder -a
$ posthtml inputFolder/**/*.html -o outputFolder -a -r inputFolder
`, {
flags: {
config: {
type: 'string',
alias: 'c'
},
version: {
type: 'boolean',
alias: 'v'
},
help: {
type: 'boolean',
alias: 'h'
},
output: {
type: 'string',
alias: 'o'
},
use: {
type: 'string',
alias: 'u',
isMultiple: true
},
// https://github.com/sindresorhus/meow/issues/158
// options: {
// type: 'string',
// isMultiple: true
// },
root: {
type: 'string',
alias: 'r',
default: './'
},
allInOutput: {
type: 'boolean',
default: false,
alias: 'a'
},
skip: {
type: 'string',
alias: 's',
isMultiple: true
}
}
});
const read = file => new Promise(resolve => {
fs.readFile(file, 'utf8', (error, data) => {
if (error) {
console.warn(error);
}
resolve(data);
});
});
const interopRequire = object => object && object.__esModule ? object.default : object;
const getPlugins = config => Object.keys(config.plugins || {})
.map(plugin => interopRequire(require(pluginResolve(plugin, config.root)))(config.plugins[plugin]));
const config = cfgResolve(cli);
const processing = async file => {
const output = await outResolve(file, config);
const plugins = Array.isArray(config.plugins) ? config.plugins : getPlugins(config);
const skipParse = config.skip.includes(file);
makeDir(path.dirname(output))
.then(read.bind(undefined, file))
.then(html => Promise.resolve(posthtml(plugins).process(html, {
...config.options,
skipParse,
from: file
})))
.then(({html}) => {
fs.writeFile(output, html, error => {
if (error) {
console.warn(error);
}
console.log(`The file ${file} has been saved!`);
});
});
};
fg.stream(config.input)
.on('data', processing)
.once('error', console.warn);