Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 79d3cb349a
Fetching contributors…

Cannot retrieve contributors at this time

executable file 96 lines (80 sloc) 2.66 kb
#!/usr/bin/env node
/*
Configures the Tests Suites
*/
var fs = require('fs'),
path = require('path'),
util = require('util'),
readline = require('readline'),
configPath = path.resolve(__dirname, '../test/fixtures/dbconfig.json'),
EventEmitter = require('events').EventEmitter;
function ObjectPrompt(jsonPath) {
this.jsonPath = jsonPath;
this.config = require(configPath);
this.keys = Object.keys(this.config);
this.objectsOnHold = [];
this.keysOnHold = [];
this.contextKeys = [];
this.currentContext = '';
if (this.keys.length > 0) {
console.log(colorize('\nTest Configuration Tool', '1;30'));
console.log('\nEditing ' + colorize(jsonPath.replace(process.cwd()+'/', ''), '0;31'));
} else {
console.log('\nNothing to be done\n');
return;
}
this.rl = readline.createInterface(process.stdin, process.stdout);
this.rl.on('close', function() {
process.stdin.destroy();
console.log('');
process.exit();
});
this.on('finalize', this.writeFile);
this.on('next_prompt', this.nextPrompt);
this.emit('next_prompt');
}
util.inherits(ObjectPrompt, EventEmitter);
ObjectPrompt.prototype.nextPrompt = function() {
var val, self = this, key = this.keys.shift();
if (key == null) this.emit('finalize');
else {
val = this.config[key];
if (typeof val == 'object') {
this.objectsOnHold.push(this.config);
this.keysOnHold.push(this.keys);
this.contextKeys.push(key);
this.currentContext = this.contextKeys.join('.') + '.';
this.config = val;
this.keys = Object.keys(val);
this.emit('next_prompt');
} else {
this.rl.question(util.format("%s%s [%s]> ", this.currentContext, key, val), function(answer) {
if (answer.trim().length > 0) { self.config[key] = answer; }
self.emit('next_prompt');
});
}
}
}
ObjectPrompt.prototype.writeFile = function() {
var self = this;
if (this.objectsOnHold.length === 0) {
// If no contexts on hold, finalize
var json = JSON.stringify(this.config, null, 2);
fs.writeFile(this.jsonPath, json, function(err) {
var msg = (err) ? '\n✗ Unable to save config' : '\n✔ Successfully saved';
console.log(colorize(msg, '1;30'))
self.rl.close();
});
} else {
// Restore original context
this.config = this.objectsOnHold.pop();
this.keys = this.keysOnHold.pop();
this.contextKeys.pop();
this.currentContext = this.contextKeys.join('.') + '.';
this.emit('next_prompt');
}
}
function colorize(str, color) {
return util.format('\u001b[%sm%s\u001b[0m', color, str);
}
new ObjectPrompt(configPath);
Jump to Line
Something went wrong with that request. Please try again.