From 43f1a1dd1b44065663a797603012394c52a9baea Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 13 May 2023 13:31:58 -0300 Subject: use ESM module syntax & update tui-lib Exciting update! This doesn't make any substantial changes exactly but does update the most quickly-archaic parts of older Node code. --- general-util.js | 48 ++++++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 26 deletions(-) (limited to 'general-util.js') diff --git a/general-util.js b/general-util.js index aba1541..bb0574a 100644 --- a/general-util.js +++ b/general-util.js @@ -1,13 +1,11 @@ -const { spawn } = require('child_process') -const { promisify } = require('util') -const fetch = require('node-fetch') -const fs = require('fs') -const npmCommandExists = require('command-exists') -const url = require('url') +import {spawn} from 'node:child_process' +import {readFile} from 'node:fs/promises' +import {fileURLToPath, URL} from 'node:url' -const readFile = promisify(fs.readFile) +import npmCommandExists from 'command-exists' +import fetch from 'node-fetch' -module.exports.promisifyProcess = function(proc, showLogging = true) { +export function promisifyProcess(proc, showLogging = true) { // Takes a process (from the child_process module) and returns a promise // that resolves when the process exits (or rejects, if the exit code is // non-zero). @@ -28,7 +26,7 @@ module.exports.promisifyProcess = function(proc, showLogging = true) { }) } -module.exports.commandExists = async function(command) { +export async function commandExists(command) { // When the command-exists module sees that a given command doesn't exist, it // throws an error instead of returning false, which is not what we want. @@ -39,12 +37,12 @@ module.exports.commandExists = async function(command) { } } -module.exports.killProcess = async function(proc) { +export async function killProcess(proc) { // Windows is stupid and doesn't like it when we try to kill processes. // So instead we use taskkill! https://stackoverflow.com/a/28163919/4633828 - if (await module.exports.commandExists('taskkill')) { - await module.exports.promisifyProcess( + if (await commandExists('taskkill')) { + await promisifyProcess( spawn('taskkill', ['/pid', proc.pid, '/f', '/t']), false ) @@ -53,18 +51,18 @@ module.exports.killProcess = async function(proc) { } } -function downloadPlaylistFromURL(url) { +export function downloadPlaylistFromURL(url) { return fetch(url).then(res => res.text()) } -function downloadPlaylistFromLocalPath(path) { +export function downloadPlaylistFromLocalPath(path) { return readFile(path).then(buf => buf.toString()) } -module.exports.downloadPlaylistFromOptionValue = function(arg) { +export function downloadPlaylistFromOptionValue(arg) { let argURL try { - argURL = new url.URL(arg) + argURL = new URL(arg) } catch (err) { // Definitely not a URL. } @@ -73,14 +71,14 @@ module.exports.downloadPlaylistFromOptionValue = function(arg) { if (argURL.protocol === 'http:' || argURL.protocol === 'https:') { return downloadPlaylistFromURL(arg) } else if (argURL.protocol === 'file:') { - return downloadPlaylistFromLocalPath(url.fileURLToPath(argURL)) + return downloadPlaylistFromLocalPath(fileURLToPath(argURL)) } } else { return downloadPlaylistFromLocalPath(arg) } } -module.exports.shuffleArray = function(array) { +export function shuffleArray(array) { // Shuffles the items in an array. Returns a new array (does not modify the // passed array). Super-interesting post on how this algorithm works: // https://bost.ocks.org/mike/shuffle/ @@ -103,7 +101,7 @@ module.exports.shuffleArray = function(array) { return workingArray } -module.exports.throttlePromise = function(maximumAtOneTime = 10) { +export function throttlePromise(maximumAtOneTime = 10) { // Returns a function that takes a callback to create a promise and either // runs it now, if there is an available slot, or enqueues it to be run // later, if there is not. @@ -139,7 +137,7 @@ module.exports.throttlePromise = function(maximumAtOneTime = 10) { return enqueue } -module.exports.getSecFromTimestamp = function(timestamp) { +export function getSecFromTimestamp(timestamp) { const parts = timestamp.split(':').map(n => parseInt(n)) switch (parts.length) { case 3: return parts[0] * 3600 + parts[1] * 60 + parts[2] @@ -149,7 +147,7 @@ module.exports.getSecFromTimestamp = function(timestamp) { } } -module.exports.getTimeStringsFromSec = function(curSecTotal, lenSecTotal, fraction = false) { +export function getTimeStringsFromSec(curSecTotal, lenSecTotal, fraction = false) { const percentVal = (100 / lenSecTotal) * curSecTotal const percentDone = ( (Math.trunc(percentVal * 100) / 100).toFixed(2) + '%' @@ -207,16 +205,16 @@ module.exports.getTimeStringsFromSec = function(curSecTotal, lenSecTotal, fracti return {percentDone, timeDone, timeLeft, duration, curSecTotal, lenSecTotal} } -module.exports.getTimeStrings = function({curHour, curMin, curSec, lenHour, lenMin, lenSec}) { +export function getTimeStrings({curHour, curMin, curSec, lenHour, lenMin, lenSec}) { // Multiplication casts to numbers; addition prioritizes strings. // Thanks, JavaScript! const curSecTotal = (3600 * curHour) + (60 * curMin) + (1 * curSec) const lenSecTotal = (3600 * lenHour) + (60 * lenMin) + (1 * lenSec) - return module.exports.getTimeStringsFromSec(curSecTotal, lenSecTotal) + return getTimeStringsFromSec(curSecTotal, lenSecTotal) } -const parseOptions = async function(options, optionDescriptorMap) { +export async function parseOptions(options, optionDescriptorMap) { // This function is sorely lacking in comments, but the basic usage is // as such: // @@ -331,5 +329,3 @@ const parseOptions = async function(options, optionDescriptorMap) { } parseOptions.handleDashless = Symbol() - -module.exports.parseOptions = parseOptions -- cgit 1.3.0-6-gf8a5