diff --git a/package-lock.json b/package-lock.json index 1883b92d..7162df66 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2002,7 +2002,6 @@ "version": "4.0.1", "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-4.0.1.tgz", "integrity": "sha1-f8DGyJV/mD9X8waiTlud3Y0N2IA=", - "dev": true, "requires": { "graceful-fs": "4.1.11", "jsonfile": "3.0.1", @@ -2492,7 +2491,6 @@ "version": "3.0.1", "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-3.0.1.tgz", "integrity": "sha1-pezG9l9T9mLEQVx2daAzHQmS7GY=", - "dev": true, "requires": { "graceful-fs": "4.1.11" } @@ -4490,8 +4488,7 @@ "universalify": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.1.tgz", - "integrity": "sha1-+nG63UQ3r0wUiEHjs7Fl+enlkLc=", - "dev": true + "integrity": "sha1-+nG63UQ3r0wUiEHjs7Fl+enlkLc=" }, "util-deprecate": { "version": "1.0.2", diff --git a/package.json b/package.json index 80e1ab57..dedac1f5 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "@commitlint/config-angular": "^3.1.1", "eslint": "^4.5.0", "eslint-config-ybiquitous": "^2.0.2", + "fs-extra": "^4.0.1", "husky": "^0.14.3", "lint-staged": "^4.0.4", "markdownlint-cli": "^0.3.1", @@ -34,7 +35,6 @@ "babel-polyfill": "^6.26.0", "babel-preset-env": "^1.6.0", "babel-register": "^6.26.0", - "fs-extra": "^4.0.1", "mocha": "^3.5.0" }, "scripts": { diff --git a/src/init.js b/src/init.js index 3f5dea20..1ebeb299 100644 --- a/src/init.js +++ b/src/init.js @@ -1,22 +1,33 @@ const path = require('path') -const fs = require('fs') -const { promisify } = require('util') +const fs = require('fs-extra') const originalPackage = require('../package.json') -const readFile = promisify(fs.readFile) -const writeFile = promisify(fs.writeFile) const targetProps = ['scripts', 'lint-staged'] -module.exports = async function init(basedir = process.cwd()) { - const packageFile = path.join(basedir, 'package.json') - const packageInfo = JSON.parse(await readFile(packageFile, 'utf8')) +async function updatePackageFile(baseDir) { + const packageFile = path.join(baseDir, 'package.json') + const packageInfo = JSON.parse(await fs.readFile(packageFile, 'utf8')) targetProps.forEach((prop) => { packageInfo[prop] = { ...originalPackage[prop], ...packageInfo[prop] } }) packageInfo.scripts['test:watch'] = `${packageInfo.scripts.test} --watch` - await writeFile(packageFile, `${JSON.stringify(packageInfo, null, 2)}\n`) + await fs.writeFile(packageFile, `${JSON.stringify(packageInfo, null, 2)}\n`) process.stdout.write(`${packageFile} was updated.\n`) } + +async function copyEditorConfig(baseDir) { + const source = path.join(__dirname, '..', '.editorconfig') + const target = path.join(baseDir, '.editorconfig') + await fs.copy(source, target) + + process.stdout.write(`${target} was updated.\n`) +} + +module.exports = async function init(baseDir = process.cwd()) { + await updatePackageFile(baseDir) + + await copyEditorConfig(baseDir) +} diff --git a/src/init.test.js b/src/init.test.js index bba7bef5..cec84ccc 100644 --- a/src/init.test.js +++ b/src/init.test.js @@ -5,16 +5,16 @@ import assert from 'assert' import init from './init' suite('init', () => { - let tmpdir + let workDir let packageJson setup('work directory', async () => { - tmpdir = path.join(os.tmpdir(), `cli-${Date.now()}`) - await fs.mkdirs(tmpdir) + workDir = path.join(os.tmpdir(), `cli-${Date.now()}`) + await fs.mkdirs(workDir) }) setup('package.json', async () => { - packageJson = path.join(tmpdir, 'package.json') + packageJson = path.join(workDir, 'package.json') await fs.writeJson(packageJson, { scripts: { test: 'abc' }, 'lint-staged': { '*.css': 'xyz' }, @@ -22,11 +22,11 @@ suite('init', () => { }) teardown(async () => { - await fs.remove(tmpdir) + await fs.remove(workDir) }) - test('success', async () => { - await init(tmpdir) + test('write package.json', async () => { + await init(workDir) const pkg = await fs.readJson(packageJson) assert.deepStrictEqual(pkg.scripts, { @@ -51,4 +51,12 @@ suite('init', () => { '*.css': 'xyz', }) }) + + test('write .editorconfig', async () => { + await init(workDir) + + const original = await fs.readFile(path.join(__dirname, '..', '.editorconfig'), 'utf8') + const wrote = await fs.readFile(path.join(workDir, '.editorconfig'), 'utf8') + assert(original === wrote) + }) })