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

better initial quesions, no more awkward stalls

parent c7373fe9
......@@ -133,20 +133,21 @@ const exchangeAuthorizationCodeForTokens = function(code) {
},
json: true
}
return new Promise(function(resolve, reject) {
request.post(options, function(error, response, body) {
if (!error && response.statusCode === 200) {
writeToConfig(
{
access_token:body.access_token,
refresh_token:body.refresh_token
}
);
resolve(true);
}
return new Promise(function(resolve, reject) {
return request.post(options, function(error, response, body) {
if (!error && response.statusCode === 200) {
writeToConfig(
{
access_token:body.access_token,
refresh_token:body.refresh_token
}
);
config.access_token = body.access_token;
config.refresh_token = body.refresh_token
resolve(config);
}
});
});
});
}
const addTrackToPlaylist = function(track, playlist) {
......@@ -262,6 +263,7 @@ const ensureClientId = function() {
writeToConfig({
client_id: answers.answer
});
config.client_id = answers.answer;
return answers.answer;
});
}else{
......@@ -286,6 +288,7 @@ const ensureClientSecret = function() {
writeToConfig({
client_secret: answers.answer
});
config.client_secret = answers.answer;
return answers.answer;
});
}else{
......@@ -298,32 +301,31 @@ const ensureClientSecret = function() {
const ensureTokens = function() {
if(!config.authorization_code){
return new Promise(function(resolve, reject) {
return new Promise(function(resolve) {
console.reset()
displayAuthorizeUrl()
.then( (authorizeUrl) => {
return new Promise(function(resolve, reject) {
opn(authorizeUrl)
resolve(true)
});
})
.then( () => {
return promptForAuthorizationCode()
})
.then( (code) => {
writeToConfig({authorization_code:code});
return code;
})
.then( (code) => {
return exchangeAuthorizationCodeForTokens(code)
})
return displayAuthorizeUrl()
.then( (authorizeUrl) => {
return new Promise(function(resolve, reject) {
opn(authorizeUrl)
resolve(true)
});
})
.then( () => {
return promptForAuthorizationCode()
})
.then( (code) => {
writeToConfig({authorization_code:code});
return code;
})
.then( (code) => {
resolve(exchangeAuthorizationCodeForTokens(code))
})
});
}
else{
return new Promise(function(resolve, reject) {
resolve(config.authorization_code)
return new Promise(function(resolve) {
resolve(config)
});
}
}
......@@ -336,27 +338,27 @@ const ProgramPlaylist = function(playlist=null) {
}
return dynpl
.then( pl => {
return getPlaylistTracks(pl)
})
.then( t => {
t.items.forEach( function(i) {
table.push(
[i.track.name, i.track.artists.map( (artist) => artist.name).toString(), i.track.album.name]
);
})
return table;
})
.then( t => {
console.log(table.toString())
return t
.then( pl => {
return getPlaylistTracks(pl)
})
.then( t => {
t.items.forEach( function(i) {
table.push(
[i.track.name, i.track.artists.map( (artist) => artist.name).toString(), i.track.album.name]
);
})
return table;
})
.then( t => {
console.log(table.toString())
return t
})
}
const runProgram = function(){
if(program.add){
refreshAccessToken()
return refreshAccessToken()
.then( () => {
getCurrentTrack()
.then( track => {
......@@ -377,29 +379,30 @@ const runProgram = function(){
})
})
.then( playlist => {
ProgramPlaylist(playlist)
return ProgramPlaylist(playlist)
})
})
}
else if(program.current){
refreshAccessToken()
.then( () => {
return getCurrentTrack()
return 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);
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);
return true
})
})
}
else if(program.playlist){
ProgramPlaylist()
return ProgramPlaylist()
}
else if(program.settings){
......@@ -412,6 +415,7 @@ const runProgram = function(){
}
ensureClientId()
.then(ensureClientSecret)
.then(ensureTokens)
.finally(runProgram)
\ No newline at end of file
.then(ensureClientSecret)
.then(ensureTokens)
.then(runProgram)
.finally(process.exit)
\ 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