dolphin-tool
Version:
🐬 dolphin-tool binaries and wrapper for Node.js.
101 lines • 4.83 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 DolphinToolBinaryPreference;
(function (DolphinToolBinaryPreference) {
DolphinToolBinaryPreference[DolphinToolBinaryPreference["PREFER_BUNDLED_BINARY"] = 1] = "PREFER_BUNDLED_BINARY";
DolphinToolBinaryPreference[DolphinToolBinaryPreference["PREFER_PATH_BINARY"] = 2] = "PREFER_PATH_BINARY";
})(DolphinToolBinaryPreference || (DolphinToolBinaryPreference = {}));
/**
* Code to find and interact with the `dolphin-tool` binary.
*/
export default class DolphinToolBin {
static getBinPath(binaryPreference) {
return __awaiter(this, void 0, void 0, function* () {
if (this.DOLPHIN_TOOL_BIN) {
return this.DOLPHIN_TOOL_BIN;
}
if ((binaryPreference !== null && binaryPreference !== void 0 ? binaryPreference : DolphinToolBinaryPreference.PREFER_BUNDLED_BINARY)
=== DolphinToolBinaryPreference.PREFER_BUNDLED_BINARY) {
const pathBundled = yield this.getBinPathBundled();
this.DOLPHIN_TOOL_BIN = pathBundled !== null && pathBundled !== void 0 ? pathBundled : (yield this.getBinPathExisting());
}
else {
const pathExisting = yield this.getBinPathExisting();
this.DOLPHIN_TOOL_BIN = pathExisting !== null && pathExisting !== void 0 ? pathExisting : (yield this.getBinPathBundled());
}
return this.DOLPHIN_TOOL_BIN;
});
}
static getBinPathBundled() {
return __awaiter(this, void 0, void 0, function* () {
try {
const dolphinTool = yield import(`@emmercm/dolphin-tool-${process.platform}-${process.arch}`);
const prebuilt = dolphinTool.default;
if (yield util.promisify(fs.exists)(prebuilt)) {
return prebuilt;
}
}
catch ( /* ignored */_a) { /* ignored */ }
return undefined;
});
}
static getBinPathExisting() {
return __awaiter(this, void 0, void 0, function* () {
const resolved = yield which(process.platform === 'win32' ? 'DolphinTool.exe' : 'dolphin-tool', { nothrow: true });
if (resolved) {
return resolved;
}
return undefined;
});
}
/**
* Run dolphin-tool with some arguments.
*/
static run(arguments_, options) {
return __awaiter(this, void 0, void 0, function* () {
const dolphinToolBin = yield this.getBinPath(options === null || options === void 0 ? void 0 : options.binaryPreference);
if (!dolphinToolBin) {
throw new Error('dolphin-tool not found');
}
return new Promise((resolve, reject) => {
const proc = child_process.spawn(dolphinToolBin, 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=dolphinToolBin.js.map