Commit 51d97247 authored by Deac Karns's avatar Deac Karns
Browse files

promisify config prompts

parent 0ecb6276
{
"name": "spotiko",
"version": "0.0.3",
"version": "0.0.4",
"description": "a node CLI app for working with the spotify API",
"main": "spotiko",
"bin": {
......
......@@ -16,7 +16,7 @@ const readline = readlineLib.createInterface({
});
program
.version('0.0.3')
.version('0.0.4')
.option('-a, --add', 'Add current track to default playlist', 0)
.option('-s, --settings', 'Display the current configuration', 0)
.option('-c, --current', 'Display the current playing track', 0)
......@@ -111,12 +111,10 @@ const displayAuthorizeUrl = function() {
state: 'spotiko-attempt-authentication'
});
opn(authorizeUrl)
return new Promise(function(resolve){
console.log(colors.cyan('\nGo to the following address in your browser (if it doesnt open automatically):\n'))
console.log(colors.yellow(authorizeUrl));
resolve(true);
resolve(authorizeUrl);
})
}
......@@ -217,119 +215,159 @@ const getDefinedPlaylist = function() {
})
}
if(!config.client_id){
promptForInput('Enter your client id:')
.then((client_id) => {
writeToConfig(
{
client_id: client_id
}
);
})
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)
})
});
}
else{
return new Promise(function(resolve, reject) {
resolve(config.client_id)
});
}
}
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{
resolve(config.client_secret)
}
});
}
if(!config.client_secret){
promptForInput('Enter your client secret:')
.then((client_secret) => {
writeToConfig(
{
client_secret: client_secret
}
);
})
const ensureTokens = function() {
if(!config.authorization_code){
return new Promise(function(resolve, reject) {
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)
})
});
}
else{
return new Promise(function(resolve, reject) {
resolve(config.authorization_code)
});
}
}
if(!config.access_token){
console.reset()
displayAuthorizeUrl()
.then( () => {
promptForAuthorizationCode()
.then( (code) => {
writeToConfig({authorization_code:code});
return code;
})
.then( (code) => {
exchangeAuthorizationCodeForTokens(code)
.then(() => process.exit());
})
});
ensureClientId()
.then(()=>{
return ensureClientSecret()
})
.then(() => {
return ensureTokens()
})
.finally(()=>{
runProgram();
})
const runProgram = function(){
if(program.add){
refreshAccessToken()
.then( () => {
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);
return track
})
.then( (track) => {
return getDefinedPlaylist()
.then( (playlist) => {
console.log(colors.cyan('\n[Playlist]'))
console.log(colors.magenta(' Name: ')+playlist.name)
console.log(colors.magenta(' ID: ')+playlist.id)
return playlist
}).then( (playlist) => {
return addTrackToPlaylist(track, playlist)
})
.finally( () => {
console.log('\nTrack added to playlist\n');
process.exit()
})
})
})
}
}
else if(program.current){
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);
})
})
.finally( () => process.exit())
}
if(program.add){
refreshAccessToken()
.then( () => {
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);
return track
})
.then( (track) => {
return getDefinedPlaylist()
else if(program.playlist){
refreshAccessToken()
.then( () => {
return getDefinedPlaylist()
.then( (playlist) => {
console.log(colors.cyan('\n[Playlist]'))
console.log(colors.magenta(' Name: ')+playlist.name)
console.log(colors.magenta(' ID: ')+playlist.id)
return playlist
}).then( (playlist) => {
return addTrackToPlaylist(track, playlist)
})
.finally( () => {
console.log('\nTrack added to playlist\n');
process.exit()
})
})
})
}
else if(program.current){
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);
})
})
.finally( () => process.exit())
}
else if(program.playlist){
refreshAccessToken()
.then( () => {
return getDefinedPlaylist()
.then( (playlist) => {
console.log(colors.cyan('\n[Playlist]'))
console.log(colors.magenta(' Name: ')+playlist.name)
console.log(colors.magenta(' ID: ')+playlist.id)
})
})
.finally( () => process.exit())
}
})
.finally( () => process.exit())
}
else if(program.settings){
console.log(config)
}
else if(program.settings){
console.log(config)
}
else {
program.help();
}
else {
program.help();
}
}
\ 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