From ebf010cee4a45d14b8890b73d5ab0e06f99c718c Mon Sep 17 00:00:00 2001 From: przemyslawpluta Date: Fri, 14 Aug 2015 14:54:23 +0100 Subject: switch to courseware search if wiki returns no results --- lib/initialize.js | 89 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 56 insertions(+), 33 deletions(-) (limited to 'lib') diff --git a/lib/initialize.js b/lib/initialize.js index 451c734..a134ed9 100644 --- a/lib/initialize.js +++ b/lib/initialize.js @@ -34,15 +34,23 @@ module.exports = function run(profile, argv) { var list = prompts.list, classes = list, check = prompts.check, confirm = prompts.confirm; - mdbvideos.init(answers, argv, function get(err, data, profile) { + function init(answers) { - if (err !== null) { throw err; } + mdbvideos.init(answers, argv, function get(err, data, profile) { + + if (err !== null) { throw err; } + + if (profile.preset.video[1] === videoPreference || argv.h) { return processList(data, profile); } - if (profile.preset.video[1] === videoPreference || argv.h) { return processList(data, profile); } + confirm[0].message = 'Your current video preference is set to ' + profile.preset.video[1].underline + '. Switch to: ' + videoPreference.green; - confirm[0].message = 'Your current video preference is set to ' + profile.preset.video[1].underline + '. Switch to: ' + videoPreference.green; + profileAssesment(confirm, data); + + }); + } - return inquirer.prompt(confirm, function prompt(answers) { + function profileAssesment(confirm, data) { + inquirer.prompt(confirm, function prompt(answers) { if (!answers.confirm) { return console.log('i'.red + ' ' + pkg.name + ' requires video preferences set to ' + videoPreference + '.\n'); } mdbvideos.updateProfile({ @@ -54,50 +62,63 @@ module.exports = function run(profile, argv) { }); }); + } - }); + function getClassContent(content) { + mdbvideos.getList(content, argv, function get(err, data, pass) { + if (err !== null) { throw err; } - function processList(data, profile) { - - if (!data.length) { - if (profile.noCourses) { return console.log('i'.red + ' ' + checkName(profile) + ' ' + profile.noCourses); } - return console.log('i'.red + ' ' + checkName(profile) + ' ' + 'Could not locate any courses in your profile. Have you registered already?\n'); - } + if (data.length) { - classes[0].message = checkName(profile) + ' Found ' + data.length + ' Course'+ ((data.length > 1)? 's' : '') + '. Select:'; - classes[0].choices = data; - currentList(); + if (pass) { return showDetails(err, data); } - } + list[0].message = 'Found ' + data.length + ' List' + ((data.length > 1)? 's' : '') + '. Select:'; + list[0].choices = data; - function currentList() { - inquirer.prompt(classes, function prompt(answers) { + return currentVideos(); - mdbvideos.getList(answers, argv, function get(err, data, pass) { - if (err !== null) { throw err; } + } 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'); } + } - if (data.length) { + console.log('i'.red + ' Unable to locate any ' + lookFor.toLowerCase() + ' lists in the wiki. Are ' + lookFor.toLowerCase() + ' list present?'); - if (pass) { return showDetails(err, data); } + if (lookFor === 'Videos' && !argv.cw) { - list[0].message = 'Found ' + data.length + ' List' + ((data.length > 1)? 's' : '') + '. Select:'; - list[0].choices = data; + confirm[0].message = 'Default wiki search returned no resuts. Perform courseware search with ' + '--cw'.green + ' ?'; - return currentVideos(); + inquirer.prompt(confirm, function prompt(answers) { + if (!answers.confirm) { return; } + argv.cw = true; + content.url = content.url.replace(/course_wiki/g, '') + 'courseware'; + getClassContent(content); + }); - } 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?' + - ((lookFor === 'Videos') ? '\n\n' + ((!argv.cw) ? 'Try to add ' + '--cw'.green + ' to switch and search on courseware instead.\n' : '') : '')); - }); + }); + } + function currentList() { + inquirer.prompt(classes, function prompt(answers) { + getClassContent(answers); }); } + function processList(data, profile) { + + if (!data.length) { + if (profile.noCourses) { return console.log('i'.red + ' ' + checkName(profile) + ' ' + profile.noCourses); } + return console.log('i'.red + ' ' + checkName(profile) + ' ' + 'Could not locate any courses in your profile. Have you registered already?\n'); + } + + classes[0].message = checkName(profile) + ' Found ' + data.length + ' Course'+ ((data.length > 1)? 's' : '') + '. Select:'; + classes[0].choices = data; + currentList(); + + } + function currentVideos() { inquirer.prompt(list, function prompt(answers) { @@ -127,6 +148,8 @@ module.exports = function run(profile, argv) { console.log('i'.red + ' Could not locate any ' + lookFor.toLowerCase() + '.'); process.exit(0); } + init(answers); + }); }; -- cgit v1.2.3