Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 11 additions & 80 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,111 +16,42 @@ npm install --global posthtml-cli
$ posthtml --help

Usage
posthtml --output|-o output.html --input|-i input.html [--config|-c path/to/json/config]
posthtml --output|-o output.html/outputFolder --input|-i input.html/inputFolder --use|-u plugin-name [--replace|-r]

Options
--config, -c Path to JSON file [string]
--use, -u posthtml plugin name [required]
--output, -o Output html file/folder result [required]
--input, -i Input html file/folder [required]
--replace, -r Replace input file(s) [boolean]
--help, -h Show help [boolean]
--version, -v Show version number [boolean]
```

## Config
*Default read options for plugins from package.json using [posthtml-load-plugins](https://github.com/michael-ciniawsky/posthtml-load-plugins)*

### ```package.json```

```json
{
"name": "my project",
"dependencies": {
"posthtml-bem": "^0.2.2",
"posthtml-each": "^1.0.1",
"posthtml-include": "^1.0.2"
},
"devDependencies": {
"posthtml-style-to-file": "^0.1.1"
},
"posthtml": {
"bem": {
"elemPrefix": "__",
"modPrefix": "-",
"modDlmtr": "--"
},
"include": {
"root": "./",
"encoding": "utf-8"
},
"styleToFile": {
"path": "./dist/style.css"
}
}
}
```

### ```[name].[ext]```

#### JS
```js
module.exports = {
bem: {
elemPrefix: '__',
modPrefix: '-',
modDlmtr: '--'
},
include: {
root: './',
encoding: 'utf-8'
},
styleToFile: {
path: './dist/style.css'
}
}
```
#### ```JSON```

```json
{
"bem": {
"elemPrefix": "__",
"modPrefix": "-",
"modDlmtr": "--"
},
"include": {
"root": "./",
"encoding": "utf-8"
},
"styleToFile": {
"path": "./dist/style.css"
}
}
--version, -v Show version number [boolean]
--local-plugins lookup plugins in current node_modules directory
```

## Examples

### Example sample
```console
$ posthtml -o output.html -i input.html
$ posthtml -o output.html -i input.html -u posthtml-custom-elements
```

### Example options config
### Pass in options for plugins (similar to [postcss-cli](https://github.com/postcss/postcss-cli))
```console
$ posthtml -o output.html -i input.html -c posthtml.json
$ posthtml -o output.html -i input.html -u posthtml-custom-elements --posthtml-custom-elements span
```

### Multiple plugins
```console
$ posthtml -o output.html -i input.html -c posthtml.js
$ posthtml -o outputFolder/ -i inputFolder/*.html -u posthtml-custom-elements -u posthtml-include
```

### Example read dir
```console
$ posthtml -o outputFolder/ -i inputFolder/*.html
$ posthtml -o outputFolder/ -i inputFolder/*.html -u posthtml-custom-elements
```

```console
$ posthtml -o outputFolder/ -i inputFolder/**/*.html
$ posthtml -o outputFolder/ -i inputFolder/**/*.html -u posthtml-custom-elements
```

## License
Expand Down
39 changes: 35 additions & 4 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
#!/usr/bin/env node

var posthtml = require('posthtml');
var resolve = require('resolve');
var globby = require('globby');
var path = require('path');
const pathExists = require('path-exists');
var fs = require('fs');
var argv = require('yargs')
.usage('Usage: $0 --output|-o output.html/outputFolder --input|-i input.html/inputFolder [--config|-c config.(js|json)] [--replace|-r]')
.example('posthtml -o output.html -i input.html', 'Default example')
.usage('Usage: $0 --output|-o output.html/outputFolder --input|-i input.html/inputFolder --use|-u plugin-name [--replace|-r]')
.example('posthtml -o output.html -i input.html -u plugin-name', 'Default example')
.alias('u', 'use')
.describe('u', 'posthtml plugin name (can be used multiple times)')
.option('local-plugins', {
describe: 'lookup plugins in current node_modules directory'
})
.alias('i', 'input')
.alias('o', 'output')
.alias('r', 'replace')
.demand(['i'])
.demand(['i', 'u'])
.array('input')
.pkgConf('posthtml')
.config('c')
Expand All @@ -23,6 +29,9 @@ var argv = require('yargs')
.help('h')
.alias('h', 'help')
.check(function (argv) {
if (!argv.use) {
throw new Error('Please specify at least one plugin name.');
}
if (argv.output && argv.replace) {
throw new Error('Both `output file` and `replace` provided: please use either --output or --replace option.');
}
Expand All @@ -33,12 +42,34 @@ var argv = require('yargs')
})
.argv;

if (!Array.isArray(argv.use)) {
argv.use = [argv.use];
}

// load and configure plugin array
var plugins = argv.use.map(function (name) {
var local = argv['local-plugins'];
var plugin;
if (local) {
var resolved = resolve.sync(name, {basedir: process.cwd()});
plugin = require(resolved);
} else {
plugin = require(name);
}
if (name in argv) {
plugin = plugin(argv[name]);
} else {
plugin = plugin.posthtml || plugin();
}
return plugin;
});

function processing(file, output) {
// get htmls
var html = fs.readFileSync(file, 'utf8');

// processing
posthtml(require('posthtml-load-plugins')(argv.config))
posthtml(plugins)
.process(html)
.then(function (result) {
fs.writeFileSync(output, result.html);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"dependencies": {
"globby": "^4.0.0",
"posthtml": "^0.8.6",
"posthtml-load-plugins": "^0.9.5",
"resolve": "^1.1.7",
"yargs": "^4.6.0"
},
"devDependencies": {
Expand Down
5 changes: 0 additions & 5 deletions test/fixtures/config.json

This file was deleted.

37 changes: 17 additions & 20 deletions test/test-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function read(pathFile) {
});
}

test('Missing required arguments -i, -o', t => {
test('Missing required arguments -i, -o, -u', t => {
t.throws(execa('../cli.js', []));
});

Expand All @@ -32,6 +32,11 @@ test('Missing required arguments -i', t => {
t.throws(execa('../cli.js', [`-o ${filename}`]));
});

test('Missing required arguments -u', t => {
const filename = tempWrite.sync('output.html');
t.throws(execa('../cli.js', [`-o ${filename} -i fixtures/input.html`]));
});

test('One of the arguments', t => {
const filename = tempWrite.sync('output.html');
t.throws(execa('../cli.js', ['-o', filename, '-r', '-i', 'fixtures/input.html']));
Expand All @@ -41,34 +46,18 @@ test('Check version', async t => {
t.is((await execa('../cli.js', ['-v'])).stdout, (await readPkg(path.dirname(__dirname))).version);
});

test('Transform html witch config in package.json', async t => {
t.plan(2);
const filename = await tempWrite('output.html', 'output.html');
await execa('../cli.js', ['-i', 'fixtures/input.html', '-o', filename]);
t.true(await pathExists(filename));
t.is((await read('expected/output-config-pkg.html')), (await read(filename)));
});

test('Transform html witch indent', async t => {
t.plan(2);
const filename = await tempWrite('output.html', 'output.html');
await execa('../cli.js', ['-i', 'fixtures/input-indent.html', '-o', filename]);
await execa('../cli.js', ['-i', 'fixtures/input-indent.html', '-o', filename, '-u', 'posthtml-custom-elements']);
t.true(await pathExists(filename));
t.is((await read('expected/output-indent.html')), (await read(filename)));
});

test('Transform html witch config in file', async t => {
t.plan(2);
const filename = await tempWrite('output.html', 'output.html');
await execa('../cli.js', ['-i', 'fixtures/input.html', '-o', filename, '-c', 'fixtures/config.json']);
t.true(await pathExists(filename));
t.is((await read('expected/output-config-file.html')), (await read(filename)));
});

test('Transform html from folder', async t => {
t.plan(2);
const folder = await tempfile();
await execa('../cli.js', ['-i', 'fixtures/*.html', '-o', `${folder}/`]);
await execa('../cli.js', ['-i', 'fixtures/*.html', '-o', `${folder}/`, '-u', 'posthtml-custom-elements']);
t.is((await read('expected/output-config-pkg.html')), (await read(`${folder}/input.html`)));
t.is((await read('expected/output-indent.html')), (await read(`${folder}/input-indent.html`)));
});
Expand All @@ -77,7 +66,15 @@ test('Transform html witch options replace', async t => {
t.plan(2);
const folder = await tempfile();
await copy(['fixtures/*.html'], `${folder}/`);
await execa('../cli.js', ['-i', `${folder}/*.html`, '-r']);
await execa('../cli.js', ['-i', `${folder}/*.html`, '-r', '-u', 'posthtml-custom-elements']);
t.is((await read('expected/output-config-pkg.html')), (await read(`${folder}/input.html`)));
t.is((await read('expected/output-indent.html')), (await read(`${folder}/input-indent.html`)));
});

test('Transform html witch config in command', async t => {
t.plan(2);
const filename = await tempWrite('output.html', 'output.html');
await execa('../cli.js', ['-i', 'fixtures/input.html', '-o', filename, '-u', 'posthtml-custom-elements', '--posthtml-custom-elements.defaultTag', 'span']);
t.true(await pathExists(filename));
t.is((await read('expected/output-config-file.html')), (await read(filename)));
});