diff --git a/package.json b/package.json index 8a845dc4..ef355f21 100644 --- a/package.json +++ b/package.json @@ -19,8 +19,8 @@ "bin", "lib", "src", - ".editorconfig", - ".eslintrc.js" + "templates", + ".editorconfig" ], "engines": { "node": ">=6" diff --git a/src/init.js b/src/init.js index a137556f..c04829ab 100644 --- a/src/init.js +++ b/src/init.js @@ -7,6 +7,13 @@ const stdout = str => process.stdout.write(`${str}\n`) const packagePath = (...pathElements) => path.join(...[__dirname, '..', ...pathElements]) +const copyFile = async (src, dest) => { + await fs.copy(src, dest) + stdout(`${dest} was updated.`) +} + +const template = name => path.join(__dirname, '..', 'templates', name) + class Init { constructor(baseDir) { this.baseDir = baseDir @@ -59,37 +66,26 @@ class Init { await this.writeFile('package.json', JSON.stringify(packageInfo, null, 2)) } - async copyEditorConfig() { - const source = packagePath('.editorconfig') - const target = this.currentPath('.editorconfig') - await fs.copy(source, target) - stdout(`${target} was updated.`) + async writeEditorConfig() { + const name = '.editorconfig' + await copyFile(packagePath(name), this.currentPath(name)) } async writeESLintConfig() { - await this.writeFile( - '.eslintrc.js', - `module.exports = { - root: true, - extends: ['ybiquitous'], -}` - ) + const name = '.eslintrc.js' + await copyFile(template(name), this.currentPath(name)) } async writeCommitlintConfig() { - await this.writeFile( - '.commitlintrc.js', - `module.exports = { - extends: ['@commitlint/config-conventional'], -}` - ) + const name = '.commitlintrc.js' + await copyFile(template(name), this.currentPath(name)) } } module.exports = async function init() { const cmd = new Init(process.cwd()) await cmd.updatePackageFile() - await cmd.copyEditorConfig() + await cmd.writeEditorConfig() await cmd.writeESLintConfig() await cmd.writeCommitlintConfig() } diff --git a/templates/.commitlintrc.js b/templates/.commitlintrc.js new file mode 100644 index 00000000..98ee7dfc --- /dev/null +++ b/templates/.commitlintrc.js @@ -0,0 +1,3 @@ +module.exports = { + extends: ['@commitlint/config-conventional'], +} diff --git a/templates/.eslintrc.js b/templates/.eslintrc.js new file mode 100644 index 00000000..c1cec76c --- /dev/null +++ b/templates/.eslintrc.js @@ -0,0 +1,4 @@ +module.exports = { + root: true, + extends: ['ybiquitous'], +} diff --git a/test/init.test.js b/test/init.test.js index a2fd9594..24c1f001 100644 --- a/test/init.test.js +++ b/test/init.test.js @@ -52,9 +52,11 @@ suite('init', () => { assert(actual === expected) }) - test('copy ".editorconfig"', async () => { + test('write ".editorconfig"', async () => { await fixture('package-normal.json') - await exec('init') + const { stdout, stderr } = await exec('init') + assert(stdout.includes('package.json was updated.')) + assert(stderr === '') const original = await readFile(path.join(originalDir, '.editorconfig')) const copy = await readFile(path.join(workDir, '.editorconfig')) @@ -63,7 +65,9 @@ suite('init', () => { test('write ".eslintrc.js"', async () => { await fixture('package-normal.json') - await exec('init') + const { stdout, stderr } = await exec('init') + assert(stdout.includes('.eslintrc.js was updated.')) + assert(stderr === '') const actual = await readFile(path.join(workDir, '.eslintrc.js')) const expected = await readFile(fixturePath('.eslintrc_expected.js')) @@ -72,7 +76,9 @@ suite('init', () => { test('write ".commitlintrc.js"', async () => { await fixture('package-normal.json') - await exec('init') + const { stdout, stderr } = await exec('init') + assert(stdout.includes('.commitlintrc.js was updated.')) + assert(stderr === '') const actual = await readFile(path.join(workDir, '.commitlintrc.js')) const expected = await readFile(fixturePath('.commitlintrc_expected.js'))