Skip to content

Commit fbee998

Browse files
author
Ivan Demidov
committed
Fixed #16, Fixed #9, add replace and read/write folder
1 parent a4d3f63 commit fbee998

File tree

2 files changed

+60
-9
lines changed

2 files changed

+60
-9
lines changed

cli.js

+57-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
#!/usr/bin/env node
22

33
var posthtml = require('posthtml');
4+
var globby = require('globby');
5+
var path = require('path');
6+
const pathExists = require('path-exists');
47
var fs = require('fs');
58
var argv = require('yargs')
6-
.usage('Usage: $0 --output|-o output.html --input|-i input.html [--config|-c config.(js|json)]')
9+
.usage('Usage: $0 --output|-o output.html/outputFolder --input|-i input.html/inputFolder [--config|-c config.(js|json)] [--replace|-r]')
710
.example('posthtml -o output.html -i input.html', 'Default example')
8-
.demand(['o', 'i'])
911
.alias('i', 'input')
1012
.alias('o', 'output')
13+
.alias('r', 'replace')
14+
.demand(['i'])
15+
.array('input')
1116
.pkgConf('posthtml')
1217
.config('c')
1318
.alias('c', 'config')
@@ -17,14 +22,57 @@ var argv = require('yargs')
1722
.alias('v', 'version')
1823
.help('h')
1924
.alias('h', 'help')
25+
.check(function (argv) {
26+
if (argv.output && argv.replace) {
27+
throw new Error('Both `output file` and `replace` provided: please use either --output or --replace option.');
28+
}
29+
if (!argv.output && !argv.replace) {
30+
throw new Error('Both `output file` and `replace` missing: please use either --output or --replace option.');
31+
}
32+
return true;
33+
})
2034
.argv;
2135

22-
// get htmls
23-
var html = fs.readFileSync(argv.input, 'utf8');
36+
function processing(file, output) {
37+
// get htmls
38+
var html = fs.readFileSync(file, 'utf8');
39+
40+
// processing
41+
posthtml(require('posthtml-load-plugins')(argv.config))
42+
.process(html)
43+
.then(function (result) {
44+
fs.writeFileSync(output, result.html);
45+
});
46+
}
47+
48+
function isFile(outputPath) {
49+
return Boolean(path.extname(outputPath));
50+
}
51+
52+
function getOutput(file) {
53+
if (argv.output === undefined) {
54+
return file;
55+
}
56+
return argv.output + path.basename(file);
57+
}
58+
59+
function createFolder(outputPath) {
60+
if (isFile(outputPath) === true) {
61+
outputPath = path.dirname(outputPath);
62+
}
63+
64+
if (pathExists.sync(outputPath) === false) {
65+
fs.mkdirSync(outputPath);
66+
}
67+
}
68+
69+
globby(argv.input).then(function (files) {
70+
if (argv.output !== undefined) {
71+
createFolder(argv.output);
72+
}
2473

25-
// processing
26-
posthtml(require('posthtml-load-plugins')(argv.config))
27-
.process(html)
28-
.then(function (result) {
29-
fs.writeFileSync(argv.output, result.html);
74+
files.forEach(function (file) {
75+
var output = isFile(argv.output) ? argv.output : getOutput(file);
76+
processing(file, output);
3077
});
78+
});

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@
2828
},
2929
"homepage": "https://github.com/GitScrum/posthtml-cli#readme",
3030
"dependencies": {
31+
"del": "^2.2.0",
32+
"globby": "^4.0.0",
3133
"posthtml": "^0.8.3",
3234
"posthtml-load-plugins": "^0.9.5",
3335
"yargs": "^4.3.1"
3436
},
3537
"devDependencies": {
3638
"ava": "^0.13.0",
3739
"coveralls": "^2.11.8",
40+
"cpy": "^4.0.0",
3841
"execa": "^0.2.2",
3942
"nyc": "^6.1.1",
4043
"path-exists": "^2.1.0",

0 commit comments

Comments
 (0)