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
2 changes: 1 addition & 1 deletion ava.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default {
files: ['test/test-*'],
require: [
"@babel/register"
'@babel/register'
]
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"meow": "^6.0.1",
"merge-options": "^2.0.0",
"posthtml": "^0.12.0",
"to-camel-case": "^1.0.0"
"to-camel-case": "^1.0.0",
"update-notifier": "^4.1.0"
},
"devDependencies": {
"@babel/cli": "^7.8.3",
Expand Down
22 changes: 11 additions & 11 deletions src/cfg-resolve.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import {cosmiconfigSync} from 'cosmiconfig';
import toCamelCase from 'to-camel-case';
import mergeOptions from 'merge-options';
import { cosmiconfigSync } from 'cosmiconfig'
import toCamelCase from 'to-camel-case'
import mergeOptions from 'merge-options'

export default ({input, flags = {}}) => {
const explorer = cosmiconfigSync('posthtml');
let {config, use, output} = flags;
export default ({ input, flags = {} }) => {
const explorer = cosmiconfigSync('posthtml')
let { config, use, output } = flags

if (config) {
({config} = explorer.load(config));
({ config } = explorer.load(config))
}

if (use) {
use = [].concat(use).reduce((cfg, key) => mergeOptions(cfg, {plugins: {[key]: flags[toCamelCase(key)] || {}}}), {});
use = [].concat(use).reduce((cfg, key) => mergeOptions(cfg, { plugins: { [key]: flags[toCamelCase(key)] || {} } }), {})
}

if (!config && !use) {
({config} = explorer.search());
({ config } = explorer.search())
}

return mergeOptions({
input,
output
}, use || {}, config || {});
};
}, use || {}, config || {})
}
115 changes: 65 additions & 50 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
#!/usr/bin/env node

import path from 'path';
import fs from 'fs';
import fg from 'fast-glob';
import meow from 'meow';
import makeDir from 'make-dir';
import posthtml from 'posthtml';
import outResolve from './out-resolve';
import cfgResolve from './cfg-resolve';
import path from 'path'
import fs from 'fs'
import fg from 'fast-glob'
import meow from 'meow'
import makeDir from 'make-dir'
import updateNotifier from 'update-notifier'
import posthtml from 'posthtml'
import outResolve from './out-resolve'
import cfgResolve from './cfg-resolve'

const cli = meow(`
const package_ = require('../package.json')
updateNotifier({ pkg: package_ }).notify()

const cli = meow(
`
Usage: posthtml <patterns>

Options:
Expand All @@ -27,64 +32,74 @@ const cli = meow(`
$ posthtml input.html -o output.html -u posthtml-bem --posthtml-bem.elemPrefix __
$ posthtml inputFolder/*.html -o outputFolder
$ posthtml inputFolder/**/*.html -o outputFolder
`, {
flags: {
config: {
type: 'string',
alias: 'c'
},
version: {
type: 'boolean',
alias: 'v'
},
help: {
type: 'boolean',
alias: 'h'
},
output: {
type: 'string',
alias: 'o'
},
use: {
type: 'array',
alias: 'u'
`,
{
flags: {
config: {
type: 'string',
alias: 'c'
},
version: {
type: 'boolean',
alias: 'v'
},
help: {
type: 'boolean',
alias: 'h'
},
output: {
type: 'string',
alias: 'o'
},
use: {
type: 'array',
alias: 'u'
}
}
}
});
)

const read = file => new Promise(resolve => fs.readFile(file, 'utf8', (error, data) => {
if (error) {
console.warn(error);
}
const read = file =>
new Promise(resolve =>
fs.readFile(file, 'utf8', (error, data) => {
if (error) {
console.warn(error)
}

resolve(data);
}));
resolve(data)
})
)

const interopRequire = object => object && object.__esModule ? object.default : object;
const interopRequire = object =>
object && object.__esModule ? object.default : object

const getPlugins = config => Object.keys(config.plugins || {})
.map(plugin => interopRequire(require(plugin))(config.plugins[plugin]));
const getPlugins = config =>
Object.keys(config.plugins || {}).map(plugin =>
interopRequire(require(plugin))(config.plugins[plugin])
)

const config = cfgResolve(cli);
const config = cfgResolve(cli)

const processing = async file => {
const output = await outResolve(file, config.output);
const plugins = Array.isArray(config.plugins) ? config.plugins : getPlugins(config);
const output = await outResolve(file, config.output)
const plugins = Array.isArray(config.plugins)
? config.plugins
: getPlugins(config)

makeDir(path.dirname(output))
.then(read.bind(null, file))
.then(html => posthtml(plugins).process(html))
.then(({html}) => {
.then(({ html }) => {
fs.writeFile(output, html, error => {
if (error) {
console.warn(error);
console.warn(error)
}

console.log(`The file ${file} has been saved!`);
});
});
};
console.log(`The file ${file} has been saved!`)
})
})
}

fg.stream(config.input)
.on('data', processing)
.once('error', console.warn);
.once('error', console.warn)
10 changes: 5 additions & 5 deletions src/out-resolve.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import path from 'path';
import path from 'path'

export default (input, output) => new Promise(resolve => {
if (output && path.extname(output)) {
return resolve(output);
return resolve(output)
}

if (output) {
return resolve(path.join(output, path.basename(input)));
return resolve(path.join(output, path.basename(input)))
}

resolve(input);
});
resolve(input)
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ module.exports = {
plugins: [
require('posthtml-custom-elements')()
]
};
}
49 changes: 24 additions & 25 deletions test/test-cfg-resolve.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,50 @@
import test from 'ava';
import cfgResolve from '../lib/cfg-resolve';
import test from 'ava'
import cfgResolve from '../lib/cfg-resolve'

test('should return function', t => {
t.true(typeof cfgResolve === 'function');
});
t.true(typeof cfgResolve === 'function')
})

test('should return config with one use key without property', async t => {
const flags = {
use: 'posthtml-bem'
};
const config = await cfgResolve({flags});
const expected = {'posthtml-bem': {}};
}
const config = await cfgResolve({ flags })
const expected = { 'posthtml-bem': {} }

t.deepEqual(config.plugins, expected);
});
t.deepEqual(config.plugins, expected)
})

test('should return config with one use key with one property', async t => {
const flags = {
use: 'posthtml-bem',
posthtmlBem: {
prefix: '__'
}
};
const config = await cfgResolve({flags});
const expected = {'posthtml-bem': {prefix: '__'}};
}
const config = await cfgResolve({ flags })
const expected = { 'posthtml-bem': { prefix: '__' } }

t.deepEqual(config.plugins, expected);
});
t.deepEqual(config.plugins, expected)
})

test('should return config with key config', async t => {
const flags = {
config: 'test/config/.config'
};
const config = await cfgResolve({flags});
const expected = {'posthtml-bem': {}};
}
const config = await cfgResolve({ flags })
const expected = { 'posthtml-bem': {} }

t.deepEqual(config.plugins, expected);
});
t.deepEqual(config.plugins, expected)
})

test('should return config with key config and use key', async t => {
const flags = {
use: 'posthtml-assets',
config: 'test/config/.config'
};
const config = await cfgResolve({flags});
const expected = {'posthtml-bem': {}, 'posthtml-assets': {}};

t.deepEqual(config.plugins, expected);
});
}
const config = await cfgResolve({ flags })
const expected = { 'posthtml-bem': {}, 'posthtml-assets': {} }

t.deepEqual(config.plugins, expected)
})
Loading