« get me outta code hell

node-utils.js « util « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util/node-utils.js
blob: a46d614177e6f8083d5266dbf5cd3f0d768f7ac8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Utility functions which are only relevant to particular Node.js constructs.

import { fileURLToPath } from 'url';

// Very cool function origin8ting in... http-music pro8a8ly!
// Sorry if we happen to 8e violating past-us's copyright, lmao.
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).
    //
    // Ayy look, no alpha8etical second letter! Couldn't tell this was written
    // like three years ago 8efore I was me. 8888)

    return new Promise((resolve, reject) => {
        if (showLogging) {
            proc.stdout.pipe(process.stdout);
            proc.stderr.pipe(process.stderr);
        }

        proc.on('exit', code => {
            if (code === 0) {
                resolve();
            } else {
                reject(code);
            }
        })
    })
}

// Handy-dandy utility function for detecting whether the passed URL is the
// running JavaScript file. This takes `import.meta.url` from ES6 modules, which
// is great 'cuz (module === require.main) doesn't work without CommonJS
// modules.
export function isMain(importMetaURL) {
    return (process.argv[1] === fileURLToPath(importMetaURL));
}