From 9645f08769860992374caa0de2f5dbe83436f27d Mon Sep 17 00:00:00 2001 From: Kevin Gisi Date: Tue, 14 Jul 2015 23:22:02 -0400 Subject: [PATCH] Add a 'gulp console' method which preloads Mongo, models, and sets up utility methods --- tasks/gulp-console.js | 52 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tasks/gulp-console.js diff --git a/tasks/gulp-console.js b/tasks/gulp-console.js new file mode 100644 index 0000000000..5b98853845 --- /dev/null +++ b/tasks/gulp-console.js @@ -0,0 +1,52 @@ +import 'coffee-script'; + +import mongoose from 'mongoose'; +import autoinc from 'mongoose-id-autoinc'; +import logging from '../website/src/logging'; +import nconf from 'nconf'; +import utils from '../website/src/utils'; +import repl from 'repl'; +import gulp from 'gulp'; + +// Add additional properties to the repl's context +let improveRepl = (context) => { + + // Let "exit" and "quit" terminate the console + ['exit', 'quit'].forEach((term) => { + Object.defineProperty(context, term, { get() { process.exit(); }}); + }); + + // "clear" clears the screen + Object.defineProperty(context, 'clear', { get() { + process.stdout.write('\u001B[2J\u001B[0;0f'); + }}); + + utils.setupConfig(); + + context.Challenge = require('../website/src/models/challenge').model; + context.Group = require('../website/src/models/group').model; + context.User = require('../website/src/models/user').model; + + var isProd = nconf.get('NODE_ENV') === 'production'; + var mongooseOptions = !isProd ? {} : { + replset: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } }, + server: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } } + }; + autoinc.init( + mongoose.connect( + nconf.get('NODE_DB_URI'), + mongooseOptions, + function(err) { + if (err) throw err; + logging.info('Connected with Mongoose'); + } + ) + ); + +}; + +gulp.task('console', (cb) => { + improveRepl(repl.start({ + prompt: 'Habitica > ' + }).context); +});