summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorprzemyslawpluta <przemekpluta@hotmail.com>2014-09-28 18:04:27 +0100
committerprzemyslawpluta <przemekpluta@hotmail.com>2014-09-28 18:04:27 +0100
commitde22e59896566ba9f680340e0e7d3744e539f4e4 (patch)
treec1d1689259cdfe438dba77ed07b3f6a91d642f3a
parent9945c1051913e889bed8503dad04d5c8b1249cbd (diff)
downloadmongo-edu-de22e59896566ba9f680340e0e7d3744e539f4e4.zip
update
add presets
-rw-r--r--lib/options.js70
-rw-r--r--mongo-edu.js60
2 files changed, 123 insertions, 7 deletions
diff --git a/lib/options.js b/lib/options.js
index 42e2616..8d743c5 100644
--- a/lib/options.js
+++ b/lib/options.js
@@ -6,13 +6,18 @@
* https://github.com/przemyslawpluta/mongo-edu/blob/master/LICENSE
*/
-var yargs = require('yargs')
+var fs = require('fs'),
+ path = require('path'),
+ _ = require('lodash'),
+ yargs = require('yargs')
.usage('Usage: $0 [options]')
.describe('d', 'download path').describe('u', 'email address')
.describe('h', 'switch from videos (default) to handouts').boolean('h')
.describe('py', 'py switch').describe('py', 'switch to point to Python')
.describe('proxy', 'pass proxy').describe('proxy', 'pass proxy switch for video download')
.describe('test', 'proxy test').describe('test', 'use with --proxy to test if usable')
+ .describe('save', 'save presets').describe('save', 'save presets for later use')
+ .describe('load', 'load presets').describe('load', 'load presets')
.describe('cw', 'switch from wiki\'s video lists (default) to courseware').boolean('cw')
.describe('cwd', 'same as --cw and dumps list of videos to file in -d').boolean('cwd')
.describe('cc', 'get closed captions').boolean('cc')
@@ -29,12 +34,75 @@ var yargs = require('yargs')
.example('$0 -d your_download_path --proxy http://proxy_ip_address:proxy_port_number --test', 'test proxy and download video via proxy tunnel')
.demand('d');
+var base = {},
+ optionsPath = path.join(__dirname, '..', '/bin/args.json');
+
+function readFromPath(callback) {
+
+ 'use strict';
+
+ fs.readFile(optionsPath, function readFile(err, data) {
+ if (err !== null) { return callback(err); }
+ callback(null, JSON.parse(data));
+ });
+}
+
+function saveToPath() {
+
+ 'use strict';
+
+ fs.writeFile(optionsPath, JSON.stringify(base), 'utf-8', function writeFile(err) {
+ if (err !== null) { return console.log('i'.red + ' Save Error: ' + err.stack); }
+ });
+}
+
+function saveOptions(name) {
+
+ 'use strict';
+
+ var handleRead = function handleRead(err, data) {
+ if (err !== null) { return console.log('i'.red + ' Unable To Read File: ' + err.stack); }
+ base = data;
+ base[name] = _.omit(yargs.argv, 'save', 'load', 'd', '_', '$0');
+ saveToPath();
+ };
+
+ fs.exists(optionsPath, function isFound(exists) {
+ if (!exists) {
+ base[name] = _.omit(yargs.argv, 'save', 'load', 'd', '_', '$0');
+ return saveToPath();
+ }
+ readFromPath(handleRead);
+ });
+}
+
+function loadOptions(name, callback) {
+
+ 'use strict';
+
+ if (typeof name === 'function') { callback = name; }
+
+ fs.exists(optionsPath, function isFound(exists) {
+ if (!exists) { return callback(null, []); }
+ readFromPath(function read(err, data) {
+ if (typeof name !== 'function') {
+ var status = !!data[name];
+ return callback(err, _.omit(_.defaults(data[name], yargs.argv), 'save', 'load'), status);
+ }
+ callback(null, Object.keys(data));
+ });
+ });
+}
+
module.exports = (function init() {
'use strict';
return {
build: function build() { return yargs; },
+ save: function save(name) { saveOptions(name); },
+ load: function load(name, callback) { loadOptions(name, callback); },
+ get: function get(callback) { loadOptions(callback); }
};
}());
diff --git a/mongo-edu.js b/mongo-edu.js
index fcee4ef..6876ce9 100644
--- a/mongo-edu.js
+++ b/mongo-edu.js
@@ -18,15 +18,17 @@ var pkg = require('./package'),
colors = require('colors'),
inquirer = require('inquirer');
+process.title = pkg.name;
+
exports.create = function start() {
'use strict';
- process.title = pkg.name;
console.log('\n[ ' + pkg.name.toUpperCase() + ' ' + pkg.version + ' ]\n');
- var argv = yargs.argv, proxyDetails = {}, lookFor = ((!argv.h)? 'Videos' : 'Handouts'), isWin = /^win/.test(process.platform), slash = (isWin) ? '\\' : '/';
+ var argv = yargs.argv, proxyDetails = {},
+ lookFor = ((!argv.h)? 'Videos' : 'Handouts'), isWin = /^win/.test(process.platform), slash = (isWin) ? '\\' : '/';
if (argv.help) { return yargs.showHelp(); }
@@ -34,9 +36,56 @@ exports.create = function start() {
if (argv.d.substr(-1) !== slash) { argv.d += slash; }
- validate.init(argv, function init(err, profile) {
- if (err !== null) { throw err; }
+ if (argv.load) {
+ if (typeof argv.load === 'string') {
+ optArgs.load(argv.load, function load(err, data, status) {
+ if (err !== null || !status) { return console.log('i'.red + ' Preset: ' + argv.load.green + ' not found.'); }
+ argv = data;
+ initRun();
+ });
+ } else {
+ optArgs.get(function get(err, data) {
+ if (err !== null || !data.length) { return console.log('i'.magenta + ' No Presets Found.'); }
+ inquirer.prompt([{ type: 'list', name: 'preset', message: 'Select Preset To Load:', choices: data}], function prompt(answers) {
+ optArgs.load(answers.preset, function load(err, data, status) {
+ if (err !== null || !status) { return console.log('i'.red + ' Unable To Load Preset: ' + argv.load); }
+ argv = data;
+ initRun();
+ });
+ });
+ });
+ }
+ }
+
+ function initRun() {
+ validate.init(argv, function init(err, profile) {
+ if (err !== null) { throw err; }
+ var savePrompt = [{ type: 'input', name: 'save', message: 'Missing [ --save ] Preset Name', default: '', validate: function(value) {
+ if (value !== '') { return true; }
+ return 'Please enter [ --save ] preset name';
+ }}];
+
+ if (argv.save) {
+ if (typeof argv.save === 'string') {
+ optArgs.save(argv.save);
+ } else {
+ return inquirer.prompt(savePrompt, function savePrompt(answers) {
+ argv.save = answers.save;
+ optArgs.save(answers.save);
+ initAndConfigure(profile);
+ });
+ }
+ }
+
+ initAndConfigure(profile);
+
+ });
+ }
+
+ if (!argv.load) { return initRun(); }
+
+ function initAndConfigure(profile) {
configure(argv, function conf(err) {
if (err !== null) { throw err; }
@@ -62,8 +111,7 @@ exports.create = function start() {
});
});
-
- });
+ }
function run(profile) {