summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorprzemyslawpluta <przemekpluta@hotmail.com>2014-09-28 18:17:43 +0100
committerprzemyslawpluta <przemekpluta@hotmail.com>2014-09-28 18:17:43 +0100
commit50b4cf93765ad4a74672baa12f8d59772356aad9 (patch)
tree90b03885c1015ad45c3dfc387ee01968006b501b /lib
parentde22e59896566ba9f680340e0e7d3744e539f4e4 (diff)
downloadmongo-edu-50b4cf93765ad4a74672baa12f8d59772356aad9.zip
cleanup
Diffstat (limited to 'lib')
-rw-r--r--lib/initialize.js103
1 files changed, 103 insertions, 0 deletions
diff --git a/lib/initialize.js b/lib/initialize.js
new file mode 100644
index 0000000..cbb2d99
--- /dev/null
+++ b/lib/initialize.js
@@ -0,0 +1,103 @@
+/*
+ * mongo-edu
+ *
+ * Copyright (c) 2014 Przemyslaw Pluta
+ * Licensed under the MIT license.
+ * https://github.com/przemyslawpluta/mongo-edu/blob/master/LICENSE
+ */
+
+var mdbvideos = require('./login'),
+ videoHandler = require('./videos'),
+ colors = require('colors'),
+ inquirer = require('inquirer');
+
+module.exports = function run(profile, argv) {
+
+ 'use strict';
+
+ var lookFor = ((!argv.h)? 'Videos' : 'Handouts');
+
+ inquirer.prompt(profile, function prompt(answers) {
+
+ var list = [{ type: 'list', name: 'url', message: '', choices: [] }], classes = list,
+
+ check = [{ type: 'checkbox', message: '', name: 'videos', choices: [],
+ validate: function validate(answer) {
+ if ( answer.length < 1 ) { return 'You must choose at least one option.'; }
+ return true;
+ }
+ }];
+
+ mdbvideos.init(answers, argv, function get(err, data) {
+ if (err !== null) { throw err; }
+
+ if (data.length) {
+
+ classes[0].message = 'Found ' + data.length + ' Course'+ ((data.length > 1)? 's' : '') + '. Select:';
+ classes[0].choices = data;
+ return currentList();
+
+ }
+
+ });
+
+ function currentList() {
+ inquirer.prompt(classes, function prompt(answers) {
+
+ mdbvideos.getList(answers, argv, function get(err, data, pass) {
+ if (err !== null) { throw err; }
+
+ if (data.length) {
+
+ if (pass) { return showDetails(err, data); }
+
+ list[0].message = 'Found ' + data.length + ' List' + ((data.length > 1)? 's' : '') + '. Select:';
+ list[0].choices = data;
+
+ return currentVideos();
+
+ } else {
+ if (pass) { return console.log('i'.red + ' Looks like the course is not yet available or has already ended. ' +
+ lookFor + ' list is not available.\n\nCheck the start/end date for selected course.\n'); }
+ }
+
+ return console.log('i'.red + ' Unable to locate any ' + lookFor.toLowerCase() + ' lists in the wiki. Are ' +
+ lookFor.toLowerCase() + ' list present in the wiki?' +
+ ((lookFor === 'Videos') ? ' Try to add ' + '--cw'.green + ' to switch and search on courseware instead.' : ''));
+ });
+
+ });
+ }
+
+ function currentVideos() {
+
+ inquirer.prompt(list, function prompt(answers) {
+
+ mdbvideos.listVideos(answers, argv, function get(err, data, pass) {
+ if (err !== null) { throw err; }
+ if (!pass) { return videoHandler.details(data, argv, showDetails); }
+ showDetails(err, data);
+ });
+
+ });
+ }
+
+ function showDetails(err, data) {
+ if (err !== null) { throw err; }
+
+ if (data.length) {
+ check[0].message = 'Select From ' + (data.length - 2) + ' ' + lookFor + '. Download:';
+ check[0].choices = data;
+
+ return inquirer.prompt(check, function prompt(answers) {
+ videoHandler.download(answers, data, argv);
+ });
+
+ }
+
+ console.log('i'.red + ' Could not locate any ' + lookFor.toLowerCase() + '.'); process.exit(0);
+ }
+
+ });
+
+};