From 8e31a7334aa6f956556b8b16474d5bc129353cea Mon Sep 17 00:00:00 2001 From: anikethsaha Date: Mon, 17 Feb 2020 20:41:08 +0530 Subject: [PATCH] chore: added update-notifier for cli --- ava.config.js | 2 +- package.json | 3 +- src/cfg-resolve.js | 22 +-- src/cli.js | 115 ++++++----- src/out-resolve.js | 10 +- .../posthtml.config.js | 2 +- test/test-cfg-resolve.js | 49 +++-- test/test-cli.js | 182 +++++++++--------- test/test-out-resolve.js | 45 +++-- 9 files changed, 222 insertions(+), 208 deletions(-) diff --git a/ava.config.js b/ava.config.js index c6ae75d..115ab44 100644 --- a/ava.config.js +++ b/ava.config.js @@ -1,6 +1,6 @@ export default { files: ['test/test-*'], require: [ - "@babel/register" + '@babel/register' ] } diff --git a/package.json b/package.json index 39bc5aa..6e6d134 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/cfg-resolve.js b/src/cfg-resolve.js index 82ac4dd..9239985 100644 --- a/src/cfg-resolve.js +++ b/src/cfg-resolve.js @@ -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 || {}) +} diff --git a/src/cli.js b/src/cli.js index e43c3f5..99bf7e3 100755 --- a/src/cli.js +++ b/src/cli.js @@ -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 Options: @@ -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) diff --git a/src/out-resolve.js b/src/out-resolve.js index be33bbc..27b5c66 100644 --- a/src/out-resolve.js +++ b/src/out-resolve.js @@ -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) +}) diff --git a/test/fixtures/by-config/one-io-and-plugins-array/posthtml.config.js b/test/fixtures/by-config/one-io-and-plugins-array/posthtml.config.js index 65333c4..dd57856 100644 --- a/test/fixtures/by-config/one-io-and-plugins-array/posthtml.config.js +++ b/test/fixtures/by-config/one-io-and-plugins-array/posthtml.config.js @@ -4,4 +4,4 @@ module.exports = { plugins: [ require('posthtml-custom-elements')() ] -}; +} diff --git a/test/test-cfg-resolve.js b/test/test-cfg-resolve.js index 78ea45c..9f4375d 100644 --- a/test/test-cfg-resolve.js +++ b/test/test-cfg-resolve.js @@ -1,19 +1,19 @@ -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 = { @@ -21,31 +21,30 @@ test('should return config with one use key with one property', async t => { 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) +}) diff --git a/test/test-cli.js b/test/test-cli.js index c34ab36..556828c 100644 --- a/test/test-cli.js +++ b/test/test-cli.js @@ -1,66 +1,66 @@ -import path from 'path'; -import fs from 'fs'; -import test from 'ava'; -import execa from 'execa'; -import pathExists from 'path-exists'; -import readPkg from 'read-pkg'; +import path from 'path' +import fs from 'fs' +import test from 'ava' +import execa from 'execa' +import pathExists from 'path-exists' +import readPkg from 'read-pkg' // import copy from 'cpy'; -import tempfile from 'tempfile'; +import tempfile from 'tempfile' -const cli = path.resolve('lib/cli.js'); +const cli = path.resolve('lib/cli.js') const read = file => new Promise((resolve, reject) => fs.readFile(file, 'utf8', (error, data) => { if (error) { - return reject(error); + return reject(error) } - resolve(data); -})); + resolve(data) +})) test('Check version', async t => { - const {stdout} = await execa(cli, ['-v']); - const {version} = await readPkg(path.dirname(__dirname)); - t.is(stdout, version); -}); + const { stdout } = await execa(cli, ['-v']) + const { version } = await readPkg(path.dirname(__dirname)) + t.is(stdout, version) +}) test('Transform html witch config in package.json', async t => { - t.plan(2); - const filename = tempfile('.html'); - await execa(cli, ['test/fixtures/input.html', '-o', filename]); - t.true(await pathExists(filename)); - t.is((await read('test/expected/output-config-pkg.html')), (await read(filename))); -}); + t.plan(2) + const filename = tempfile('.html') + await execa(cli, ['test/fixtures/input.html', '-o', filename]) + t.true(await pathExists(filename)) + t.is((await read('test/expected/output-config-pkg.html')), (await read(filename))) +}) test('Transform html witch indent', async t => { - t.plan(2); - const filename = tempfile('.html'); - await execa(cli, ['test/fixtures/input-indent.html', '-o', filename]); - t.true(await pathExists(filename)); - t.is((await read('test/expected/output-indent.html')), (await read(filename))); -}); + t.plan(2) + const filename = tempfile('.html') + await execa(cli, ['test/fixtures/input-indent.html', '-o', filename]) + t.true(await pathExists(filename)) + t.is((await read('test/expected/output-indent.html')), (await read(filename))) +}) test('Transform html witch config in file', async t => { - t.plan(2); - const filename = tempfile('.html'); - await execa(cli, ['test/fixtures/input.html', '-o', filename, '-c', 'test/fixtures/config.json']); - t.true(await pathExists(filename)); - t.is((await read('test/expected/output-config-file.html')), (await read(filename))); -}); + t.plan(2) + const filename = tempfile('.html') + await execa(cli, ['test/fixtures/input.html', '-o', filename, '-c', 'test/fixtures/config.json']) + t.true(await pathExists(filename)) + t.is((await read('test/expected/output-config-file.html')), (await read(filename))) +}) test('Transform html witch dot config in file', async t => { - t.plan(2); - const filename = tempfile('.html'); - await execa(cli, ['test/fixtures/input.html', '-o', filename, '-c', 'test/fixtures/.config']); - t.true(await pathExists(filename)); - t.is((await read('test/expected/output-config-file.html')), (await read(filename))); -}); + t.plan(2) + const filename = tempfile('.html') + await execa(cli, ['test/fixtures/input.html', '-o', filename, '-c', 'test/fixtures/.config']) + t.true(await pathExists(filename)) + t.is((await read('test/expected/output-config-file.html')), (await read(filename))) +}) test('Transform html from two file', async t => { - t.plan(2); - const folder = await tempfile(); - await execa(cli, ['test/fixtures/input.html', 'test/fixtures/input-indent.html', '-o', folder]); - t.is((await read('test/expected/output-config-pkg.html')), (await read(`${folder}/input.html`))); - t.is((await read('test/expected/output-indent.html')), (await read(`${folder}/input-indent.html`))); -}); + t.plan(2) + const folder = await tempfile() + await execa(cli, ['test/fixtures/input.html', 'test/fixtures/input-indent.html', '-o', folder]) + t.is((await read('test/expected/output-config-pkg.html')), (await read(`${folder}/input.html`))) + t.is((await read('test/expected/output-indent.html')), (await read(`${folder}/input-indent.html`))) +}) // test('Transform html witch options replace', async t => { // t.plan(2); @@ -72,8 +72,8 @@ test('Transform html from two file', async t => { // }); test('Transform html witch config in file and stdin options use', async t => { - t.plan(2); - const filename = tempfile('.html'); + t.plan(2) + const filename = tempfile('.html') await execa(cli, [ 'test/fixtures/input-bem.html', '-o', @@ -86,14 +86,14 @@ test('Transform html witch config in file and stdin options use', async t => { '--posthtml-bem.modPrefix', '_', '--posthtml-bem.modDlmtr' - ]); - t.true(await pathExists(filename)); - t.is((await read('test/expected/output-bem.html')), (await read(filename))); -}); + ]) + t.true(await pathExists(filename)) + t.is((await read('test/expected/output-bem.html')), (await read(filename))) +}) test('Transform html witch stdin options use', async t => { - t.plan(2); - const filename = tempfile('.html'); + t.plan(2) + const filename = tempfile('.html') await execa(cli, [ 'test/fixtures/input-custom-elements.html', '-o', @@ -102,14 +102,14 @@ test('Transform html witch stdin options use', async t => { 'posthtml-custom-elements', '--posthtml-custom-elements.defaultTag', 'span' - ]); - t.true(await pathExists(filename)); - t.is((await read('test/expected/output-custom-elements.html')), (await read(filename))); -}); + ]) + t.true(await pathExists(filename)) + t.is((await read('test/expected/output-custom-elements.html')), (await read(filename))) +}) test('Transform html witch stdin options use two key', async t => { - t.plan(2); - const filename = tempfile('.html'); + t.plan(2) + const filename = tempfile('.html') await execa(cli, [ 'test/fixtures/input-bem.html', '-o', @@ -124,14 +124,14 @@ test('Transform html witch stdin options use two key', async t => { 'posthtml-custom-elements', '--posthtml-custom-elements.defaultTag', 'span' - ]); - t.true(await pathExists(filename)); - t.is((await read('test/expected/output-bem.html')), (await read(filename))); -}); + ]) + t.true(await pathExists(filename)) + t.is((await read('test/expected/output-bem.html')), (await read(filename))) +}) test('Transform html stdin options use witch modules', async t => { - t.plan(2); - const filename = tempfile('.html'); + t.plan(2) + const filename = tempfile('.html') await execa(cli, [ 'test/fixtures/input-modules.html', '-o', @@ -140,64 +140,64 @@ test('Transform html stdin options use witch modules', async t => { 'posthtml-css-modules', '--posthtml-css-modules', 'test/fixtures/css-modules.json' - ]); - t.true(await pathExists(filename)); - t.is((await read('test/expected/output-modules.html')), (await read(filename))); -}); + ]) + t.true(await pathExists(filename)) + t.is((await read('test/expected/output-modules.html')), (await read(filename))) +}) test('Transform html stdin options only config one-io', async t => { - t.plan(2); + t.plan(2) await execa(cli, [ '-c', 'test/fixtures/by-config/one-io/.config' - ]); - t.true(await pathExists('test/expected/by-config/one-io/output.html')); + ]) + t.true(await pathExists('test/expected/by-config/one-io/output.html')) t.is( (await read('test/expected/by-config/one-io/output.html')), (await read('test/fixtures/by-config/one-io/input.html')) - ); -}); + ) +}) test('Transform html stdin options only config two-io to dir', async t => { - t.plan(4); + t.plan(4) await execa(cli, [ '-c', 'test/fixtures/by-config/two-io/.config' - ]); - t.true(await pathExists('test/expected/by-config/two-io/input-1.html')); - t.true(await pathExists('test/expected/by-config/two-io/input-2.html')); + ]) + t.true(await pathExists('test/expected/by-config/two-io/input-1.html')) + t.true(await pathExists('test/expected/by-config/two-io/input-2.html')) t.is( (await read('test/expected/by-config/two-io/input-1.html')), (await read('test/fixtures/by-config/two-io/input-1.html')) - ); + ) t.is( (await read('test/expected/by-config/two-io/input-2.html')), (await read('test/fixtures/by-config/two-io/input-2.html')) - ); -}); + ) +}) test('Transform html stdin options only config one-io-by-pattern', async t => { - t.plan(2); + t.plan(2) await execa(cli, [ '-c', 'test/fixtures/by-config/one-io-by-pattern/.config' - ]); - t.true(await pathExists('test/expected/by-config/one-io-by-pattern/input-1.html')); + ]) + t.true(await pathExists('test/expected/by-config/one-io-by-pattern/input-1.html')) t.is( (await read('test/expected/by-config/one-io-by-pattern/input-1.html')), (await read('test/fixtures/by-config/one-io-by-pattern/input-1.html')) - ); -}); + ) +}) test('Transform html stdin options only config one-io anf plugins array', async t => { - t.plan(2); + t.plan(2) await execa(cli, [ '-c', 'test/fixtures/by-config/one-io-and-plugins-array/posthtml.config.js' - ]); - t.true(await pathExists('test/expected/by-config/one-io-and-plugins-array/output.html')); + ]) + t.true(await pathExists('test/expected/by-config/one-io-and-plugins-array/output.html')) t.is( (await read('test/expected/by-config/one-io-and-plugins-array/output.html')), (await read('test/fixtures/by-config/one-io-and-plugins-array/input.html')) - ); -}); + ) +}) diff --git a/test/test-out-resolve.js b/test/test-out-resolve.js index 49da27e..d872f08 100644 --- a/test/test-out-resolve.js +++ b/test/test-out-resolve.js @@ -1,41 +1,40 @@ -import path from 'path'; -import test from 'ava'; -import isPromise from 'is-promise'; -import outResolve from '../lib/out-resolve'; +import path from 'path' +import test from 'ava' +import isPromise from 'is-promise' +import outResolve from '../lib/out-resolve' test('should return function', t => { - t.true(typeof outResolve === 'function'); -}); + t.true(typeof outResolve === 'function') +}) test('should return Promise', t => { - t.true(isPromise(outResolve())); -}); + t.true(isPromise(outResolve())) +}) test('only input should return file.ext', async t => { - t.is(await outResolve('file.ext'), 'file.ext'); -}); + t.is(await outResolve('file.ext'), 'file.ext') +}) test('only input should return tmp/file.ext', async t => { - t.is(await outResolve('tmp/file.ext'), 'tmp/file.ext'); -}); + t.is(await outResolve('tmp/file.ext'), 'tmp/file.ext') +}) test('input file and output file should return output.ext', async t => { - t.is(await outResolve('file.ext', 'output.ext'), 'output.ext'); -}); + t.is(await outResolve('file.ext', 'output.ext'), 'output.ext') +}) test('input file and output folder should return tmp/file.ext', async t => { - t.is(await outResolve('file.ext', 'tmp'), path.normalize('tmp/file.ext')); -}); + t.is(await outResolve('file.ext', 'tmp'), path.normalize('tmp/file.ext')) +}) test('input files and output folder should return tmp/test/*.ext', async t => { - t.is(await outResolve(path.normalize('tmp/test/*.ext'), undefined), path.normalize('tmp/test/*.ext')); -}); + t.is(await outResolve(path.normalize('tmp/test/*.ext'), undefined), path.normalize('tmp/test/*.ext')) +}) test('input files and output file should return output.ext', async t => { - t.is(await outResolve('test/*', 'output.ext'), 'output.ext'); -}); + t.is(await outResolve('test/*', 'output.ext'), 'output.ext') +}) test('input files and output file should return tmp/output.ext', async t => { - t.is(await outResolve('test/*', 'tmp/output.ext'), 'tmp/output.ext'); -}); - + t.is(await outResolve('test/*', 'tmp/output.ext'), 'tmp/output.ext') +})