bun-safe
Version:
Run scripts using Bun.js and perhaps install Bun first if necessary.
53 lines (50 loc) • 1.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.bunSafe = bunSafe;
var child_process = _interopRequireWildcard(require("child_process"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
/// <reference types="bun-types" />
const has = () => {
try {
child_process.execSync('bun --version', {
stdio: 'ignore'
});
return true;
} catch (e) {
return false;
}
};
const install = () => {
try {
const script = child_process.execSync('curl https://bun.sh/install', {
encoding: 'utf8'
});
child_process.execSync(script, {
stdio: 'inherit'
});
} catch (e) {
console.error(`\n> failed to install bun.js:\n${e.toString()}`);
process.exit(1);
}
};
function bunSafe(args, options) {
const script = Array.isArray(args) ? args.join(' ') : args;
if (!script?.length) {
throw new Error('Received empty arguments');
}
if (!has()) install();
const defaults = {
encoding: 'utf8',
stdio: options?.encoding ? 'ignore' : 'inherit'
};
// @ts-ignore
options = {
...defaults,
...options
};
return child_process.execSync(`bun ${script}`, options);
}
//# sourceMappingURL=index.cjs.map