« get me outta code hell

Add bin script and some error handling - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-06-12 22:24:47 -0300
committerFlorrie <towerofnix@gmail.com>2018-06-12 22:24:47 -0300
commit70d6ea774ce47ca1a12cde8040c345de49beb29b (patch)
treebbc82345f75c3e003e8736e2404e032572311f19
parenta99273788ef30b79ddf2bccd52f1ac5a1d8cd383 (diff)
Add bin script and some error handling
-rwxr-xr-x[-rw-r--r--]index.js24
-rw-r--r--package.json4
2 files changed, 25 insertions, 3 deletions
diff --git a/index.js b/index.js
index 72c2383..e74f2eb 100644..100755
--- 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)
diff --git a/package.json b/package.json
index e2501ab..047b6bb 100644
--- a/package.json
+++ b/package.json
@@ -3,8 +3,8 @@
   "version": "1.0.0",
   "description": "",
   "main": "index.js",
-  "scripts": {
-    "test": "echo \"Error: no test specified\" && exit 1"
+  "bin": {
+    "mtui": "./index.js"
   },
   "author": "",
   "license": "GPL-3.0",