chdman
Version:
💿 chdman binaries and wrapper for Node.js.
128 lines • 6.03 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import which from 'which';
import util from 'node:util';
import fs from 'node:fs';
import * as child_process from 'node:child_process';
export var ChdmanBinaryPreference;
(function (ChdmanBinaryPreference) {
ChdmanBinaryPreference[ChdmanBinaryPreference["PREFER_BUNDLED_BINARY"] = 1] = "PREFER_BUNDLED_BINARY";
ChdmanBinaryPreference[ChdmanBinaryPreference["PREFER_PATH_BINARY"] = 2] = "PREFER_PATH_BINARY";
})(ChdmanBinaryPreference || (ChdmanBinaryPreference = {}));
/**
* Code to find and interact with the `chdman` binary.
*/
export default class ChdmanBin {
static getBinPath(binaryPreference) {
return __awaiter(this, void 0, void 0, function* () {
if (this.CHDMAN_BIN) {
return this.CHDMAN_BIN;
}
if ((binaryPreference !== null && binaryPreference !== void 0 ? binaryPreference : ChdmanBinaryPreference.PREFER_BUNDLED_BINARY)
=== ChdmanBinaryPreference.PREFER_BUNDLED_BINARY) {
const pathBundled = yield this.getBinPathBundled();
this.CHDMAN_BIN = pathBundled !== null && pathBundled !== void 0 ? pathBundled : (yield this.getBinPathExisting());
}
else {
const pathExisting = yield this.getBinPathExisting();
this.CHDMAN_BIN = pathExisting !== null && pathExisting !== void 0 ? pathExisting : (yield this.getBinPathBundled());
}
return this.CHDMAN_BIN;
});
}
static getBinPathBundled() {
return __awaiter(this, void 0, void 0, function* () {
try {
const chdman = yield import(`@emmercm/chdman-${process.platform}-${process.arch}`);
const prebuilt = chdman.default;
try {
yield util.promisify(fs.stat)(prebuilt);
return prebuilt;
}
catch ( /* ignored */_a) { /* ignored */ }
}
catch ( /* ignored */_b) { /* ignored */ }
return undefined;
});
}
static getBinPathExisting() {
return __awaiter(this, void 0, void 0, function* () {
const resolved = yield which(process.platform === 'win32' ? 'chdman.exe' : 'chdman', { nothrow: true });
if (resolved) {
return resolved;
}
return undefined;
});
}
/**
* Run chdman with some arguments.
*/
static run(arguments_, options) {
return __awaiter(this, void 0, void 0, function* () {
const chdmanBin = yield ChdmanBin.getBinPath(options === null || options === void 0 ? void 0 : options.binaryPreference);
if (!chdmanBin) {
throw new Error('chdman not found');
}
const inputIndex = arguments_.indexOf('--input');
if (inputIndex !== -1 && (inputIndex + 1) < arguments_.length) {
const inputPath = arguments_[inputIndex + 1];
try {
yield util.promisify(fs.stat)(inputPath);
}
catch (_a) {
throw new Error(`input file doesn't exist: ${inputPath}`);
}
}
// if (process.platform === 'darwin'
// && !fs.existsSync(path.join('Library', 'Frameworks', 'SDL2.framework'))) {
// throw new Error('chdman requires the SDL2 framework to be installed on macOS');
// }
return new Promise((resolve, reject) => {
const proc = child_process.spawn(chdmanBin, arguments_, { windowsHide: true });
let killed = false;
const chunks = [];
proc.stdout.on('data', (chunk) => {
if (options === null || options === void 0 ? void 0 : options.logStd) {
console.log(chunk.toString());
}
chunks.push(chunk);
});
proc.stderr.on('data', (chunk) => {
if (options === null || options === void 0 ? void 0 : options.logStd) {
console.error(chunk.toString());
}
chunks.push(chunk);
if (chunk.toString().includes('nan% complete')) {
// chdman can hang forever on input files that aren't valid (i.e. too small)
proc.kill();
killed = true;
}
if (/([2-9]|\d\d)\d\d\.\d+%/.test(chunk.toString())) {
// chdman can waste a lot of time extracting bad files
proc.kill();
killed = true;
}
});
proc.on('close', (code) => {
const output = Buffer.concat(chunks).toString().trim();
if ((code !== null && code !== 0) || killed) {
return reject(output);
}
return resolve(output);
});
proc.on('error', () => {
const output = Buffer.concat(chunks).toString().trim();
reject(output);
});
});
});
}
}
//# sourceMappingURL=chdmanBin.js.map