|
1 | 1 | #!/usr/bin/env node |
2 | 2 |
|
3 | | -var path = require('path') |
4 | | -var fs = require('fs') |
5 | | -var posthtml = require('posthtml') |
6 | | -var globby = require('globby') |
7 | | -var argv = require('yargs') |
8 | | - .usage('Usage: $0 [-o output-file/directory|-r] [-i input-file/directory] [--config|-c path/to/file/config]') |
9 | | - .example('posthtml -o output.html -i input.html', 'Default example') |
10 | | - .alias('i', 'input') |
11 | | - .array('input') |
12 | | - .demand(['i']) |
13 | | - .alias('o', 'output') |
14 | | - .alias('r', 'replace') |
15 | | - .alias('u', 'use') |
16 | | - .array('use') |
17 | | - .pkgConf('posthtml') |
18 | | - .config('config', function (config) { |
19 | | - return JSON.parse(fs.readFileSync(config, 'utf-8')) |
20 | | - }) |
21 | | - .alias('c', 'config') |
22 | | - .version() |
23 | | - .alias('v', 'version') |
24 | | - .help('h') |
25 | | - .alias('h', 'help') |
26 | | - .check(function (argv) { |
27 | | - if (argv.output && argv.replace) { |
28 | | - throw new Error('Both `output file` and `replace` provided: please use either --output or --replace option.') |
29 | | - } |
30 | | - if (!argv.output && !argv.replace) { |
31 | | - throw new Error('Both `output file` and `replace` missing: please use either --output or --replace option.') |
32 | | - } |
33 | | - return true |
34 | | - }) |
35 | | - .argv |
36 | | - |
37 | | -function processing (file, output) { |
38 | | - // get htmls |
39 | | - var html = fs.readFileSync(file, 'utf8') |
40 | | - var plugins |
41 | | - var fileConfig = argv.config && JSON.parse(fs.readFileSync(argv.config, 'utf-8')) |
| 3 | +import path from 'path'; |
| 4 | +import fs from 'fs'; |
| 5 | +import fg from 'fast-glob'; |
| 6 | +import meow from 'meow'; |
| 7 | +import makeDir from 'make-dir'; |
| 8 | +import posthtml from 'posthtml'; |
| 9 | +import load from 'post-load-plugins'; |
| 10 | +import outResolve from './out-resolve'; |
| 11 | +import cfgResolve from './cfg-resolve'; |
42 | 12 |
|
43 | | - if (argv.autoOff) { |
44 | | - var use = argv.use ? argv.use : [] |
45 | | - var cfg = argv.config ? Object.keys(fileConfig) : [] |
46 | | - plugins = [].concat(use, cfg).map((plugin) => { |
47 | | - try { |
48 | | - return require(plugin)(argv[plugin]) |
49 | | - } catch (err) { |
50 | | - if (err.code === 'MODULE_NOT_FOUND') { |
51 | | - throw new TypeError('Plugin Error: Cannot find module ' + plugin) |
52 | | - } |
53 | | - } |
54 | | - }) |
55 | | - } else { |
56 | | - // config |
57 | | - var config = {} |
| 13 | +const cli = meow(` |
| 14 | + Usage: posthtml <patterns> |
58 | 15 |
|
59 | | - // create config extends for post-load-plugins |
60 | | - if (argv.use) { |
61 | | - argv.use.forEach(function (plugin) { |
62 | | - config[plugin] = argv[plugin] || {} |
63 | | - }) |
64 | | - } |
| 16 | + Options: |
| 17 | + --output -o Output File or Folder |
| 18 | + --config -c Path to config file |
| 19 | + --use -u PostHTML plugin name |
| 20 | + --help -h CLI Help |
| 21 | + --auto-off Disable automatically loads plug-ins with configuration from package.json |
| 22 | + --version -v CLI Version |
65 | 23 |
|
66 | | - if (argv.config) { |
67 | | - config = Object.assign(fileConfig, config) |
| 24 | + Examples: |
| 25 | + $ posthtml input.html |
| 26 | + $ posthtml input.html -o output.html |
| 27 | + $ posthtml inputFolder/*.html !unicorn.html |
| 28 | + $ posthtml input.html -o output.html -c posthtml.js |
| 29 | + $ posthtml input.html -o output.html -u posthtml-bem --posthtml-bem.elemPrefix __ |
| 30 | + $ posthtml inputFolder/*.html -o outputFolder |
| 31 | + $ posthtml inputFolder/**/*.html -o outputFolder |
| 32 | +`, { |
| 33 | + flags: { |
| 34 | + config: { |
| 35 | + type: 'string', |
| 36 | + alias: 'c' |
| 37 | + }, |
| 38 | + version: { |
| 39 | + type: 'boolean', |
| 40 | + alias: 'v' |
| 41 | + }, |
| 42 | + help: { |
| 43 | + type: 'boolean', |
| 44 | + alias: 'h' |
| 45 | + }, |
| 46 | + output: { |
| 47 | + type: 'string', |
| 48 | + alias: 'o' |
| 49 | + }, |
| 50 | + use: { |
| 51 | + type: 'Array', |
| 52 | + alias: 'u' |
68 | 53 | } |
69 | 54 | } |
| 55 | +}); |
70 | 56 |
|
71 | | - // processing |
72 | | - posthtml(argv.autoOff ? plugins : require('post-load-plugins')(config)) |
73 | | - .process(html) |
74 | | - .then(function (result) { |
75 | | - fs.writeFileSync(output, result.html) |
76 | | - }) |
77 | | -} |
78 | | - |
79 | | -function isFile (outputPath) { |
80 | | - if (outputPath === undefined) { |
81 | | - return false |
82 | | - } |
83 | | - return Boolean(path.extname(outputPath)) |
84 | | -} |
85 | | - |
86 | | -function getOutput (file) { |
87 | | - if (argv.output === undefined) { |
88 | | - return file |
| 57 | +const read = file => new Promise(resolve => fs.readFile(file, 'utf8', (err, data) => { |
| 58 | + if (err) { |
| 59 | + console.warn(err); |
89 | 60 | } |
90 | | - return argv.output + path.basename(file) |
91 | | -} |
| 61 | + resolve(data); |
| 62 | +})); |
92 | 63 |
|
93 | | -function createFolder (outputPath) { |
94 | | - if (isFile(outputPath) === true) { |
95 | | - outputPath = path.dirname(outputPath) |
96 | | - } |
| 64 | +const processing = async file => { |
| 65 | + const output = await outResolve(file, cli.flags.output); |
| 66 | + const config = await cfgResolve(cli.flags); |
97 | 67 |
|
98 | | - if (fs.existsSync(outputPath) === false) { |
99 | | - fs.mkdirSync(outputPath) |
100 | | - } |
101 | | -} |
| 68 | + console.log(config); |
102 | 69 |
|
103 | | -globby(argv.input).then(function (files) { |
104 | | - if (argv.output !== undefined) { |
105 | | - createFolder(argv.output) |
106 | | - } |
| 70 | + makeDir(path.dirname(output)) |
| 71 | + .then(read.bind(null, file)) |
| 72 | + .then(html => posthtml(load(config)).process(html)) |
| 73 | + .then(({html}) => { |
| 74 | + fs.writeFile(output, html, err => { |
| 75 | + if (err) { |
| 76 | + console.warn(err); |
| 77 | + } |
| 78 | + console.log(`The file ${file} has been saved!`); |
| 79 | + }); |
| 80 | + }); |
| 81 | +}; |
107 | 82 |
|
108 | | - files.forEach(function (file) { |
109 | | - var output = isFile(argv.output) ? argv.output : getOutput(file) |
110 | | - processing(file, output) |
111 | | - }) |
112 | | -}) |
| 83 | +fg.stream(cli.input) |
| 84 | + .on('data', processing) |
| 85 | + .once('error', console.warn); |
0 commit comments