diff --git a/src/init.js b/src/init.js index 027b64d5..32dd6b82 100644 --- a/src/init.js +++ b/src/init.js @@ -2,8 +2,6 @@ const path = require('path') const fs = require('fs-extra') const originalPackage = require('../package.json') -const targetProps = ['scripts', 'lint-staged'] - function stdout(str) { process.stdout.write(`${str}\n`) } @@ -30,11 +28,18 @@ class Init { async updatePackageFile() { const packageInfo = JSON.parse(await this.readFile('package.json')) - targetProps.forEach((prop) => { - packageInfo[prop] = { ...originalPackage[prop], ...packageInfo[prop] } + const { scripts } = packageInfo + Object.assign(scripts, { + 'test:watch': `${scripts.test} --watch`, + 'test:coverage': 'echo "unsupported." && exit 1', }) - packageInfo.scripts['test:watch'] = `${packageInfo.scripts.test} --watch` - packageInfo.scripts['test:coverage'] = 'echo "unsupported." && exit 1' + Object.keys(originalPackage.scripts) + .filter(key => !(key === 'test' || key.startsWith('test:'))) + .forEach((key) => { + scripts[key] = originalPackage.scripts[key] + }) + + Object.assign(packageInfo['lint-staged'], originalPackage['lint-staged']) await this.writeFile('package.json', JSON.stringify(packageInfo, null, 2)) } diff --git a/test/init.test.js b/test/init.test.js index 21a0aad3..598a75d3 100644 --- a/test/init.test.js +++ b/test/init.test.js @@ -20,7 +20,7 @@ suite('init', () => { setup('package.json', async () => { packageJson = path.join(workDir, 'package.json') await fs.writeJson(packageJson, { - scripts: { test: 'abc' }, + scripts: { test: 'abc', 'lint:js': 'eslint .' }, 'lint-staged': { '*.css': 'xyz' }, }) }) @@ -36,6 +36,7 @@ suite('init', () => { const pkg = await fs.readJson(packageJson) assert.deepStrictEqual(pkg.scripts, { + build: 'babel src/ -d lib/', commitmsg: 'commitlint -e', 'lint:js': 'eslint --max-warnings=-1 --ignore-path=.gitignore --ext=.js --ext=.jsx .', 'lint:js:fix': 'npm run lint:js -- --fix', @@ -47,7 +48,6 @@ suite('init', () => { test: 'abc', 'test:watch': 'abc --watch', 'test:coverage': 'echo "unsupported." && exit 1', - build: 'babel src/ -d lib/', }) assert.deepStrictEqual(pkg['lint-staged'], {