From a23608963f403bd64c89e60de57be05afafa66d3 Mon Sep 17 00:00:00 2001 From: Masafumi Koba Date: Mon, 18 Jun 2018 11:59:54 +0900 Subject: [PATCH] fix(eslint): update settings and fix source code --- .commitlintrc.js | 36 +++++----- .eslintrc.js | 9 +-- bin/cli.js | 4 +- index.js | 4 +- lib/cli.js | 14 ++-- lib/init.js | 89 +++++++++++------------ package.json | 5 +- test/help.test.js | 46 ++++++------ test/helpers/exec.js | 30 ++++---- test/init.test.js | 168 +++++++++++++++++++++---------------------- test/main.test.js | 20 +++--- test/version.test.js | 24 +++---- 12 files changed, 227 insertions(+), 222 deletions(-) diff --git a/.commitlintrc.js b/.commitlintrc.js index 1c0368dd..2215ce01 100644 --- a/.commitlintrc.js +++ b/.commitlintrc.js @@ -1,26 +1,26 @@ module.exports = { - extends: ['@commitlint/config-conventional'], + extends: ["@commitlint/config-conventional"], rules: { - 'scope-enum': [ + "scope-enum": [ 2, - 'always', + "always", [ - '', - 'deps', - 'package', - 'readme', - 'eslint', - 'editorconfig', - 'commitlint', - 'coverage', - 'release', - 'init', - 'api', - 'markdownlint', - 'travis', - 'prettier', + "", + "deps", + "package", + "readme", + "eslint", + "editorconfig", + "commitlint", + "coverage", + "release", + "init", + "api", + "markdownlint", + "travis", + "prettier", ], ], }, -} +}; diff --git a/.eslintrc.js b/.eslintrc.js index f9390cc0..1a31b60e 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,14 +1,15 @@ module.exports = { root: true, - extends: ['ybiquitous'], + extends: ["ybiquitous/node"], overrides: [ { - files: ['**/test/**/*.js'], + files: ["**/test/**/*.js"], rules: { - 'no-shadow': ['error', { allow: ['t'] }], + "no-shadow": ["error", { allow: ["t"] }], + "import/no-internal-modules": "off", }, }, ], -} +}; diff --git a/bin/cli.js b/bin/cli.js index 98be9452..2209fd4a 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -1,4 +1,4 @@ #!/usr/bin/env node -const cli = require(`../lib/cli`) +const cli = require(`../lib/cli`); -cli() +cli(); diff --git a/index.js b/index.js index dd347c18..ad8bfbd9 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,3 @@ -const init = require(`./lib/init`) +const init = require(`./lib/init`); -module.exports = { init } +module.exports = { init }; diff --git a/lib/cli.js b/lib/cli.js index 2d26686e..bab98ea5 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -1,13 +1,13 @@ -const yargs = require('yargs') -const init = require('./init') +const yargs = require("yargs"); +const init = require("./init"); module.exports = function cli() { // eslint-disable-next-line no-unused-expressions yargs - .usage('$0 ') - .command('init', init.desc, {}, init) + .usage("$0 ") + .command("init", init.desc, {}, init) .demandCommand(1) .strict() - .alias('help', 'h') - .alias('version', 'v').argv -} + .alias("help", "h") + .alias("version", "v").argv; +}; diff --git a/lib/init.js b/lib/init.js index be8aafea..4d632152 100644 --- a/lib/init.js +++ b/lib/init.js @@ -1,95 +1,96 @@ -const path = require('path') -const { EOL } = require('os') -const fs = require('fs-extra') -const originalPackage = require('../package.json') +const path = require("path"); +const { EOL } = require("os"); +const fs = require("fs-extra"); +const originalPackage = require("../package.json"); const packagePath = (...pathElements) => - path.join(...[__dirname, '..', ...pathElements]) + path.join(...[__dirname, "..", ...pathElements]); -const template = name => path.join(__dirname, '..', 'templates', name) +const template = name => path.join(__dirname, "..", "templates", name); class Init { constructor(baseDir, logger) { - this.baseDir = baseDir - this.logger = logger + this.baseDir = baseDir; + this.logger = logger; } async copyFile(src, dest) { - await fs.copy(src, dest) - this.logger(`${dest} was updated.`) + await fs.copy(src, dest); + this.logger(`${dest} was updated.`); } currentPath(...pathElements) { - return path.join(...[this.baseDir, ...pathElements]) + return path.join(...[this.baseDir, ...pathElements]); } async writeFile(fileName, fileContent) { - const file = this.currentPath(fileName) - await fs.writeFile(file, `${fileContent}\n`) - this.logger(`${file} was updated.`) + const file = this.currentPath(fileName); + await fs.writeFile(file, `${fileContent}\n`); + this.logger(`${file} was updated.`); } - async readFile(fileName) { - return fs.readFile(this.currentPath(fileName), 'utf8') + readFile(fileName) { + return fs.readFile(this.currentPath(fileName), "utf8"); } + // eslint-disable-next-line max-statements async updatePackageFile() { - const packageInfo = JSON.parse(await this.readFile('package.json')) + const packageInfo = JSON.parse(await this.readFile("package.json")); // update 'scripts' - if (!('scripts' in packageInfo)) { - packageInfo.scripts = {} + if (!("scripts" in packageInfo)) { + packageInfo.scripts = {}; } - const { scripts } = packageInfo - if (!('test' in scripts)) { - scripts.test = 'test' + const { scripts } = packageInfo; + if (!("test" in scripts)) { + scripts.test = "test"; } - scripts['test:watch'] = `${scripts.test} --watch` - scripts['test:coverage'] = 'echo "unsupported." && exit 1' + scripts["test:watch"] = `${scripts.test} --watch`; + scripts["test:coverage"] = 'echo "unsupported." && exit 1'; Object.keys(originalPackage.scripts) - .filter(key => !(key === 'test' || key.startsWith('test:'))) + .filter(key => !(key === "test" || key.startsWith("test:"))) .forEach(key => { - scripts[key] = originalPackage.scripts[key] - }) + scripts[key] = originalPackage.scripts[key]; + }); // update other keys - const keys = ['lint-staged'] + const keys = ["lint-staged"]; keys.forEach(key => { if (!(key in packageInfo)) { - packageInfo[key] = {} + packageInfo[key] = {}; } - Object.assign(packageInfo[key], originalPackage[key]) - }) + Object.assign(packageInfo[key], originalPackage[key]); + }); - await this.writeFile('package.json', JSON.stringify(packageInfo, null, 2)) + await this.writeFile("package.json", JSON.stringify(packageInfo, null, 2)); } async writeTemplateFile(name) { - await this.copyFile(template(name), this.currentPath(name)) + await this.copyFile(template(name), this.currentPath(name)); } async writePackageFile(name) { - await this.copyFile(packagePath(name), this.currentPath(name)) + await this.copyFile(packagePath(name), this.currentPath(name)); } } -const defaultLogger = msg => process.stdout.write(`${msg}${EOL}`) +const defaultLogger = msg => process.stdout.write(`${msg}${EOL}`); module.exports = async function init({ cwd = process.cwd(), logger = defaultLogger, } = {}) { - const cmd = new Init(cwd, logger) - await cmd.updatePackageFile() - await cmd.writePackageFile('.editorconfig') - await cmd.writePackageFile('.prettierignore') - await cmd.writePackageFile('.markdownlint.json') - await cmd.writeTemplateFile('.eslintrc.js') - await cmd.writeTemplateFile('.commitlintrc.js') -} + const cmd = new Init(cwd, logger); + await cmd.updatePackageFile(); + await cmd.writePackageFile(".editorconfig"); + await cmd.writePackageFile(".prettierignore"); + await cmd.writePackageFile(".markdownlint.json"); + await cmd.writeTemplateFile(".eslintrc.js"); + await cmd.writeTemplateFile(".commitlintrc.js"); +}; module.exports.desc = `Setup npm project: - Update 'package.json' - Create '.commitlintrc.js' - Create '.editorconfig' -- Create '.eslintrc.js'` +- Create '.eslintrc.js'`; diff --git a/package.json b/package.json index 25809e4a..0964cc7f 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ ".markdownlint.json" ], "engines": { - "node": ">=8" + "node": ">=8.3.0" }, "dependencies": { "@commitlint/cli": "^7.0.0", @@ -76,6 +76,9 @@ "CHANGELOG.md" ] }, + "prettier": { + "trailingComma": "es5" + }, "browserslist": [ "> 1%", "not ie 11", diff --git a/test/help.test.js b/test/help.test.js index 92c24a32..777a0305 100644 --- a/test/help.test.js +++ b/test/help.test.js @@ -1,5 +1,5 @@ -const test = require('tape') -const exec = require('./helpers/exec') +const test = require("tape"); +const exec = require("./helpers/exec"); const HELP = ` cli.js @@ -14,26 +14,26 @@ Commands: Options: --help, -h Show help [boolean] --version, -v Show version number [boolean] -`.trim() +`.trim(); -test('help', t => { - ;[[], ['unknown'], ['unknown', 'xyz']].forEach(args => { - t.test(`with arguments [${args.join(', ')}]`, async t => { - const error = await exec(...args).catch(err => err) - const { code, stdout, stderr } = error - t.ok(error instanceof Error) - t.is(code, 1) - t.is(stdout, '') - t.ok(stderr.includes(HELP)) - t.end() - }) - }) - ;['--help', '-h'].forEach(option => { +test("help", t => { + [[], ["unknown"], ["unknown", "xyz"]].forEach(args => { + t.test(`with arguments [${args.join(", ")}]`, async t => { + const error = await exec(...args).catch(err => err); + const { code, stdout, stderr } = error; + t.ok(error instanceof Error); + t.is(code, 1); + t.is(stdout, ""); + t.ok(stderr.includes(HELP)); + t.end(); + }); + }); + ["--help", "-h"].forEach(option => { t.test(`with "${option}" option`, async t => { - const { stdout, stderr } = await exec(option) - t.ok(stdout.includes(HELP)) - t.is(stderr, '') - t.end() - }) - }) -}) + const { stdout, stderr } = await exec(option); + t.ok(stdout.includes(HELP)); + t.is(stderr, ""); + t.end(); + }); + }); +}); diff --git a/test/helpers/exec.js b/test/helpers/exec.js index 149af740..bd816011 100644 --- a/test/helpers/exec.js +++ b/test/helpers/exec.js @@ -1,27 +1,27 @@ -const path = require('path') -const cp = require('child_process') +const path = require("path"); +const cp = require("child_process"); -const tested = path.join(process.cwd(), 'bin', 'cli.js') +const tested = path.join(process.cwd(), "bin", "cli.js"); module.exports = function exec(...args) { const options = { - env: { ...process.env, LANG: 'C' }, - } - const lastArg = args[args.length - 1] - if (lastArg && typeof lastArg === 'object') { - options.cwd = lastArg.cwd + env: { ...process.env, LANG: "C" }, + }; + const lastArg = args[args.length - 1]; + if (lastArg && typeof lastArg === "object") { + options.cwd = lastArg.cwd; } return new Promise((resolve, reject) => { cp.execFile(tested, args, options, (error, stdout, stderr) => { if (error) { /* eslint-disable no-param-reassign */ - error.stdout = stdout - error.stderr = stderr + error.stdout = stdout; + error.stderr = stderr; /* eslint-enable no-param-reassign */ - reject(error) + reject(error); } else { - resolve({ stdout, stderr }) + resolve({ stdout, stderr }); } - }) - }) -} + }); + }); +}; diff --git a/test/init.test.js b/test/init.test.js index 5d556423..55018589 100644 --- a/test/init.test.js +++ b/test/init.test.js @@ -1,29 +1,29 @@ -const path = require('path') -const os = require('os') -const fs = require('fs-extra') -const test = require('tape') -const exec = require('./helpers/exec') -const pkg = require('../package.json') -const init = require('../lib/init') +const path = require("path"); +const os = require("os"); +const fs = require("fs-extra"); +const test = require("tape"); +const pkg = require("../package.json"); +const init = require("../lib/init"); +const exec = require("./helpers/exec"); -const readFile = file => fs.readFile(file, 'utf8') +const readFile = file => fs.readFile(file, "utf8"); const sandbox = async (fn, t) => { - const workDir = path.join(os.tmpdir(), `${pkg.name}${Date.now()}`) - await fs.mkdirs(workDir) + const workDir = path.join(os.tmpdir(), `${pkg.name}${Date.now()}`); + await fs.mkdirs(workDir); - const logMsgs = [] - const logger = msg => logMsgs.push(msg) + const logMsgs = []; + const logger = msg => logMsgs.push(msg); try { - const cwd = process.cwd() - const fixturePath = name => path.join(cwd, 'test', 'fixtures', name) + const cwd = process.cwd(); + const fixturePath = name => path.join(cwd, "test", "fixtures", name); const fixture = async name => { - const src = fixturePath(name) - const dest = path.join(workDir, 'package.json') - await fs.copy(src, dest) - return dest - } + const src = fixturePath(name); + const dest = path.join(workDir, "package.json"); + await fs.copy(src, dest); + return dest; + }; return await fn(t, { fixturePath, @@ -31,85 +31,85 @@ const sandbox = async (fn, t) => { readFixture: name => readFile(fixturePath(name)), readOrigFile: name => readFile(path.join(cwd, name)), readWorkFile: name => readFile(path.join(workDir, name)), - logMessage: () => logMsgs.join(''), + logMessage: () => logMsgs.join(""), initArgs: { cwd: workDir, logger }, - }) + }); } finally { - await fs.remove(workDir) + await fs.remove(workDir); } -} +}; -test('init', t => { +test("init", t => { const testInSandbox = (name, fn) => { - t.test(name, t => sandbox(fn, t)) - } + t.test(name, t => sandbox(fn, t)); + }; testInSandbox('update "package.json"', async (t, ctx) => { - const src = await ctx.fixture('package-normal.json') - await init(ctx.initArgs) - const actual = await readFile(src) - const expected = await ctx.readFixture('package-normal_expected.json') - t.is(actual, expected) - t.end() - }) + const src = await ctx.fixture("package-normal.json"); + await init(ctx.initArgs); + const actual = await readFile(src); + const expected = await ctx.readFixture("package-normal_expected.json"); + t.is(actual, expected); + t.end(); + }); testInSandbox('update "package.json" without fields', async (t, ctx) => { - const src = await ctx.fixture('package-empty.json') - await init(ctx.initArgs) - const actual = await readFile(src) - const expected = await ctx.readFixture('package-empty_expected.json') - t.is(actual, expected) - t.end() - }) - ;['.editorconfig', '.prettierignore', '.markdownlint.json'].forEach(file => { + const src = await ctx.fixture("package-empty.json"); + await init(ctx.initArgs); + const actual = await readFile(src); + const expected = await ctx.readFixture("package-empty_expected.json"); + t.is(actual, expected); + t.end(); + }); + [".editorconfig", ".prettierignore", ".markdownlint.json"].forEach(file => { testInSandbox(`write "${file}"`, async (t, ctx) => { - await ctx.fixture('package-normal.json') - await init(ctx.initArgs) - t.ok(ctx.logMessage().includes('package.json was updated.')) + await ctx.fixture("package-normal.json"); + await init(ctx.initArgs); + t.ok(ctx.logMessage().includes("package.json was updated.")); - const original = await ctx.readOrigFile(file) - const copy = await ctx.readWorkFile(file) - t.is(original, copy) + const original = await ctx.readOrigFile(file); + const copy = await ctx.readWorkFile(file); + t.is(original, copy); - t.ok(pkg.files.includes(file)) - t.end() - }) - }) + t.ok(pkg.files.includes(file)); + t.end(); + }); + }); testInSandbox('write ".eslintrc.js"', async (t, ctx) => { - await ctx.fixture('package-normal.json') - await init(ctx.initArgs) - t.ok(ctx.logMessage().includes('.eslintrc.js was updated.')) + await ctx.fixture("package-normal.json"); + await init(ctx.initArgs); + t.ok(ctx.logMessage().includes(".eslintrc.js was updated.")); - const actual = await ctx.readWorkFile('.eslintrc.js') - const expected = await ctx.readFixture('.eslintrc_expected.js') - t.is(actual, expected) - t.end() - }) + const actual = await ctx.readWorkFile(".eslintrc.js"); + const expected = await ctx.readFixture(".eslintrc_expected.js"); + t.is(actual, expected); + t.end(); + }); testInSandbox('write ".commitlintrc.js"', async (t, ctx) => { - await ctx.fixture('package-normal.json') - await init(ctx.initArgs) - t.ok(ctx.logMessage().includes('.commitlintrc.js was updated.')) - - const actual = await ctx.readWorkFile('.commitlintrc.js') - const expected = await ctx.readFixture('.commitlintrc_expected.js') - t.is(actual, expected) - t.end() - }) - - testInSandbox('throw error if no package.json', async (t, ctx) => { - const error = await init(ctx.initArgs).catch(err => err) - t.ok(error instanceof Error) - t.is(error.code, 'ENOENT') - t.end() - }) - - testInSandbox('End-to-End via CLI', async (t, ctx) => { - await ctx.fixture('package-normal.json') - const { stdout, stderr } = await exec('init', { cwd: ctx.initArgs.cwd }) - t.ok(stdout.includes('package.json was updated.')) - t.is(stderr, '') - t.end() - }) -}) + await ctx.fixture("package-normal.json"); + await init(ctx.initArgs); + t.ok(ctx.logMessage().includes(".commitlintrc.js was updated.")); + + const actual = await ctx.readWorkFile(".commitlintrc.js"); + const expected = await ctx.readFixture(".commitlintrc_expected.js"); + t.is(actual, expected); + t.end(); + }); + + testInSandbox("throw error if no package.json", async (t, ctx) => { + const error = await init(ctx.initArgs).catch(err => err); + t.ok(error instanceof Error); + t.is(error.code, "ENOENT"); + t.end(); + }); + + testInSandbox("End-to-End via CLI", async (t, ctx) => { + await ctx.fixture("package-normal.json"); + const { stdout, stderr } = await exec("init", { cwd: ctx.initArgs.cwd }); + t.ok(stdout.includes("package.json was updated.")); + t.is(stderr, ""); + t.end(); + }); +}); diff --git a/test/main.test.js b/test/main.test.js index 7853822f..17041f30 100644 --- a/test/main.test.js +++ b/test/main.test.js @@ -1,11 +1,11 @@ -const test = require('tape') -const main = require('..') +const test = require("tape"); +const main = require(".."); -test('main', t => { - t.test('init', async t => { - const { init } = main - t.is(typeof init, 'function') - t.is(init.name, 'init') - t.end() - }) -}) +test("main", t => { + t.test("init", t => { + const { init } = main; + t.is(typeof init, "function"); + t.is(init.name, "init"); + t.end(); + }); +}); diff --git a/test/version.test.js b/test/version.test.js index 89a18490..4bba1014 100644 --- a/test/version.test.js +++ b/test/version.test.js @@ -1,14 +1,14 @@ -const test = require('tape') -const exec = require('./helpers/exec') -const pkg = require('../package.json') +const test = require("tape"); +const pkg = require("../package.json"); +const exec = require("./helpers/exec"); -test('version', t => { - ;['--version', '-v'].forEach(option => { +test("version", t => { + ["--version", "-v"].forEach(option => { t.test(`with "${option}" option`, async t => { - const { stdout, stderr } = await exec(option) - t.is(stdout.trim(), pkg.version) - t.is(stderr, '') - t.end() - }) - }) -}) + const { stdout, stderr } = await exec(option); + t.is(stdout.trim(), pkg.version); + t.is(stderr, ""); + t.end(); + }); + }); +});