From 6466af7eaacf5020e2034638546803de3a0905d6 Mon Sep 17 00:00:00 2001 From: ybiquitous Date: Tue, 10 Oct 2017 21:37:13 +0900 Subject: [PATCH] feat(init): add more details to `init` command help --- README.md | 2 +- src/cli.js | 6 +--- src/init.js | 90 ++++++++++++++++++++++++++++------------------- test/help.test.js | 8 +++-- 4 files changed, 62 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 9bbb46e2..47fe59a1 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ ybiq --help ### `init` -Setup Node.js project. +Setup npm project. ```sh ybiq init diff --git a/src/cli.js b/src/cli.js index f59eea93..2ae7fb7e 100644 --- a/src/cli.js +++ b/src/cli.js @@ -5,11 +5,7 @@ module.exports = function cli() { // eslint-disable-next-line no-unused-expressions yargs .usage('ybiq ') - .command({ - command: 'init', - desc: 'Setup npm project', - handler: () => init(), - }) + .command('init', init.desc, {}, init) .demandCommand(1) .argv } diff --git a/src/init.js b/src/init.js index 6742dc6e..027b64d5 100644 --- a/src/init.js +++ b/src/init.js @@ -4,54 +4,72 @@ const originalPackage = require('../package.json') const targetProps = ['scripts', 'lint-staged'] -async function updatePackageFile(baseDir) { - const packageFile = path.join(baseDir, 'package.json') - const packageInfo = JSON.parse(await fs.readFile(packageFile, 'utf8')) +function stdout(str) { + process.stdout.write(`${str}\n`) +} - targetProps.forEach((prop) => { - packageInfo[prop] = { ...originalPackage[prop], ...packageInfo[prop] } - }) - packageInfo.scripts['test:watch'] = `${packageInfo.scripts.test} --watch` - packageInfo.scripts['test:coverage'] = 'echo "unsupported." && exit 1' +class Init { + constructor(baseDir) { + this.baseDir = baseDir + } - await fs.writeFile(packageFile, `${JSON.stringify(packageInfo, null, 2)}\n`) + filePath(fileName) { + return path.join(this.baseDir, fileName) + } - process.stdout.write(`${packageFile} was updated.\n`) -} + async writeFile(fileName, fileContent) { + const file = this.filePath(fileName) + await fs.writeFile(file, `${fileContent}\n`) + stdout(`${file} was updated.`) + } -async function copyEditorConfig(baseDir) { - const source = path.join(__dirname, '..', '.editorconfig') - const target = path.join(baseDir, '.editorconfig') - await fs.copy(source, target) + async readFile(fileName) { + return fs.readFile(path.join(this.baseDir, fileName), 'utf8') + } - process.stdout.write(`${target} was updated.\n`) -} + async updatePackageFile() { + const packageInfo = JSON.parse(await this.readFile('package.json')) -async function writeConfigFile(baseDir, fileName, fileContent) { - const target = path.join(baseDir, fileName) - await fs.writeFile(target, fileContent) - process.stdout.write(`${target} was wrote.\n`) -} + targetProps.forEach((prop) => { + packageInfo[prop] = { ...originalPackage[prop], ...packageInfo[prop] } + }) + packageInfo.scripts['test:watch'] = `${packageInfo.scripts.test} --watch` + packageInfo.scripts['test:coverage'] = 'echo "unsupported." && exit 1' + + await this.writeFile('package.json', JSON.stringify(packageInfo, null, 2)) + } -async function writeESLintConfig(baseDir) { - writeConfigFile(baseDir, '.eslintrc.js', `module.exports = { + async copyEditorConfig() { + const source = path.join(__dirname, '..', '.editorconfig') + const target = this.filePath('.editorconfig') + await fs.copy(source, target) + stdout(`${target} was updated.`) + } + + async writeESLintConfig() { + await this.writeFile('.eslintrc.js', `module.exports = { root: true, extends: ['ybiquitous'], -} -`) -} +}`) + } -async function writeCommitlintConfig(baseDir) { - writeConfigFile(baseDir, 'commitlint.config.js', `module.exports = { + async writeCommitlintConfig() { + await this.writeFile('commitlint.config.js', `module.exports = { extends: ['@commitlint/config-angular'], -} -`) +}`) + } } module.exports = async function init() { - const baseDir = process.cwd() - await updatePackageFile(baseDir) - await copyEditorConfig(baseDir) - await writeESLintConfig(baseDir) - await writeCommitlintConfig(baseDir) + const cmd = new Init(process.cwd()) + await cmd.updatePackageFile() + await cmd.copyEditorConfig() + await cmd.writeESLintConfig() + await cmd.writeCommitlintConfig() } + +module.exports.desc = `Setup npm project: +- Update 'package.json' +- Create '.editorconfig' +- Create '.eslintrc.js' +- Create 'commitlint.config.js'` diff --git a/test/help.test.js b/test/help.test.js index 9f3d65d5..b7360032 100644 --- a/test/help.test.js +++ b/test/help.test.js @@ -6,7 +6,11 @@ const HELP = ` ybiq Commands: - init Setup npm project + init Setup npm project: + - Update 'package.json' + - Create '.editorconfig' + - Create '.eslintrc.js' + - Create 'commitlint.config.js' Options: --help Show help [boolean] @@ -25,7 +29,7 @@ suite('help', () => { test('with `--help` option', async () => { const { stdout, stderr } = await exec('--help') - assert(stdout.includes(HELP)) + assert(stdout.includes(HELP), stdout) assert(stderr === '') }) })