Skip to content

Commit

Permalink
improve load-database script
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent C. Dodds committed Feb 17, 2017
1 parent 60b5228 commit 8f69bc6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion api/scripts/generate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ connectPromise
console.log(chalk.bgGreen.white.bold('operation successful'))
console.log(
chalk.green.bold(
`insertted ${users.length} users, ` +
`inserted ${users.length} users, ` +
`${articles.length} articles, ` +
`and ${comments.length} comments`,
),
Expand Down
1 change: 1 addition & 0 deletions package-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = {
`mongod --dbpath ${path.join(__dirname, './.mongo-db')} --quiet`,
]),
description: 'Create the .mongo-db dir and start the mongod process',
stop: 'mongo admin --eval "db.shutdownServer()"',
},
api: {
script: series(['cd api', 'npm start']),
Expand Down
55 changes: 35 additions & 20 deletions scripts/load-database.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
const spawn = require('spawn-command-with-kill')

console.log('spawning')
const mongodChild = spawn('npm start mongo --silent', {stdio: 'inherit'})
const stopMongo = 'npm start mongo.stop'
console.log(`stopping mongod with "${stopMongo}" (in case it is running)`)
const stopMongoChild = spawn(stopMongo, {stdio: 'ignore'})

process.on('exit', cleanUp)
process.on('SIGINT', cleanUp)
process.on('uncaughtException', cleanUp)
stopMongoChild.on('exit', () => {
const startMongo = 'npm start mongo'
console.log(`starting mongod with "${startMongo}"`)
const mongodChild = spawn(startMongo, {stdio: 'ignore'})
let killed = false

setTimeout(spawnGenerate, 3000)
process.on('exit', cleanUp)
process.on('SIGINT', cleanUp)
process.on('uncaughtException', cleanUp)

setTimeout(spawnGenerate, 3000)

function spawnGenerate() {
const babelNode = './node_modules/.bin/babel-node'
const generateCommand = `cd api && ${babelNode} scripts/generate`
console.log(`generating and inserting data with "${generateCommand}"`)
const generateChild = spawn(generateCommand, {
stdio: 'inherit',
})
generateChild.on('exit', cleanUp)
}

function cleanUp() {
if (!killed) {
killed = true
console.log('killing mongod process')
mongodChild.kill()
setTimeout(() => {
// wait for mongo to die
}, 1000)
}
}
})

function spawnGenerate() {
const babelNode = './node_modules/.bin/babel-node'
const generateChild = spawn(`cd api && ${babelNode} scripts/generate`, {
stdio: 'inherit',
})
generateChild.on('exit', cleanUp)
}

function cleanUp() {
console.log('killing mongod process')
mongodChild.kill()
setTimeout(() => {
// wait for mongo to die
}, 1000)
}

0 comments on commit 8f69bc6

Please sign in to comment.