run-script-cli
Version:
<p align="center"> <a href="https://www.npmjs.com/package/run-script-cli" target="_blank" rel="noopener noreferrer"> <img src="https://github.com/hunghg255/run-script-cli/blob/main/assets/icon.png?raw=true" alt="logo" width='100'/></a> </p>
169 lines (160 loc) • 5.18 kB
JavaScript
;
const process = require('node:process');
const index = require('./shared/run-script-cli.23752879.cjs');
const fs = require('node:fs');
const path = require('node:path');
const os = require('node:os');
require('node:util');
require('node:child_process');
require('node:buffer');
require('child_process');
require('path');
require('fs');
require('node:url');
require('node:timers/promises');
require('stream');
require('tty');
require('node:readline');
require('node:tty');
require('events');
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
const process__default = /*#__PURE__*/_interopDefaultCompat(process);
const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
const os__default = /*#__PURE__*/_interopDefaultCompat(os);
function getPackageJSON(cwdParams) {
const cwd = cwdParams ?? process__default.cwd();
const path$1 = path.resolve(cwd, "package.json");
if (fs__default.existsSync(path$1)) {
try {
const raw = fs__default.readFileSync(path$1, "utf8");
const data = JSON.parse(raw);
return data;
} catch {
console.warn("Failed to parse package.json");
process__default.exit(1);
}
}
}
const CLI_TEMP_DIR = path.join(os__default.tmpdir(), "run-script-cli");
const storagePath = path.resolve(CLI_TEMP_DIR, "_storage.json");
let storage;
let counter = 0;
async function openTemp() {
if (!fs.existsSync(CLI_TEMP_DIR)) {
await fs.promises.mkdir(CLI_TEMP_DIR, { recursive: true });
}
const competitivePath = path.join(CLI_TEMP_DIR, `.${process__default.pid}.${counter}`);
counter++;
return fs.promises.open(competitivePath, "wx").then((fd) => ({
fd,
path: competitivePath,
cleanup() {
fd.close().then(() => {
if (fs.existsSync(competitivePath)) {
fs.promises.unlink(competitivePath);
}
});
}
})).catch((error) => {
return error && error.code === "EEXIST" ? openTemp() : void 0;
});
}
async function writeFileSafe(path$1, data = "") {
const temp = await openTemp();
if (temp) {
fs.promises.writeFile(temp.path, data).then(() => {
const directory = path.dirname(path$1);
if (!fs.existsSync(directory)) {
fs.promises.mkdir(directory, { recursive: true });
}
return fs.promises.rename(temp.path, path$1).then(() => true).catch(() => false);
}).catch(() => false).finally(temp.cleanup);
}
return false;
}
async function load(fn) {
if (!storage) {
storage = fs.existsSync(storagePath) ? JSON.parse(await fs.promises.readFile(storagePath, "utf8") || "{}") || {} : {};
}
if (fn && await fn(storage)) {
await dump();
}
return storage;
}
async function dump() {
if (storage) {
await writeFileSafe(storagePath, JSON.stringify(storage));
}
}
function limitText(text, maxWidth) {
if (text.length <= maxWidth) {
return text;
}
return `${text.slice(0, maxWidth)}${index.$.dim("\u2026")}`;
}
const niCli = async (cwd = process__default.cwd(), argv = process__default.argv) => {
try {
const storage = await load();
const pkg = getPackageJSON(cwd);
const scripts = pkg.scripts || {};
const scriptsInfo = pkg["scripts-info"] || {};
let scriptValue = "";
const names = Object.entries(scripts);
if (names.length === 0) {
return process__default.exit(0);
}
const raw = names.filter((i) => !i[0].startsWith("?")).map(([key, cmd]) => ({
key,
cmd,
description: scriptsInfo[key] || scripts[`?${key}`] || cmd
}));
const agent = await index.detectAgent(cwd);
if (!agent?.name) {
return process__default.exit(0);
}
if (argv?.length > 2) {
scriptValue = argv.slice(2).join(" ");
} else {
const initialValue = storage.lastRunCommand;
scriptValue = await index.te({
message: index.$.bgCyan(" Run script "),
options: raw.map((scriptItem) => ({
label: `${index.$.green(scriptItem.key)}: ${index.$.dim(limitText(scriptItem.description, 50))}`,
value: scriptItem.key
})),
initialValue
});
if (index.H(scriptValue)) {
index.ne("Run script cancelled");
return process__default.exit(0);
}
}
if (storage.lastRunCommand !== scriptValue) {
storage.lastRunCommand = scriptValue;
dump();
}
if (agent.name === "bun") {
index.oe(index.$.bold(index.$.green(`bun run ${scriptValue}
`)));
await index.execaCommand(`bun run ${scriptValue}`, { stdio: "inherit", cwd });
}
if (agent.name === "pnpm") {
index.oe(index.$.bold(index.$.green(`pnpm ${scriptValue}
`)));
await index.execaCommand(`pnpm ${scriptValue}`, { stdio: "inherit", cwd });
}
if (agent.name === "npm") {
index.oe(index.$.bold(index.$.green(`npm run ${scriptValue}
`)));
await index.execaCommand(`npm run ${scriptValue}`, { stdio: "inherit", cwd });
}
if (agent.name === "yarn") {
index.oe(index.$.bold(index.$.green(`yarn ${scriptValue}
`)));
await index.execaCommand(`yarn ${scriptValue}`, { stdio: "inherit", cwd });
}
} catch {
}
};
niCli();
exports.niCli = niCli;