summaryrefslogtreecommitdiff
path: root/lib/options.js
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 /lib/options.js
parent9945c1051913e889bed8503dad04d5c8b1249cbd (diff)
downloadmongo-edu-de22e59896566ba9f680340e0e7d3744e539f4e4.zip
update
add presets
Diffstat (limited to 'lib/options.js')
-rw-r--r--lib/options.js70
1 files changed, 69 insertions, 1 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); }
};
}());