|
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 pathExists = require('path-exists'); |
| 3 | +var path = require('path') |
| 4 | +var fs = require('fs') |
| 5 | +var posthtml = require('posthtml') |
| 6 | +var globby = require('globby') |
| 7 | +var pathExists = require('path-exists') |
8 | 8 | var argv = require('yargs')
|
9 |
| - .usage('Usage: $0 [-o output-file/directory|-r] [-i input-file/directory] [--config|-c path/to/file/config]') |
10 |
| - .example('posthtml -o output.html -i input.html', 'Default example') |
11 |
| - .alias('i', 'input') |
12 |
| - .array('input') |
13 |
| - .demand(['i']) |
14 |
| - .alias('o', 'output') |
15 |
| - .alias('r', 'replace') |
16 |
| - .alias('u', 'use') |
17 |
| - .array('use') |
18 |
| - .pkgConf('posthtml') |
19 |
| - .config('c') |
20 |
| - .alias('c', 'config') |
21 |
| - .version(function () { |
22 |
| - return require('./package.json').version; |
23 |
| - }) |
24 |
| - .alias('v', 'version') |
25 |
| - .help('h') |
26 |
| - .alias('h', 'help') |
27 |
| - .check(function (argv) { |
28 |
| - if (argv.output && argv.replace) { |
29 |
| - throw new Error('Both `output file` and `replace` provided: please use either --output or --replace option.'); |
30 |
| - } |
31 |
| - if (!argv.output && !argv.replace) { |
32 |
| - throw new Error('Both `output file` and `replace` missing: please use either --output or --replace option.'); |
33 |
| - } |
34 |
| - return true; |
35 |
| - }) |
36 |
| - .argv; |
| 9 | + .usage('Usage: $0 [-o output-file/directory|-r] [-i input-file/directory] [--config|-c path/to/file/config]') |
| 10 | + .example('posthtml -o output.html -i input.html', 'Default example') |
| 11 | + .alias('i', 'input') |
| 12 | + .array('input') |
| 13 | + .demand(['i']) |
| 14 | + .alias('o', 'output') |
| 15 | + .alias('r', 'replace') |
| 16 | + .alias('u', 'use') |
| 17 | + .array('use') |
| 18 | + .pkgConf('posthtml') |
| 19 | + .config('c') |
| 20 | + .alias('c', 'config') |
| 21 | + .version(function () { |
| 22 | + return require('./package.json').version |
| 23 | + }) |
| 24 | + .alias('v', 'version') |
| 25 | + .help('h') |
| 26 | + .alias('h', 'help') |
| 27 | + .check(function (argv) { |
| 28 | + if (argv.output && argv.replace) { |
| 29 | + throw new Error('Both `output file` and `replace` provided: please use either --output or --replace option.') |
| 30 | + } |
| 31 | + if (!argv.output && !argv.replace) { |
| 32 | + throw new Error('Both `output file` and `replace` missing: please use either --output or --replace option.') |
| 33 | + } |
| 34 | + return true |
| 35 | + }) |
| 36 | + .argv |
37 | 37 |
|
38 | 38 | function processing(file, output) {
|
39 |
| - // get htmls |
40 |
| - var html = fs.readFileSync(file, 'utf8'); |
41 |
| - var ext = {}; |
| 39 | + // get htmls |
| 40 | + var html = fs.readFileSync(file, 'utf8') |
| 41 | + var ext = {} |
42 | 42 |
|
43 |
| - // create config extends for posthtml-load-plugins |
44 |
| - if (argv.use) { |
45 |
| - argv.use.forEach(function (plugin) { |
46 |
| - ext[plugin] = argv[plugin] || {}; |
47 |
| - }); |
48 |
| - } |
| 43 | + // create config extends for posthtml-load-plugins |
| 44 | + if (argv.use) { |
| 45 | + argv.use.forEach(function (plugin) { |
| 46 | + ext[plugin] = argv[plugin] || {} |
| 47 | + }) |
| 48 | + } |
49 | 49 |
|
50 |
| - // processing |
51 |
| - posthtml(require('posthtml-load-plugins')(argv.config, ext)) |
52 |
| - .process(html) |
53 |
| - .then(function (result) { |
54 |
| - fs.writeFileSync(output, result.html); |
55 |
| - }); |
| 50 | + // processing |
| 51 | + posthtml(require('posthtml-load-plugins')(argv.config, ext)) |
| 52 | + .process(html) |
| 53 | + .then(function (result) { |
| 54 | + fs.writeFileSync(output, result.html) |
| 55 | + }) |
56 | 56 | }
|
57 | 57 |
|
58 | 58 | function isFile(outputPath) {
|
59 |
| - if (outputPath === undefined) { |
60 |
| - return false; |
61 |
| - } |
62 |
| - return Boolean(path.extname(outputPath)); |
| 59 | + if (outputPath === undefined) { |
| 60 | + return false |
| 61 | + } |
| 62 | + return Boolean(path.extname(outputPath)) |
63 | 63 | }
|
64 | 64 |
|
65 | 65 | function getOutput(file) {
|
66 |
| - if (argv.output === undefined) { |
67 |
| - return file; |
68 |
| - } |
69 |
| - return argv.output + path.basename(file); |
| 66 | + if (argv.output === undefined) { |
| 67 | + return file |
| 68 | + } |
| 69 | + return argv.output + path.basename(file) |
70 | 70 | }
|
71 | 71 |
|
72 | 72 | function createFolder(outputPath) {
|
73 |
| - if (isFile(outputPath) === true) { |
74 |
| - outputPath = path.dirname(outputPath); |
75 |
| - } |
| 73 | + if (isFile(outputPath) === true) { |
| 74 | + outputPath = path.dirname(outputPath) |
| 75 | + } |
76 | 76 |
|
77 |
| - if (pathExists.sync(outputPath) === false) { |
78 |
| - fs.mkdirSync(outputPath); |
79 |
| - } |
| 77 | + if (pathExists.sync(outputPath) === false) { |
| 78 | + fs.mkdirSync(outputPath) |
| 79 | + } |
80 | 80 | }
|
81 | 81 |
|
82 | 82 | globby(argv.input).then(function (files) {
|
83 |
| - if (argv.output !== undefined) { |
84 |
| - createFolder(argv.output); |
85 |
| - } |
| 83 | + if (argv.output !== undefined) { |
| 84 | + createFolder(argv.output) |
| 85 | + } |
86 | 86 |
|
87 |
| - files.forEach(function (file) { |
88 |
| - var output = isFile(argv.output) ? argv.output : getOutput(file); |
89 |
| - processing(file, output); |
90 |
| - }); |
91 |
| -}); |
| 87 | + files.forEach(function (file) { |
| 88 | + var output = isFile(argv.output) ? argv.output : getOutput(file) |
| 89 | + processing(file, output) |
| 90 | + }) |
| 91 | +}) |
0 commit comments