From a36e372ba88b59e08fa938f76b261fdc2797bef2 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 13 May 2023 18:11:34 -0300 Subject: load basic config file from ~/.mtui/config.json This is basically just a stub for now, but you can specify what playlists you want open when mtui is called without any provided directories/playlists in "defaultPlaylists'! Also comes with complementary --config-file and --skip-config-file arguments. --- index.js | 51 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file 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 () => { -- cgit 1.3.0-6-gf8a5