Commit a7dcd416 authored by Deac Karns's avatar Deac Karns
Browse files

working on promises

parent 51d97247
node_modules/
package-lock.json
\ No newline at end of file
This diff is collapsed.
......@@ -7,13 +7,13 @@ const home = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE
const inquirer = require('inquirer');
const request = require('request');
const querystring = require('querystring');
const readlineLib = require('readline');
//const readlineLib = require('readline');
const opn = require('opn');
const readline = readlineLib.createInterface({
/*const readline = readlineLib.createInterface({
input: process.stdin,
output: process.stdout
});
});*/
program
.version('0.0.4')
......@@ -54,21 +54,18 @@ if (!fs.existsSync(home+'/.spotiko')) {
var config = loadConfig();
const promptForAuthorizationCode = function() {
return new Promise((resolve) => {
readline.question(colors.cyan('\nPaste the provided authentication code here:\n\n'), (answer) => {
readline.close();
resolve(answer)
})
});
}
var questions = [
{
type: 'input',
name: 'answer',
message: 'Paste the provided authentication code here:'
}
];
const promptForInput = function(prompt) {
return new Promise((resolve) => {
readline.question(colors.cyan('\n'+prompt+'\n\n'), (answer) => {
readline.close();
resolve(answer)
})
});
return inquirer.prompt(questions)
.then( (answers) => {
return answers.answer;
});
}
const refreshAccessToken = function() {
......@@ -215,20 +212,39 @@ const getDefinedPlaylist = function() {
})
}
const getAllPlaylists = function() {
let options = {
url: 'https://api.spotify.com/v1/me/playlists',
auth: {
'bearer': config.access_token
},
json: true
}
return new Promise(function(resolve, reject) {
request(options, function(error, response, body){
resolve(body);
})
})
}
const ensureClientId = function() {
if(!config.client_id){
return new Promise(function(resolve, reject) {
promptForInput('Enter your client id:')
.then((client_id) => {
writeToConfig({
client_id: client_id
});
resolve(client_id)
})
var questions = [
{
type: 'input',
name: 'answer',
message: 'Please enter your client ID:'
}
];
});
}
else{
return inquirer.prompt(questions)
.then( (answers) => {
writeToConfig({
client_id: answers.answer
});
return answers.answer;
});
}else{
return new Promise(function(resolve, reject) {
resolve(config.client_id)
});
......@@ -236,20 +252,27 @@ const ensureClientId = function() {
}
const ensureClientSecret = function() {
return new Promise(function(resolve, reject) {
if(!config.client_id){
console.log("----here 1")
promptForInput('Enter your client secret:')
.then((client_secret) => {
writeToConfig({
client_secret: client_secret
});
resolve(client_secret)
})
}else{
if(!config.client_secret){
var questions = [
{
type: 'input',
name: 'answer',
message: 'Please enter your client Secret:'
}
];
return inquirer.prompt(questions)
.then( (answers) => {
writeToConfig({
client_secret: answers.answer
});
return answers.answer;
});
}else{
return new Promise(function(resolve, reject) {
resolve(config.client_secret)
}
});
});
}
}
......@@ -285,18 +308,6 @@ const ensureTokens = function() {
}
}
ensureClientId()
.then(()=>{
return ensureClientSecret()
})
.then(() => {
return ensureTokens()
})
.finally(()=>{
runProgram();
})
const runProgram = function(){
if(program.add){
......@@ -337,15 +348,15 @@ const runProgram = function(){
refreshAccessToken()
.then( () => {
return getCurrentTrack()
.then( (track) => {
console.log(colors.cyan('\n[Track]'))
console.log(colors.magenta(' Artist: ')+track.album_artist);
console.log(colors.magenta(' Album: ')+track.album_name);
console.log(colors.magenta(' Name: ')+track.track_name);
console.log(colors.magenta(' ID: ')+track.track_id);
console.log(colors.magenta(' URI: ')+track.track_uri);
console.log(colors.magenta(' URL: ')+track.track_url);
})
.then( (track) => {
console.log(colors.cyan('\n[Track]'))
console.log(colors.magenta(' Artist: ')+track.album_artist);
console.log(colors.magenta(' Album: ')+track.album_name);
console.log(colors.magenta(' Name: ')+track.track_name);
console.log(colors.magenta(' ID: ')+track.track_id);
console.log(colors.magenta(' URI: ')+track.track_uri);
console.log(colors.magenta(' URL: ')+track.track_url);
})
})
.finally( () => process.exit())
}
......@@ -370,4 +381,12 @@ const runProgram = function(){
else {
program.help();
}
}
\ No newline at end of file
}
getAllPlaylists()
.then(pl => console.log(pl))
/*ensureClientId()
.then(ensureClientSecret)
.then(ensureTokens)
.finally(runProgram)*/
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment