Skip to content

Commit 2c8decd

Browse files
committed
refactor: close #28, close #91, close #259, close #260, close #263
1 parent 3558732 commit 2c8decd

File tree

2 files changed

+112
-120
lines changed

2 files changed

+112
-120
lines changed

package.json

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"description": "CLI for posthtml",
55
"license": "MIT",
66
"repository": "posthtml/posthtml-cli",
7+
"homepage": "posthtml/posthtml-cli#readme",
8+
"bugs": "posthtml/posthtml-cli/issues",
79
"author": {
810
"name": "Ivan Demidov",
911
"email": "Scrum@list.ru",
@@ -22,6 +24,8 @@
2224
"postpublish": "conventional-changelog -i changelog.md -s -r 0 && git commit -am \"build: update changelog\"",
2325
"prepare": "npm run build",
2426
"build": "rimraf lib && babel src -d lib",
27+
"lint": "eslint src/cli.js",
28+
"pretest": "npm run build",
2529
"test": "nyc ava"
2630
},
2731
"files": [
@@ -33,67 +37,77 @@
3337
"cli"
3438
],
3539
"dependencies": {
36-
"globby": "^7.1.1",
37-
"post-load-plugins": "0.0.22",
38-
"posthtml": "^0.11.2",
39-
"yargs": "^11.0.0"
40+
"fast-glob": "^2.0.4",
41+
"get-cff": "^0.1.1",
42+
"make-dir": "^1.2.0",
43+
"meow": "^4.0.0",
44+
"path-exists": "^3.0.0",
45+
"post-load-plugins": "0.0.23",
46+
"posthtml": "^0.11.3",
47+
"to-camel-case": "^1.0.0"
4048
},
4149
"devDependencies": {
50+
"@babel/cli": "^7.0.0-beta.40",
51+
"@babel/core": "^7.0.0-beta.40",
52+
"@babel/node": "^7.0.0-beta.40",
53+
"@babel/preset-env": "^7.0.0-beta.40",
54+
"@babel/register": "^7.0.0-beta.40",
4255
"@commitlint/cli": "^6.1.0",
4356
"@commitlint/config-angular": "^6.1.0",
4457
"ava": "*",
45-
"babel-cli": "^6.24.1",
46-
"babel-eslint": "^8.2.1",
47-
"babel-preset-env": "^1.3.2",
58+
"babel-eslint": "^8.2.2",
4859
"babel-preset-minify": "^0.3.0",
49-
"babel-register": "^6.24.1",
5060
"clinton": "^0.13.0",
51-
"conventional-changelog-cli": "^1.3.9",
61+
"conventional-changelog-cli": "^1.3.14",
5262
"coveralls": "^3.0.0",
5363
"cpy": "^6.0.0",
54-
"eslint": "^4.17.0",
55-
"eslint-plugin-ava": "^4.5.0",
64+
"deep-assign": "^2.0.0",
65+
"eslint": "^4.18.0",
66+
"eslint-config-xo": "^0.20.1",
67+
"eslint-config-xo-space": "^0.18.0",
68+
"eslint-plugin-ava": "^4.5.1",
5669
"eslint-plugin-babel": "^4.1.2",
5770
"eslint-plugin-html": "^4.0.1",
5871
"eslint-plugin-import": "^2.8.0",
5972
"eslint-plugin-markdown": "^1.0.0-beta.7",
6073
"eslint-plugin-node": "^6.0.0",
6174
"eslint-plugin-promise": "^3.6.0",
62-
"eslint-plugin-unicorn": "^3.0.1",
75+
"eslint-plugin-unicorn": "^4.0.2",
6376
"execa": "^0.9.0",
6477
"husky": "^0.14.3",
65-
"lint-staged": "^6.1.0",
66-
"nyc": "^11.4.1",
67-
"path-exists": "^3.0.0",
78+
"lint-staged": "^6.1.1",
79+
"nyc": "^11.5.0",
6880
"posthtml-bem": "^0.2.2",
6981
"posthtml-css-modules": "^0.1.3",
7082
"posthtml-custom-elements": "^1.1.0",
7183
"read-pkg": "^3.0.0",
7284
"tempfile": "^2.0.0"
7385
},
86+
"readme": "posthtml/posthtml-cli/readme",
7487
"lint-staged": {
75-
"{src,test}*.js": "eslint",
88+
"{src,test}/**/*.js": "eslint",
7689
"*.md": "eslint"
7790
},
7891
"ava": {
7992
"require": [
80-
"babel-register"
93+
"@babel/register"
8194
]
8295
},
8396
"babel": {
8497
"presets": [
8598
[
86-
"env",
99+
"@babel/preset-env",
87100
{
88101
"targets": {
89102
"node": 4
90103
},
91-
"useBuiltIns": true
104+
"useBuiltIns": "usage"
92105
}
93106
],
94107
[
95108
"minify",
96109
{
110+
"removeUndefined": false,
97111
"mangle": false
98112
}
99113
]
@@ -113,8 +127,13 @@
113127
],
114128
"extends": [
115129
"plugin:unicorn/recommended",
116-
"plugin:ava/recommended"
117-
]
130+
"plugin:ava/recommended",
131+
"xo",
132+
"xo-space"
133+
],
134+
"rules": {
135+
"capitalized-comments": "off"
136+
}
118137
},
119138
"clinton": {
120139
"ignores": [

src/cli.js

Lines changed: 72 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,85 @@
11
#!/usr/bin/env node
22

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';
4212

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>
5815
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
6523
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'
6853
}
6954
}
55+
});
7056

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);
8960
}
90-
return argv.output + path.basename(file)
91-
}
61+
resolve(data);
62+
}));
9263

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);
9767

98-
if (fs.existsSync(outputPath) === false) {
99-
fs.mkdirSync(outputPath)
100-
}
101-
}
68+
console.log(config);
10269

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+
};
10782

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

Comments
 (0)