Skip to content

Commit

Permalink
Update "init"-command for use with node 6+
Browse files Browse the repository at this point in the history
- Add "thought" as dev-dependency instead of global install
- Omit "thoughtcheck", since node@6 ships with npm>2.14 already
- Tests for all
  • Loading branch information
nknapp committed Apr 7, 2017
1 parent 12d151a commit a6b3fe4
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 13 deletions.
22 changes: 9 additions & 13 deletions lib/init.js
Expand Up @@ -9,14 +9,15 @@ var qfs = require('m-io/fs')
// var _ = require('lodash')
var debug = require('debug')('thought:init')
var exec = require('./utils/exeq')
var thoughtPackageJson = require('../package.json')

module.exports = function () {
var packageJson
return qfs.read('package.json')
.then(function (contents) {
packageJson = JSON.parse(contents)
if (packageJson.scripts && packageJson.scripts.thought) {
throw new Error("I think scripts are already in your package.json ('scripts.thought' exists)")
throw new Error('I think scripts are already in your package.json (\'scripts.thought\' exists)')
}
})
.then(function () {
Expand All @@ -25,22 +26,17 @@ module.exports = function () {
.then(function () {
packageJson.scripts = packageJson.scripts || {}
packageJson.scripts.thought = 'thought run -a'
packageJson.scripts.prethoughtcheck = 'thought --version || npm -g install thought'
packageJson.scripts.thoughtcheck = 'thought check-engines'
if (packageJson.scripts.version) {
packageJson.scripts.version = 'npm run thought && ' + packageJson.scripts.version
} else {
packageJson.scripts.version = 'npm run thought'
}

if (packageJson.scripts.preversion) {
packageJson.scripts.preversion = 'npm run thoughtcheck && ' + packageJson.scripts.preversion
} else {
packageJson.scripts.preversion = 'npm run thoughtcheck'
}

packageJson.devDependencies.thought = `^${thoughtPackageJson.version}`
return qfs.write('package.json', JSON.stringify(packageJson, null, 2))
})
.then(function () {
return exec('npm', ['install', '--save-dev', 'thought'])
})
.then(function () {
return exec('git', ['commit', 'package.json', '-m', '[Thought] Added scripts to run thought on version-bumps'])
})
Expand All @@ -53,18 +49,18 @@ module.exports = function () {

/**
* Ensure that package.json is checked in and unmodified
* @return {Promise<boolean} true, if everything is fine
* @return {Promise<boolean>} true, if everything is fine
*/

function checkPackageJsonInGit () {
return exec('git', ['status', '--porcelain', 'package.json'])
.spread(function (stdout, stderr) {
.then(function ([stdout, stderr]) {
debug('git status --porcelain package.json', 'stdout', stdout, 'stderr', stderr)
if (stdout.indexOf('package.json') >= 0) {
throw new Error('package.json has changes!\n' +
'I would like to add scripts to your package.json, but ' +
'there are changes that have not been commited yet.\n' +
"I don't want to damage anything, so I'm not doing anyhting right now. " +
'I don\'t want to damage anything, so I\'m not doing anyhting right now. ' +
'Please commit your package.json')
}
})
Expand Down
159 changes: 159 additions & 0 deletions test/init-spec.js
@@ -0,0 +1,159 @@
/*!
* thought <https://github.com/nknapp/thought>
*
* Copyright (c) 2015 Nils Knappmeier.
* Released under the MIT license.
*/

/* eslint-env mocha */

'use strict'

var Scenario = require('./lib/scenarios')
var init = require('../lib/init')
var bluebird = require('bluebird')
var simpleGit = require('simple-git')

var chai = require('chai')
chai.use(require('chai-as-promised'))
chai.use(require('dirty-chai'))

var qfs = require('m-io/fs')

var expect = chai.expect

describe('The "init" option', function () {
this.timeout(15000)
it('should add scripts and devDependency to package.json', function () {
var scenario = new Scenario('simple-project').withTmpDir('test-output/thought-init1')
return scenario.prepareAndRun(() => {
var git = bluebird.promisifyAll(simpleGit(scenario.actual))

return git.initAsync()
.then(() => git.addAsync('package.json'))
.then(() => git.commitAsync('Initial checkin'))
.then(() => qfs.write('.gitignore', 'node_modules'))
.then(() => init())
.then(() => git.logAsync())
// Check only which files have been added to the index
.then(log => expect(log.latest.message, 'package.json must have been committed').to.equal('[Thought] Added scripts to run thought on version-bumps (HEAD -> master)'))
.then(() => qfs.read('package.json'))
.then(pkgJson => expect(JSON.parse(pkgJson), 'Checking package.json').to.deep.equals({
'author': '',
'description': 'A simple description',
'devDependencies': {
'fs-walker': '^1.0.0',
'thought': '^' + require('../package.json').version
},
'license': 'ISC',
'main': 'index.js',
'name': 'simple-project',
'repository': {
'type': 'git',
'url': 'https://github.com/unit-test/simple-project.git'
},
'scripts': {
'test': 'echo "Error: no test specified" && exit 1',
'thought': 'thought run -a',
'version': 'npm run thought'
},
'version': '1.0.0'
}))
.then(() => expect(qfs.exists('node_modules/thought'), 'Thought dependency must be installed').to.be.ok())
})
})

it('should add scripts and devDependency to package.json (even if no scripts-property exists', function () {
var scenario = new Scenario('simple-project').withTmpDir('test-output/thought-init1')
return scenario.prepareAndRun(() => {
var git = bluebird.promisifyAll(simpleGit(scenario.actual))

return git.initAsync()
.then(() => qfs.read('package.json'))
.then(JSON.parse)
.then((pkgJson) => {
delete pkgJson.scripts
return qfs.write('package.json', JSON.stringify(pkgJson))
})
.then(() => git.addAsync('package.json'))
.then(() => git.commitAsync('Initial checkin'))
.then(() => qfs.write('.gitignore', 'node_modules'))
.then(() => init())
.then(() => git.logAsync())
// Check only which files have been added to the index
.then(log => expect(log.latest.message, 'package.json must have been committed').to.equal('[Thought] Added scripts to run thought on version-bumps (HEAD -> master)'))
.then(() => qfs.read('package.json'))
.then(pkgJson => expect(JSON.parse(pkgJson), 'Checking package.json').to.deep.equals({
'author': '',
'description': 'A simple description',
'devDependencies': {
'fs-walker': '^1.0.0',
'thought': '^' + require('../package.json').version
},
'license': 'ISC',
'main': 'index.js',
'name': 'simple-project',
'repository': {
'type': 'git',
'url': 'https://github.com/unit-test/simple-project.git'
},
'scripts': {
'thought': 'thought run -a',
'version': 'npm run thought'
},
'version': '1.0.0'
}))
.then(() => expect(qfs.exists('node_modules/thought'), 'Thought dependency must be installed').to.be.ok())
})
})

it('should throw an exception, if the package.json-file has uncommitted changes', function () {
var scenario = new Scenario('simple-project').withTmpDir('test-output/thought-init2')
return scenario.prepareAndRun(() => {
var git = bluebird.promisifyAll(simpleGit(scenario.actual))
return git.initAsync()
.then(() => expect(init()).to.be.rejectedWith(/package.json has changes/))
})
})

it('should throw an exception, if Thought is already registered in a script', function () {
var scenario = new Scenario('simple-project').withTmpDir('test-output/thought-init2')
return scenario.prepareAndRun(() => {
var git = bluebird.promisifyAll(simpleGit(scenario.actual))

return git.initAsync()
.then(() => qfs.read('package.json'))

.then(JSON.parse)
.then((pkgJson) => {
pkgJson.scripts.thought = 'thought run -a'
return qfs.write('package.json', JSON.stringify(pkgJson))
})
.then(() => git.addAsync('package.json'))
.then(() => git.commitAsync('Initial checkin'))
.then(() => expect(init()).to.be.rejectedWith(/scripts are already in your package.json/))
})
})

it('should prepend the version script, if it already exists', function () {
var scenario = new Scenario('simple-project').withTmpDir('test-output/thought-init2')
return scenario.prepareAndRun(() => {
var git = bluebird.promisifyAll(simpleGit(scenario.actual))

return git.initAsync()
.then(() => qfs.read('package.json'))

.then(JSON.parse)
.then((pkgJson) => {
pkgJson.scripts.version = 'run something'
return qfs.write('package.json', JSON.stringify(pkgJson))
})
.then(() => git.addAsync('package.json'))
.then(() => git.commitAsync('Initial checkin'))
.then(() => init())
.then(() => qfs.read('package.json'))
.then(JSON.parse)
.then((pkgJson) => expect(pkgJson.scripts.version).to.equal('npm run thought && run something'))
})
})
})
29 changes: 29 additions & 0 deletions test/utils-spec.js
@@ -0,0 +1,29 @@
/*!
* thought <https://github.com/nknapp/thought>
*
* Copyright (c) 2015 Nils Knappmeier.
* Released under the MIT license.
*/

/* eslint-env mocha */

'use strict'

var chai = require('chai')
chai.use(require('chai-as-promised'))
chai.use(require('dirty-chai'))
var expect = chai.expect

describe('The "exeq" utility', function () {
var exec = require('../lib/utils/exeq')
this.timeout(10000)
it('should return stdout and stderr', function () {
return expect(exec(process.argv[0], ['-e', 'console.log("123");console.error("abc")']))
.to.eventually.deep.equal(['123\n', 'abc\n'])
})

it('should throw an error on exit-code > 0', function () {
return expect(exec(process.argv[0], ['-e', 'throw "Hommingberg"']))
.to.rejectedWith(/Command failed:.*Hommingberg/)
})
})

0 comments on commit a6b3fe4

Please sign in to comment.