@simon_he/pi
Version:
Project-aware CLI that detects npm, pnpm, yarn, bun, Go, Rust and Python projects, then routes installs, scripts, builds and workspace commands automatically.
215 lines (213 loc) • 10.4 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_pui = require('./pui-Bnd6wAam.cjs');
let node_path = require("node:path");
node_path = require_pui.__toESM(node_path);
let node_process = require("node:process");
node_process = require_pui.__toESM(node_process);
let fast_glob = require("fast-glob");
fast_glob = require_pui.__toESM(fast_glob);
let lazy_js_utils = require("lazy-js-utils");
let lazy_js_utils_node = require("lazy-js-utils/node");
let picocolors = require("picocolors");
picocolors = require_pui.__toESM(picocolors);
//#region src/index.ts
let rootPath = node_process.default.cwd();
const runMap = {
pi: require_pui.pi,
pix: require_pui.pix,
pa: require_pui.pa,
pu: require_pui.pu,
pui: require_pui.pui,
pil: require_pui.pil,
pci: require_pui.pci,
prun: require_pui.prun,
pinit: require_pui.pinit,
pfind: require_pui.pfind,
pio: require_pui.pio
};
const isZh = node_process.default.env.PI_Lang === "zh";
const pkgToolFlagCommands = new Set([
"pi",
"pil",
"pci"
]);
const supportedPkgTools = new Set(require_pui.getSupportedPkgToolNames());
function getExecName(argv1 = node_process.default.argv[1] ?? "") {
return node_path.default.basename(argv1).replace(/(?:\.(?:mjs|cjs|js|cmd|ps1))+$/i, "");
}
function parsePkgToolFlags(argv) {
const hasInspectFlag = argv.includes("--show-tool") || argv.includes("--list-tools");
let chooseTool = false;
let forgetTool = false;
let listTools = false;
let showTool = false;
let showToolJson = false;
let preferredTool = "";
let invalidPreferredTool = "";
const normalizedArgv = [];
for (let i = 0; i < argv.length; i++) {
const arg = argv[i];
if (arg === "--forget-tool") {
forgetTool = true;
continue;
}
if (arg === "--show-tool") {
showTool = true;
continue;
}
if (arg === "--list-tools") {
listTools = true;
continue;
}
if (arg === "--json" && hasInspectFlag) {
showToolJson = true;
continue;
}
if (arg === "--choose-tool") {
chooseTool = true;
const next = argv[i + 1];
if (next && !next.startsWith("-") && supportedPkgTools.has(next)) {
preferredTool = next;
i++;
}
continue;
}
if (arg.startsWith("--choose-tool=")) {
chooseTool = true;
const value = arg.slice(14);
if (supportedPkgTools.has(value)) preferredTool = value;
else invalidPreferredTool = value;
continue;
}
normalizedArgv.push(arg);
}
return {
chooseTool,
forgetTool,
invalidPreferredTool,
listTools,
normalizedArgv,
preferredTool,
showTool,
showToolJson
};
}
async function setup() {
const exec = getExecName(node_process.default.argv[1]);
const argv = node_process.default.argv.slice(2);
await require_pui.help(argv);
if (exec === "prun" && argv[0] === "--init") {
require_pui.printPrunInit(argv.slice(1));
return;
}
if ((exec === "prun" || exec === "pfind") && argv[0] === "--doctor") {
require_pui.printPrunDoctor();
return;
}
const supportsPkgToolFlags = pkgToolFlagCommands.has(exec);
const { chooseTool, forgetTool, invalidPreferredTool, listTools, normalizedArgv, preferredTool, showTool, showToolJson } = supportsPkgToolFlags ? parsePkgToolFlags(argv) : {
chooseTool: false,
forgetTool: false,
invalidPreferredTool: "",
listTools: false,
normalizedArgv: argv,
preferredTool: "",
showTool: false,
showToolJson: false
};
if (invalidPreferredTool) {
console.error(picocolors.default.red(isZh ? `不支持直接指定 ${invalidPreferredTool},可选值为: ${require_pui.getSupportedPkgToolNames().join(", ")}` : `Unsupported tool "${invalidPreferredTool}". Valid values: ${require_pui.getSupportedPkgToolNames().join(", ")}`));
node_process.default.exitCode = 1;
return;
}
if (chooseTool) node_process.default.env.PI_FORCE_PICK_TOOL = "1";
else delete node_process.default.env.PI_FORCE_PICK_TOOL;
if (forgetTool) node_process.default.env.PI_FORGET_PICK_TOOL = "1";
else delete node_process.default.env.PI_FORGET_PICK_TOOL;
if (preferredTool) node_process.default.env.PI_PREFERRED_TOOL = preferredTool;
else delete node_process.default.env.PI_PREFERRED_TOOL;
let params = (0, lazy_js_utils.spaceFormat)(normalizedArgv.join(" ")).trim();
const hasPackage = await (0, lazy_js_utils_node.hasPkg)(rootPath);
if (supportsPkgToolFlags && (chooseTool || forgetTool || showTool || listTools)) {
if (!hasPackage) {
console.log(picocolors.default.yellow(isZh ? "当前命令仅在 Node 项目的包管理场景下可用。" : "This option is only available for package-manager selection in Node projects."));
return;
}
if (showTool || listTools) {
if (forgetTool && !chooseTool) {
const removed = await require_pui.forgetPkgToolPreference();
console.log(removed ? picocolors.default.green(isZh ? "已清除当前 workspace 保存的包管理器选择。" : "Cleared the saved package-manager choice for this workspace.") : picocolors.default.yellow(isZh ? "当前 workspace 没有保存的包管理器选择。" : "No saved package-manager choice was found for this workspace."));
}
if (chooseTool) await require_pui.resolvePkgTool();
const status = await require_pui.getPkgToolStatus();
if (listTools) require_pui.printPkgToolCandidates(status, { json: showToolJson });
else require_pui.printPkgToolStatus(status, { json: showToolJson });
return;
}
if (normalizedArgv.length === 0) {
if (forgetTool && !chooseTool) {
const removed = await require_pui.forgetPkgToolPreference();
console.log(removed ? picocolors.default.green(isZh ? "已清除当前 workspace 保存的包管理器选择。" : "Cleared the saved package-manager choice for this workspace.") : picocolors.default.yellow(isZh ? "当前 workspace 没有保存的包管理器选择。" : "No saved package-manager choice was found for this workspace."));
return;
}
await require_pui.resolvePkgTool();
return;
}
}
if (!hasPackage) {
if (await (0, lazy_js_utils_node.isGo)(rootPath)) {
if (exec === "pi") {
const loading_status = await require_pui.loading(`${isZh ? "正在为您安装" : "Installing"} ${params} ...\n`);
const { status } = params ? await (0, lazy_js_utils_node.useNodeWorker)(`go get ${params}`) : await (0, lazy_js_utils_node.useNodeWorker)("go mod tidy");
if (status === 0) loading_status.succeed(picocolors.default.green(isZh ? "安装成功! 😊" : "Installed successfully! 😊"));
else loading_status.fail(picocolors.default.red(isZh ? "安装失败 😭" : "Failed to install 😭"));
} else if (exec === "pui") {
const loading_status = await require_pui.loading(`${isZh ? "正在为您卸载" : "Uninstalling"} ${params} ...\n`);
const { status } = await (0, lazy_js_utils_node.useNodeWorker)(`go clean ${params}`);
if (status === 0) loading_status.succeed(picocolors.default.green(isZh ? "卸载成功! 😊" : "Uninstalled successfully! 😊"));
else loading_status.fail(picocolors.default.red(isZh ? "卸载失败 😭" : "Failed to uninstall 😭"));
} else if (exec === "prun") {
const target = (await (0, fast_glob.default)(params ? params.endsWith(".go") ? [`**/${params}`] : [`**/${params}.go`, `**/${params}/main.go`] : "main.go"))[0];
if (target) return await (0, lazy_js_utils_node.jsShell)(`go run ${target}`, "inherit");
const { ccommand } = require_pui.getCcommand();
return ccommand(params);
} else if (exec === "pinit") await (0, lazy_js_utils_node.jsShell)(`go mod init ${params}`, "inherit");
else if (exec === "pbuild") await (0, lazy_js_utils_node.jsShell)(`go build ${params}`, "inherit");
else console.log(picocolors.default.red(isZh ? "当前指令还不支持" : "The commands is not supported"));
node_process.default.exit();
}
let projectPath = "";
if (params && !await (0, lazy_js_utils_node.isRust)()) {
projectPath = params.split(" ")[0];
rootPath = node_path.default.resolve(rootPath, projectPath);
params = params.replace(projectPath, "").trim();
}
if (await (0, lazy_js_utils_node.isRust)(rootPath)) {
if (exec === "pi") {
const loading_status = await require_pui.loading(`${isZh ? "正在为您安装" : "Installing"} ${params} ...\n`);
const { status } = await (0, lazy_js_utils_node.useNodeWorker)(`cargo install ${params}${projectPath ? `--manifest-path=./${projectPath}/Cargo.toml` : ""}`);
if (status === 0) loading_status.succeed(picocolors.default.green(isZh ? "安装成功! 😊" : "Installed successfully! 😊"));
else loading_status.fail(picocolors.default.red(isZh ? "安装失败 😭" : "Failed to install 😭"));
} else if (exec === "pui") {
const loading_status = await require_pui.loading(`${isZh ? "正在为您卸载" : "Uninstalling"} ${params} ...\n`);
const { status } = await (0, lazy_js_utils_node.useNodeWorker)(`cargo uninstall ${params}${projectPath ? `--manifest-path=./${projectPath}/Cargo.toml` : ""}`);
if (status === 0) loading_status.succeed(picocolors.default.green(isZh ? "卸载成功! 😊" : "Uninstalled successfully! 😊"));
else loading_status.fail(picocolors.default.red(isZh ? "卸载失败 😭" : "Failed to uninstall 😭"));
} else if (exec === "prun") await (0, lazy_js_utils_node.jsShell)(`cargo run ${params}${projectPath ? `--manifest-path=./${projectPath}/Cargo.toml` : ""}`, "inherit");
else if (exec === "pinit") await (0, lazy_js_utils_node.jsShell)(`cargo init ${params}${projectPath ? `--manifest-path=./${projectPath}/Cargo.toml` : ""}`, "inherit");
else if (exec === "pbuild") await (0, lazy_js_utils_node.jsShell)(`cargo build ${params}${projectPath ? `--manifest-path=./${projectPath}/Cargo.toml` : ""}`, "inherit");
else console.log(picocolors.default.red(isZh ? "当前指令还不支持" : "The commands is not supported"));
node_process.default.exit();
}
}
const handler = runMap[exec];
if (!handler) {
if (exec === "pbuild") console.log(picocolors.default.yellow(isZh ? "pbuild 仅支持 Go/Rust 项目(go build / cargo build)。" : "pbuild is only supported for Go/Rust projects (go build / cargo build)."));
else console.log(picocolors.default.yellow(isZh ? "命令不存在, 请执行 pi -h 查看帮助" : "The command does not exist, please execute pi -h to view the help"));
return;
}
const pkg = normalizedArgv.filter((v) => !v.startsWith("-")).join(" ");
await handler(params, pkg);
}
//#endregion
exports.setup = setup;