« get me outta code hell

mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rwxr-xr-xindex.js51
1 files changed, 42 insertions, 9 deletions
diff --git a/index.js b/index.js
index a5930bc..7632844 100755
--- a/index.js
+++ b/index.js
@@ -12,8 +12,9 @@ import TelnetServer from './telnet.js'
 import {CommandLineInterface} from 'tui-lib/util/interfaces'
 import * as ansi from 'tui-lib/util/ansi'
 
-import {writeFile} from 'node:fs/promises'
+import {readFile, writeFile} from 'node:fs/promises'
 import os from 'node:os'
+import path from 'node:path'
 
 // Hack to get around errors when piping many things to stdout/err
 // (from general-util promisifyProcess)
@@ -48,12 +49,16 @@ async function main() {
         }
       }
     },
+
     'player-options': {type: 'series'},
     'stress-test': {type: 'flag'},
     'telnet-server': {type: 'flag'},
+    'skip-config-file': {type: 'flag'},
+    'config-file': {type: 'value'},
+
     [parseOptions.handleDashless](option) {
       playlistSources.push(option)
-    }
+    },
   })
 
   if (options['player-options'] && !options['player']) {
@@ -61,6 +66,30 @@ async function main() {
     process.exit(1)
   }
 
+  let jsonConfig = {}
+  let jsonError = null
+
+  const jsonPath =
+    (options['config-file']
+      ? path.resolve(options['config-file'])
+      : path.join(os.homedir(), '.mtui', 'config.json'))
+
+  try {
+    jsonConfig = JSON.parse(await readFile(jsonPath))
+  } catch (error) {
+    if (error.code !== 'ENOENT') {
+      jsonError = error
+    }
+  }
+
+  if (jsonError) {
+    console.error(`Error loading JSON config:`)
+    console.error(jsonError.message)
+    console.error(`Edit the file below to fix the error, or run mtui with --skip-config-file.`)
+    console.error(jsonPath)
+    process.exit(1)
+  }
+
   const backend = new Backend({
     playerName: options['player'],
     playerOptions: options['player-options']
@@ -107,13 +136,17 @@ async function main() {
   })
 
   if (playlistSources.length === 0) {
-    playlistSources.push({
-      name: 'My ~/Music Library',
-      comment: (
-        '(Add tracks and folders to ~/Music to make them show up here,' +
-        ' or pass mtui your own playlist.json file!)'),
-      source: ['crawl-local', os.homedir() + '/Music']
-    })
+    if (jsonConfig.defaultPlaylists) {
+      playlistSources.push(...jsonConfig.defaultPlaylists)
+    } else {
+      playlistSources.push({
+        name: 'My ~/Music Library',
+        comment: (
+          '(Add tracks and folders to ~/Music to make them show up here,' +
+          ' or pass mtui your own playlist.json file!)'),
+        source: ['crawl-local', os.homedir() + '/Music']
+      })
+    }
   }
 
   const loadPlaylists = async () => {