Skip to content

Commit

Permalink
fix(init): missing scripts field in package.json (#39)
Browse files Browse the repository at this point in the history
If `scripts` or `scripts.test` field did not exist, error was raised.
  • Loading branch information
ybiquitous committed Oct 20, 2017
1 parent 34d563a commit bf3e0cb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ class Init {
const packageInfo = JSON.parse(await this.readFile('package.json'))

// update 'scripts'
if (!('scripts' in packageInfo)) {
packageInfo.scripts = {}
}
const { scripts } = packageInfo
if (!('test' in scripts)) {
scripts.test = 'test'
}
Object.assign(scripts, {
'test:watch': `${scripts.test} --watch`,
'test:coverage': 'echo "unsupported." && exit 1',
Expand Down
12 changes: 11 additions & 1 deletion test/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ suite('init', () => {
await fs.remove(workDir)
})

test('write package.json', async () => {
test('update package.json', async () => {
await exec('init')
const pkg = await fs.readJson(packageJson)

Expand Down Expand Up @@ -66,6 +66,16 @@ suite('init', () => {
})
})

test('update package.json without fields', async () => {
await fs.writeJson(packageJson, {})
await exec('init')
const pkg = await fs.readJson(packageJson)
assert('scripts' in pkg)
assert('test' in pkg.scripts)
assert('lint-staged' in pkg)
assert('standard-version' in pkg)
})

test('copy .editorconfig', async () => {
await exec('init')

Expand Down

0 comments on commit bf3e0cb

Please sign in to comment.