From 70d6ea774ce47ca1a12cde8040c345de49beb29b Mon Sep 17 00:00:00 2001 From: Florrie Date: Tue, 12 Jun 2018 22:24:47 -0300 Subject: Add bin script and some error handling --- index.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) mode change 100644 => 100755 index.js (limited to 'index.js') diff --git a/index.js b/index.js old mode 100644 new mode 100755 index 72c2383..e74f2eb --- a/index.js +++ b/index.js @@ -1,7 +1,11 @@ +#!/usr/bin/env node + // omg I am tired of code const { AppElement } = require('./ui') const { updatePlaylistFormat } = require('./playlist-utils') +const fs = require('fs') +const util = require('util') const processSmartPlaylist = require('./smart-playlist') const ansi = require('./tui-lib/util/ansi') const CommandLineInterfacer = require('./tui-lib/util/CommandLineInterfacer') @@ -9,6 +13,8 @@ const EventEmitter = require('events') const Flushable = require('./tui-lib/util/Flushable') const Root = require('./tui-lib/ui/Root') +const readFile = util.promisify(fs.readFile) + // Hack to get around errors when piping many things to stdout/err // (from general-util promisifyProcess) process.stdout.setMaxListeners(Infinity) @@ -69,7 +75,23 @@ async function main() { flushable.write(ansi.moveCursor(0, 0)) flushable.write('Opening playlist...') flushable.flush() - grouplike = require(process.argv[2]) + + let grouplikeText + try { + grouplikeText = await readFile(process.argv[2]) + } catch (error) { + flushable.write('Error opening the passed file "' + process.argv[2] + '"!') + flushable.flush() + process.exit(1) + } + + try { + grouplike = JSON.parse(grouplikeText) + } catch (error) { + flushable.write('Error parsing the passed file as JSON!') + flushable.flush() + process.exit(1) + } } grouplike = await processSmartPlaylist(grouplike) -- cgit 1.3.0-6-gf8a5