From 9d278d956cd1503fbba597c4a84e4b032c83705f Mon Sep 17 00:00:00 2001 From: liam4 Date: Tue, 18 Jul 2017 18:25:18 -0300 Subject: New option --write-playlist (alias --save) which writes active playlist to a file; updated man page --- src/http-music.js | 65 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/http-music.js b/src/http-music.js index a28b0f2..6457ef7 100755 --- a/src/http-music.js +++ b/src/http-music.js @@ -16,6 +16,7 @@ const { } = require('./playlist-utils') const readFile = promisify(fs.readFile) +const writeFile = promisify(fs.writeFile) function downloadPlaylistFromURL(url) { return fetch(url).then(res => res.text()) @@ -104,15 +105,64 @@ Promise.resolve() 'h': util => util.alias('-help'), '?': util => util.alias('-help'), - '-open': async function(util) { - // --open (alias: -o) + '-open-playlist': async function(util) { + // --open-playlist (alias: --open, -o) // Opens a separate playlist file. // This sets the source playlist. await openPlaylist(util.nextArg()) }, - 'o': util => util.alias('-open'), + '-open': util => util.alias('-open-playlist'), + 'o': util => util.alias('-open-playlist'), + + '-write-playlist': function(util) { + // --write-playlist (alias: --write, -w, --save) + // Writes the active playlist to a file. This file can later be used + // with --open ; you won't need to stick in all the filtering + // options again. + + requiresOpenPlaylist() + + const playlistString = JSON.stringify(activePlaylist, null, 2) + const file = util.nextArg() + + console.log(`Saving playlist to file ${file}...`) + + return writeFile(file, playlistString).then(() => { + console.log("Saved.") + + // If this is the last option, the user probably doesn't actually + // want to play the playlist. (We need to check if this is len - 2 + // rather than len - 1, because of the option that comes + // after --write-playlist.) + if (util.index === util.argv.length - 2) { + shouldPlay = false + } + }) + }, + + '-write': util => util.alias('-write-playlist'), + 'w': util => util.alias('-write-playlist'), + '-save': util => util.alias('-write-playlist'), + + '-print-playlist': function(util) { + // --print-playlist (alias: --log-playlist, --json) + // Prints out the JSON representation of the active playlist. + + requiresOpenPlaylist() + + console.log(JSON.stringify(activePlaylist, null, 2)) + + // As with --write-playlist, the user probably doesn't want to actually + // play anything if this is the last option. + if (util.index === util.argv.length - 1) { + shouldPlay = false + } + }, + + '-log-playlist': util => util.alias('-print-playlist'), + '-json': util => util.alias('-print-playlist'), '-clear': function(util) { // --clear (alias: -c) @@ -226,15 +276,6 @@ Promise.resolve() // Sets command line options passed to the `play` command. playOpts = util.nextArg().split(' ') - }, - - '-debug-list': function(util) { - // --debug-list - // Prints out the JSON representation of the active playlist. - - requiresOpenPlaylist() - - console.log(JSON.stringify(activePlaylist, null, 2)) } } -- cgit 1.3.0-6-gf8a5