diff options
Diffstat (limited to 'index.js')
-rwxr-xr-x | index.js | 97 |
1 files changed, 62 insertions, 35 deletions
diff --git a/index.js b/index.js index 3ecd59b..fea9222 100755 --- a/index.js +++ b/index.js @@ -2,42 +2,26 @@ // omg I am tired of code -const { getAllCrawlersForArg } = require('./crawlers') -const { getPlayer } = require('./players') -const { parseOptions } = require('./general-util') -const AppElement = require('./ui') -const Backend = require('./backend') -const TelnetServer = require('./telnet') -const processSmartPlaylist = require('./smart-playlist') -const setupClient = require('./client') - -const { +import {getPlayer} from './players.js' +import {parseOptions} from './general-util.js' +import {getItemPathString} from './playlist-utils.js' +import Backend from './backend.js' +import setupClient from './client.js' +import TelnetServer from './telnet.js' + +import { makeSocketServer, makeSocketClient, attachBackendToSocketClient, - attachSocketServerToBackend -} = require('./socket') - -const { - getItemPathString, - updatePlaylistFormat -} = require('./playlist-utils') - -const { - ui: { - Root - }, - util: { - ansi, - CommandLineInterfacer, - Flushable - } -} = require('tui-lib') + attachSocketServerToBackend, +} from './socket.js' + +import {CommandLineInterface} from 'tui-lib/util/interfaces' +import * as ansi from 'tui-lib/util/ansi' -const { promisify } = require('util') -const fs = require('fs') -const readFile = promisify(fs.readFile) -const writeFile = promisify(fs.writeFile) +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) @@ -72,15 +56,19 @@ async function main() { } } }, + 'player-options': {type: 'series'}, 'stress-test': {type: 'flag'}, 'socket-client': {type: 'value'}, 'socket-name': {type: 'value'}, 'socket-server': {type: 'value'}, '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']) { @@ -88,6 +76,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'] @@ -110,11 +122,11 @@ async function main() { const { appElement, dirtyTerminal, flushable, root } = await setupClient({ backend, - interfacer: new CommandLineInterfacer(), + screenInterface: new CommandLineInterface(), writable: process.stdout, appConfig: { showPartyControls: !!(options['socket-server'] || options['socket-client']) - } + }, }) appElement.on('quitRequested', () => { @@ -136,6 +148,20 @@ async function main() { root.renderNow() }) + if (playlistSources.length === 0) { + 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 () => { for (const source of playlistSources) { await appElement.loadPlaylistOrSource(source, true) @@ -191,6 +217,7 @@ async function main() { root.h = h root.fixAllLayout() + /* eslint-disable-next-line no-unused-vars */ const XXstress = func => '[disabled]' const stress = func => { |