UNPKG

node-pkware

Version:

node.js implementation of StormLib's pkware compressor/de-compressor

67 lines 1.95 kB
import * as fs from 'node:fs'; import * as path from 'node:path'; import * as process from 'node:process'; import { fileURLToPath } from 'node:url'; function isDecimalString(input) { return /^\d+$/.test(input); } function isFullHexString(input) { return /^\s*0x[\da-f]+\s*$/.test(input); } export function parseNumberString(n, defaultValue = 0) { if (n === undefined) { return defaultValue; } if (isDecimalString(n)) { return Number.parseInt(n, 10); } if (isFullHexString(n)) { return Number.parseInt(n.replace(/^0x/, ''), 16); } return defaultValue; } export function pathToRepoRoot() { const filename = fileURLToPath(import.meta.url); const dirname = path.dirname(filename); return path.resolve(dirname, '../../'); } function pathToPackageJson() { return path.resolve(pathToRepoRoot(), './package.json'); } export async function getPackageVersion() { try { const rawIn = await fs.promises.readFile(pathToPackageJson(), 'utf8'); const { version } = JSON.parse(rawIn); return version; } catch { return 'unknown'; } } export async function fileExists(filename) { try { await fs.promises.access(filename, fs.constants.R_OK); return true; } catch { return false; } } export async function getInputStream(filename) { if (filename === undefined) { process.stdin.resume(); return process.stdin; } if (await fileExists(filename)) { return fs.createReadStream(filename); } throw new Error('input file does not exist'); } // eslint-disable-next-line @typescript-eslint/require-await -- this is async to be consistent with the getInputStream function export async function getOutputStream(filename) { if (filename === undefined) { return process.stdout; } return fs.createWriteStream(filename); } //# sourceMappingURL=helpers.js.map