UNPKG

maxcso

Version:

💿 maxcso binaries and wrapper for Node.js.

100 lines • 4.64 kB
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 { promisify } from 'node:util'; import fs from 'node:fs'; import which from 'which'; import * as child_process from 'node:child_process'; export var MaxcsoBinaryPreference; (function (MaxcsoBinaryPreference) { MaxcsoBinaryPreference[MaxcsoBinaryPreference["PREFER_BUNDLED_BINARY"] = 1] = "PREFER_BUNDLED_BINARY"; MaxcsoBinaryPreference[MaxcsoBinaryPreference["PREFER_PATH_BINARY"] = 2] = "PREFER_PATH_BINARY"; })(MaxcsoBinaryPreference || (MaxcsoBinaryPreference = {})); /** * Code to find and interact with the `maxcso` binary. */ export default class MaxcsoBin { static getBinPath(binaryPreference) { return __awaiter(this, void 0, void 0, function* () { if (this.MAXCSO_BIN) { return this.MAXCSO_BIN; } if ((binaryPreference !== null && binaryPreference !== void 0 ? binaryPreference : MaxcsoBinaryPreference.PREFER_BUNDLED_BINARY) === MaxcsoBinaryPreference.PREFER_BUNDLED_BINARY) { const pathBundled = yield this.getBinPathBundled(); this.MAXCSO_BIN = pathBundled !== null && pathBundled !== void 0 ? pathBundled : (yield this.getBinPathExisting()); } else { const pathExisting = yield this.getBinPathExisting(); this.MAXCSO_BIN = pathExisting !== null && pathExisting !== void 0 ? pathExisting : (yield this.getBinPathBundled()); } return this.MAXCSO_BIN; }); } static getBinPathBundled() { return __awaiter(this, void 0, void 0, function* () { // try { const maxcso = yield import(`@emmercm/maxcso-${process.platform}-${process.arch}`); const prebuilt = maxcso.default; if (yield promisify(fs.exists)(prebuilt)) { return prebuilt; } // } catch { /* ignored */ } return undefined; }); } static getBinPathExisting() { return __awaiter(this, void 0, void 0, function* () { const resolved = yield which(process.platform === 'win32' ? 'maxcso.exe' : 'maxcso', { nothrow: true }); if (resolved) { return resolved; } return undefined; }); } /** * Run maxcso with some arguments. */ static run(arguments_, options) { return __awaiter(this, void 0, void 0, function* () { const maxcsoBin = yield this.getBinPath(options === null || options === void 0 ? void 0 : options.binaryPreference); if (!maxcsoBin) { throw new Error('maxcso not found'); } return new Promise((resolve, reject) => { const proc = child_process.spawn(maxcsoBin, arguments_, { windowsHide: true }); 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); }); proc.on('close', (code) => { const output = Buffer.concat(chunks).toString().trim(); if (code !== null && code !== 0) { return reject(output); } return resolve(output); }); proc.on('error', () => { const output = Buffer.concat(chunks).toString().trim(); reject(output); }); }); }); } } //# sourceMappingURL=maxcsoBin.js.map