Skip to content
Merged
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
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
## [0.2.0] - 05-05-2016
### Added
- Added #72, options --use

### Change
- Update #73, devDep
- Update config posthtml in package.json for test
- Update #75, readme
- Update tests for options --use

### Fixed
- Fixed linter report error
- Fixed #79, test for windows

## Removed
- Remove temp-write now used tempfile

## [0.1.2] - 28-04-2016
### Added
- Added #68, node 6 to config travis
Expand Down
33 changes: 28 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ npm install --global posthtml-cli
$ posthtml --help

Usage
posthtml --output|-o output.html --input|-i input.html [--config|-c path/to/json/config]
posthtml [-o output-file/directory|-r] [-i input-file/directory] [--config|-c path/to/file/config] [--use|-u plugin]

Options
--config, -c Path to JSON file [string]
Expand Down Expand Up @@ -62,7 +62,8 @@ $ posthtml --help

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

#### JS
#### ```JS```

```js
module.exports = {
bem: {
Expand All @@ -79,6 +80,7 @@ module.exports = {
}
}
```

#### ```JSON```

```json
Expand All @@ -100,12 +102,12 @@ module.exports = {

## Examples

### Example sample
### Sample
```console
$ posthtml -o output.html -i input.html
```

### Example options config
### Options config
```console
$ posthtml -o output.html -i input.html -c posthtml.json
```
Expand All @@ -114,7 +116,19 @@ $ posthtml -o output.html -i input.html -c posthtml.json
$ posthtml -o output.html -i input.html -c posthtml.js
```

### Example read dir
### Options use
```console
$ posthtml
-o output.html
-i input.html
-c config.json
-u posthtml-bem
--posthtml-bem.elemPrefix __
--posthtml-bem.elemMod _
-u posthtml-custom-elements
```

### Read dir
```console
$ posthtml -o outputFolder/ -i inputFolder/*.html
```
Expand All @@ -123,6 +137,15 @@ $ posthtml -o outputFolder/ -i inputFolder/*.html
$ posthtml -o outputFolder/ -i inputFolder/**/*.html
```

### Replace
```console
$ posthtml -i input.html -r
```

```console
$ posthtml -i inputFolder/*.html -r
```

## License
MIT

Expand Down
25 changes: 18 additions & 7 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
#!/usr/bin/env node

var posthtml = require('posthtml');
var globby = require('globby');
var path = require('path');
const pathExists = require('path-exists');
var fs = require('fs');
var posthtml = require('posthtml');
var globby = require('globby');
var pathExists = require('path-exists');
var argv = require('yargs')
.usage('Usage: $0 --output|-o output.html/outputFolder --input|-i input.html/inputFolder [--config|-c config.(js|json)] [--replace|-r]')
.usage('Usage: $0 [-o output-file/directory|-r] [-i input-file/directory] [--config|-c path/to/file/config]')
.example('posthtml -o output.html -i input.html', 'Default example')
.alias('i', 'input')
.array('input')
.demand(['i'])
.alias('o', 'output')
.alias('r', 'replace')
.demand(['i'])
.array('input')
.alias('u', 'use')
.array('use')
.pkgConf('posthtml')
.config('c')
.alias('c', 'config')
Expand All @@ -36,9 +38,17 @@ var argv = require('yargs')
function processing(file, output) {
// get htmls
var html = fs.readFileSync(file, 'utf8');
var ext = {};

// create config extends for posthtml-load-plugins
if (argv.use) {
argv.use.forEach(function (plugin) {
ext[plugin] = argv[plugin] || {};
});
}

// processing
posthtml(require('posthtml-load-plugins')(argv.config))
posthtml(require('posthtml-load-plugins')(argv.config, ext))
.process(html)
.then(function (result) {
fs.writeFileSync(output, result.html);
Expand Down Expand Up @@ -73,6 +83,7 @@ globby(argv.input).then(function (files) {
if (argv.output !== undefined) {
createFolder(argv.output);
}

files.forEach(function (file) {
var output = isFile(argv.output) ? argv.output : getOutput(file);
processing(file, output);
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "posthtml-cli",
"version": "0.1.2",
"version": "0.2.0",
"description": "CLI for posthtml",
"bin": {
"posthtml": "./cli.js"
Expand Down Expand Up @@ -30,8 +30,8 @@
"homepage": "https://github.com/GitScrum/posthtml-cli#readme",
"dependencies": {
"globby": "^4.0.0",
"posthtml": "^0.8.6",
"posthtml-load-plugins": "^0.9.5",
"posthtml": "^0.8.7",
"posthtml-load-plugins": "^0.10.0",
"yargs": "^4.6.0"
},
"devDependencies": {
Expand All @@ -41,15 +41,15 @@
"cpy": "^4.0.0",
"execa": "^0.4.0",
"nyc": "^6.4.1",
"path-exists": "^2.1.0",
"path-exists": "^3.0.0",
"posthtml-bem": "^0.2.2",
"posthtml-custom-elements": "^1.0.3",
"read-pkg": "^1.1.0",
"temp-write": "^2.1.0",
"tempfile": "^1.1.1",
"updtr": "^0.1.10",
"xo": "^0.13.0"
"xo": "^0.15.0"
},
"posthtml": {
"posthtmlCustomElements": {}
"customElements": {}
}
}
4 changes: 4 additions & 0 deletions test/expected/output-bem.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div class="myComponent MadTeaParty">
<div class="myTitle MadTeaParty__march-title">Super Title</div>
<div class="myText MadTeaParty__march-text">Awesome Text</div>
</div>
10 changes: 10 additions & 0 deletions test/fixtures/config-for-bem.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"bem": {
"elemPrefix": "__",
"modPrefix": "-",
"modDlmtr": "--"
},
"customElements": {
"defaultTag": "span"
}
}
4 changes: 4 additions & 0 deletions test/fixtures/input-bem.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<myComponent block="MadTeaParty">
<myTitle elem="march-title">Super Title</myTitle>
<myText elem="march-text">Awesome Text</myText>
</myComponent>
61 changes: 42 additions & 19 deletions test/test-cli.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const path = require('path');
const fs = require('fs');
const test = require('ava');
const execa = require('execa');
const tempWrite = require('temp-write');
const pathExists = require('path-exists');
const path = require('path');
const readPkg = require('read-pkg');
const copy = require('cpy');
const readFile = require('fs').readFile;
const tempfile = require('tempfile');
const cli = path.resolve('../cli.js');

function read(pathFile) {
return new Promise((resolve, reject) => {
readFile(pathFile, 'utf8', (err, data) => {
fs.readFile(pathFile, 'utf8', (err, data) => {
if (err) {
reject(err);
}
Expand All @@ -20,55 +20,55 @@ function read(pathFile) {
}

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

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

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

test('One of the arguments', t => {
const filename = tempWrite.sync('output.html');
t.throws(execa('../cli.js', ['-o', filename, '-r', '-i', 'fixtures/input.html']));
const filename = tempfile('.html');
t.throws(execa(cli, ['-o', filename, '-r', '-i', 'fixtures/input.html']));
});

test('Check version', async t => {
t.is((await execa('../cli.js', ['-v'])).stdout, (await readPkg(path.dirname(__dirname))).version);
t.is((await execa(cli, ['-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]);
const filename = tempfile('.html');
await execa(cli, ['-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]);
const filename = tempfile('.html');
await execa(cli, ['-i', 'fixtures/input-indent.html', '-o', filename]);
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']);
const filename = tempfile('.html');
await execa(cli, ['-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, ['-i', 'fixtures/*.html', '-o', `${folder}/`]);
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 +77,30 @@ 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, ['-i', `${folder}/*.html`, '-r']);
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 file and stdin options use', async t => {
t.plan(2);
const filename = tempfile('.html');
await execa(cli, [
'-o',
filename,
'-i',
'fixtures/input-bem.html',
'-c',
'fixtures/config.json',
'-u',
'posthtml-bem',
'--posthtml-bem.elemPrefix',
'__',
'--posthtml-bem.elemMod',
'_',
'-u',
'posthtml-custom-elements'
]);
t.true(await pathExists(filename));
t.is((await read('expected/output-bem.html')), (await read(filename)));
});