@simon_he/pi 0.2.10 → 0.2.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -1
- package/dist/index.cjs +494 -36
- package/dist/index.mjs +499 -42
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -7,16 +7,17 @@ import { getPkg, getPkgTool, hasPkg, isGo, isRust, jsShell, useNodeWorker } from
|
|
|
7
7
|
import color from "picocolors";
|
|
8
8
|
import readline from "node:readline";
|
|
9
9
|
import { log } from "node:console";
|
|
10
|
-
import fs from "node:fs";
|
|
10
|
+
import fs from "node:fs/promises";
|
|
11
11
|
import os from "node:os";
|
|
12
|
+
import fs$1 from "node:fs";
|
|
12
13
|
import { fileURLToPath } from "node:url";
|
|
13
14
|
|
|
14
15
|
//#region package.json
|
|
15
|
-
var version = "0.2.
|
|
16
|
+
var version = "0.2.11";
|
|
16
17
|
|
|
17
18
|
//#endregion
|
|
18
19
|
//#region src/tty.ts
|
|
19
|
-
const isZh$
|
|
20
|
+
const isZh$7 = process.env.PI_Lang === "zh";
|
|
20
21
|
function isInteractive() {
|
|
21
22
|
return Boolean(process.stdin.isTTY && process.stdout.isTTY);
|
|
22
23
|
}
|
|
@@ -198,7 +199,7 @@ async function runSelect(options, config) {
|
|
|
198
199
|
if (anchor) {
|
|
199
200
|
const prompt = "> ";
|
|
200
201
|
const header = `? ${config.placeholder}`;
|
|
201
|
-
const hintLine = `${config.mode === "multiple" ? isZh$
|
|
202
|
+
const hintLine = `${config.mode === "multiple" ? isZh$7 ? "↑/↓ 选择,空格标记,Tab 补全,/ 搜索,回车确认,Esc 取消" : "Use ↑/↓ to move, Space to toggle, Tab to complete, / to search, Enter to confirm, Esc to cancel" : isZh$7 ? "↑/↓ 选择,Tab 补全,/ 搜索,回车确认,Esc 取消" : "Use ↑/↓ to move, Tab to complete, / to search, Enter to confirm, Esc to cancel"} (1/${options.length})`;
|
|
202
203
|
const headerRows = rowCount(header, columns);
|
|
203
204
|
const inputRows = rowCount(`${prompt}`, columns);
|
|
204
205
|
const hintRows = rowCount(hintLine, columns);
|
|
@@ -236,10 +237,10 @@ async function runSelect(options, config) {
|
|
|
236
237
|
const hintLine = (() => {
|
|
237
238
|
const position = ` (${Math.min(cursor + 1, filtered.length)}/${filtered.length})`;
|
|
238
239
|
if (config.mode === "multiple") {
|
|
239
|
-
if (isZh$
|
|
240
|
+
if (isZh$7) return color.dim("↑/↓ 选择,") + color.bold(color.cyan("空格")) + color.dim(" 标记,Tab 补全,/ 搜索,回车确认,Esc 取消") + color.dim(position);
|
|
240
241
|
return color.dim("Use ↑/↓ to move, ") + color.bold(color.cyan("Space")) + color.dim(" to toggle, Tab to complete, / to search, Enter to confirm, Esc to cancel") + color.dim(position);
|
|
241
242
|
}
|
|
242
|
-
const hint = isZh$
|
|
243
|
+
const hint = isZh$7 ? "↑/↓ 选择,Tab 补全,/ 搜索,回车确认,Esc 取消" : "Use ↑/↓ to move, Tab to complete, / to search, Enter to confirm, Esc to cancel";
|
|
243
244
|
return color.dim(`${hint}${position}`);
|
|
244
245
|
})();
|
|
245
246
|
const headerRows = rowCount(header, columns);
|
|
@@ -253,7 +254,7 @@ async function runSelect(options, config) {
|
|
|
253
254
|
const maxVisibleRaw = needsScroll ? Math.max(minVisibleTarget, optionAreaRows - ellipsisReserve) : filtered.length;
|
|
254
255
|
const maxVisible = Math.max(1, Math.min(maxVisibleRaw, optionAreaRows, filtered.length));
|
|
255
256
|
const lines = [header, inputLine];
|
|
256
|
-
if (filtered.length === 0) lines.push(color.dim(isZh$
|
|
257
|
+
if (filtered.length === 0) lines.push(color.dim(isZh$7 ? "没有匹配项" : "No matches"));
|
|
257
258
|
else {
|
|
258
259
|
const window = getVisibleWindow(filtered.length, cursor, maxVisible);
|
|
259
260
|
const visible = filtered.slice(window.start, window.end);
|
|
@@ -454,11 +455,11 @@ function renderBox(lines, options = {}) {
|
|
|
454
455
|
|
|
455
456
|
//#endregion
|
|
456
457
|
//#region src/help.ts
|
|
457
|
-
const isZh$
|
|
458
|
+
const isZh$6 = process.env.PI_Lang === "zh";
|
|
458
459
|
async function help(argv) {
|
|
459
460
|
const arg = argv[0];
|
|
460
461
|
if (arg === "-v" || arg === "--version") {
|
|
461
|
-
const message = isZh$
|
|
462
|
+
const message = isZh$6 ? [
|
|
462
463
|
`pi 版本: ${version}`,
|
|
463
464
|
"请为我的努力点一个行 🌟",
|
|
464
465
|
"谢谢 🤟"
|
|
@@ -480,6 +481,13 @@ async function help(argv) {
|
|
|
480
481
|
console.log(renderBox([
|
|
481
482
|
"PI Commands:",
|
|
482
483
|
"~ pi: install package",
|
|
484
|
+
"~ pi --choose-tool: choose package manager for this workspace",
|
|
485
|
+
"~ pi --choose-tool bun: choose the tool directly",
|
|
486
|
+
"~ pi --forget-tool: clear saved package manager choice",
|
|
487
|
+
"~ pi --show-tool: show current workspace package manager",
|
|
488
|
+
"~ pi --show-tool --json: show current tool as JSON",
|
|
489
|
+
"~ pi --list-tools: list detected package-manager candidates",
|
|
490
|
+
"~ pi --list-tools --json: list candidates as JSON",
|
|
483
491
|
"~ pix: npx package",
|
|
484
492
|
"~ pui: uninstall package",
|
|
485
493
|
"~ prun: run package script",
|
|
@@ -489,10 +497,24 @@ async function help(argv) {
|
|
|
489
497
|
"~ pa: agent alias",
|
|
490
498
|
"~ pu: package upgrade",
|
|
491
499
|
"~ pci: package clean install",
|
|
492
|
-
"~
|
|
500
|
+
"~ pci --choose-tool: re-pick tool before clean install",
|
|
501
|
+
"~ pci --choose-tool bun: choose the tool directly",
|
|
502
|
+
"~ pci --forget-tool: clear saved tool before clean install",
|
|
503
|
+
"~ pci --show-tool: show current tool before clean install",
|
|
504
|
+
"~ pci --show-tool --json: show current tool as JSON",
|
|
505
|
+
"~ pci --list-tools: list detected candidates",
|
|
506
|
+
"~ pci --list-tools --json: list candidates as JSON",
|
|
507
|
+
"~ pil: package latest install",
|
|
508
|
+
"~ pil --choose-tool: re-pick tool before latest install",
|
|
509
|
+
"~ pil --choose-tool bun: choose the tool directly",
|
|
510
|
+
"~ pil --forget-tool: clear saved tool before latest install",
|
|
511
|
+
"~ pil --show-tool: show current tool before latest install",
|
|
512
|
+
"~ pil --show-tool --json: show current tool as JSON",
|
|
513
|
+
"~ pil --list-tools: list detected candidates",
|
|
514
|
+
"~ pil --list-tools --json: list candidates as JSON"
|
|
493
515
|
], {
|
|
494
516
|
align: "left",
|
|
495
|
-
width:
|
|
517
|
+
width: 76,
|
|
496
518
|
marginX: 2,
|
|
497
519
|
marginY: 1,
|
|
498
520
|
paddingX: 1,
|
|
@@ -522,9 +544,27 @@ async function detectNode() {
|
|
|
522
544
|
|
|
523
545
|
//#endregion
|
|
524
546
|
//#region src/pkgManager.ts
|
|
547
|
+
const resolvedToolCache = /* @__PURE__ */ new Map();
|
|
548
|
+
const toolIndicatorMap = {
|
|
549
|
+
pnpm: ["pnpm-workspace.yaml", "pnpm-lock.yaml"],
|
|
550
|
+
yarn: ["yarn.lock", ".yarnrc.yml"],
|
|
551
|
+
bun: ["bun.lock", "bun.lockb"],
|
|
552
|
+
npm: ["package-lock.json", "npm-shrinkwrap.json"]
|
|
553
|
+
};
|
|
554
|
+
const isZh$5 = process.env.PI_Lang === "zh";
|
|
555
|
+
const supportedPkgTools$1 = Object.keys(toolIndicatorMap);
|
|
556
|
+
function isEnabled(value) {
|
|
557
|
+
if (!value) return false;
|
|
558
|
+
const normalized = value.toLowerCase();
|
|
559
|
+
return normalized === "1" || normalized === "true" || normalized === "yes";
|
|
560
|
+
}
|
|
525
561
|
function normalizeDir$1(dir) {
|
|
526
562
|
return path.resolve(dir);
|
|
527
563
|
}
|
|
564
|
+
function isSameOrInsideDir(base, target) {
|
|
565
|
+
const relative = path.relative(base, target);
|
|
566
|
+
return relative === "" || !relative.startsWith("..") && !path.isAbsolute(relative);
|
|
567
|
+
}
|
|
528
568
|
function findUpSync$1(startDir, predicate) {
|
|
529
569
|
let current = normalizeDir$1(startDir);
|
|
530
570
|
while (true) {
|
|
@@ -534,24 +574,332 @@ function findUpSync$1(startDir, predicate) {
|
|
|
534
574
|
current = parent;
|
|
535
575
|
}
|
|
536
576
|
}
|
|
537
|
-
function
|
|
538
|
-
|
|
539
|
-
if (
|
|
540
|
-
|
|
541
|
-
return null;
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
577
|
+
function findToolCandidate(cwd, tool) {
|
|
578
|
+
const indicators = toolIndicatorMap[tool];
|
|
579
|
+
if (!indicators?.length) return null;
|
|
580
|
+
const root = findUpSync$1(cwd, (dir) => indicators.some((indicator) => isFile(path.join(dir, indicator))));
|
|
581
|
+
if (!root) return null;
|
|
582
|
+
const foundIndicators = indicators.filter((indicator) => isFile(path.join(root, indicator)));
|
|
583
|
+
return {
|
|
584
|
+
tool,
|
|
585
|
+
root,
|
|
586
|
+
indicators: foundIndicators.length ? foundIndicators : indicators.slice(0, 1)
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
function getToolCandidates(cwd) {
|
|
590
|
+
return Object.keys(toolIndicatorMap).map((tool) => findToolCandidate(cwd, tool)).filter(Boolean);
|
|
591
|
+
}
|
|
592
|
+
function getPreferenceWorkspaceKey(cwd, candidates) {
|
|
593
|
+
const roots = [...new Set(candidates.map((candidate) => normalizeDir$1(candidate.root)))];
|
|
594
|
+
if (roots.length === 1) return roots[0];
|
|
595
|
+
return normalizeDir$1(cwd);
|
|
596
|
+
}
|
|
597
|
+
function findStoredWorkspaceKey(cwd, preferences) {
|
|
598
|
+
return Object.keys(preferences.workspaces).filter((workspaceKey) => isSameOrInsideDir(workspaceKey, cwd)).sort((a, b) => b.length - a.length)[0] || null;
|
|
599
|
+
}
|
|
600
|
+
function resolveWorkspaceKey(cwd, candidates, preferences) {
|
|
601
|
+
if (candidates.length > 0) return getPreferenceWorkspaceKey(cwd, candidates);
|
|
602
|
+
return findStoredWorkspaceKey(cwd, preferences) || normalizeDir$1(cwd);
|
|
603
|
+
}
|
|
604
|
+
function getConfigHome() {
|
|
605
|
+
const home = process.env.HOME || os.homedir();
|
|
606
|
+
if (process.platform === "win32") return process.env.APPDATA || path.join(home, "AppData", "Roaming");
|
|
607
|
+
return process.env.XDG_CONFIG_HOME || path.join(home, ".config");
|
|
608
|
+
}
|
|
609
|
+
function getWorkspaceToolPreferencePath() {
|
|
610
|
+
return path.join(getConfigHome(), "pi", "workspace-tools.json");
|
|
611
|
+
}
|
|
612
|
+
async function readWorkspaceToolPreferences() {
|
|
613
|
+
const configPath = getWorkspaceToolPreferencePath();
|
|
614
|
+
try {
|
|
615
|
+
const raw = await fs.readFile(configPath, "utf8");
|
|
616
|
+
const parsed = JSON.parse(raw);
|
|
617
|
+
return {
|
|
618
|
+
version: 1,
|
|
619
|
+
workspaces: parsed.workspaces && typeof parsed.workspaces === "object" ? parsed.workspaces : {}
|
|
620
|
+
};
|
|
621
|
+
} catch {
|
|
622
|
+
return {
|
|
623
|
+
version: 1,
|
|
624
|
+
workspaces: {}
|
|
625
|
+
};
|
|
548
626
|
}
|
|
549
|
-
|
|
627
|
+
}
|
|
628
|
+
async function writeWorkspaceToolPreference(workspaceKey, tool) {
|
|
629
|
+
const configPath = getWorkspaceToolPreferencePath();
|
|
630
|
+
const data = await readWorkspaceToolPreferences();
|
|
631
|
+
data.workspaces[workspaceKey] = tool;
|
|
632
|
+
await fs.mkdir(path.dirname(configPath), { recursive: true });
|
|
633
|
+
await fs.writeFile(configPath, JSON.stringify(data, null, 2), "utf8");
|
|
634
|
+
resolvedToolCache.clear();
|
|
635
|
+
}
|
|
636
|
+
async function deleteWorkspaceToolPreference(workspaceKey) {
|
|
637
|
+
const configPath = getWorkspaceToolPreferencePath();
|
|
638
|
+
const data = await readWorkspaceToolPreferences();
|
|
639
|
+
if (!(workspaceKey in data.workspaces)) return;
|
|
640
|
+
delete data.workspaces[workspaceKey];
|
|
641
|
+
await fs.mkdir(path.dirname(configPath), { recursive: true });
|
|
642
|
+
await fs.writeFile(configPath, JSON.stringify(data, null, 2), "utf8");
|
|
643
|
+
resolvedToolCache.clear();
|
|
644
|
+
}
|
|
645
|
+
async function forgetPkgToolPreference() {
|
|
646
|
+
const workspaceKey = findStoredWorkspaceKey(normalizeDir$1(process.cwd()), await readWorkspaceToolPreferences());
|
|
647
|
+
if (!workspaceKey) return false;
|
|
648
|
+
await deleteWorkspaceToolPreference(workspaceKey);
|
|
649
|
+
return true;
|
|
650
|
+
}
|
|
651
|
+
function getSupportedPkgToolNames() {
|
|
652
|
+
return supportedPkgTools$1.slice();
|
|
653
|
+
}
|
|
654
|
+
function getPreferredToolFromEnv(candidates) {
|
|
655
|
+
const preferred = process.env.PI_DEFAULT;
|
|
656
|
+
if (!preferred) return null;
|
|
657
|
+
return candidates.some((candidate) => candidate.tool === preferred) ? preferred : null;
|
|
658
|
+
}
|
|
659
|
+
function getExplicitPreferredTool(value) {
|
|
660
|
+
if (!value) return null;
|
|
661
|
+
return supportedPkgTools$1.includes(value) ? value : null;
|
|
662
|
+
}
|
|
663
|
+
function validateExplicitPreferredTool(preferredTool, candidates) {
|
|
664
|
+
if (candidates.length === 0) return true;
|
|
665
|
+
return candidates.some((candidate) => candidate.tool === preferredTool);
|
|
666
|
+
}
|
|
667
|
+
function logInvalidPreferredTool(preferredTool, candidates) {
|
|
668
|
+
const names = candidates.map((candidate) => candidate.tool).join(", ");
|
|
669
|
+
console.log(color.red(isZh$5 ? `当前 workspace 可选的包管理器是: ${names},不能直接指定 ${preferredTool}。` : `This workspace supports: ${names}. ${preferredTool} cannot be selected directly here.`));
|
|
670
|
+
}
|
|
671
|
+
function getDetectedToolFallback(detected, candidates) {
|
|
672
|
+
if (candidates.some((candidate) => candidate.tool === detected)) return detected;
|
|
673
|
+
return candidates[0]?.tool || detected;
|
|
674
|
+
}
|
|
675
|
+
function formatCandidateLabel(candidate, cwd) {
|
|
676
|
+
const indicators = candidate.indicators.join(", ");
|
|
677
|
+
const relativeRoot = path.relative(cwd, candidate.root) || ".";
|
|
678
|
+
if (relativeRoot === ".") return `${candidate.tool}: ${indicators}`;
|
|
679
|
+
return `${candidate.tool}: ${indicators} (${relativeRoot})`;
|
|
680
|
+
}
|
|
681
|
+
function toCandidateInfo(candidates) {
|
|
682
|
+
return candidates.map((candidate) => ({
|
|
683
|
+
tool: candidate.tool,
|
|
684
|
+
indicators: candidate.indicators,
|
|
685
|
+
root: candidate.root
|
|
686
|
+
}));
|
|
687
|
+
}
|
|
688
|
+
async function selectToolCandidate(candidates, cwd) {
|
|
689
|
+
if (!isInteractive()) return { status: "unavailable" };
|
|
690
|
+
const options = candidates.map((candidate) => formatCandidateLabel(candidate, cwd));
|
|
691
|
+
const labelToTool = new Map(candidates.map((candidate) => [formatCandidateLabel(candidate, cwd), candidate.tool]));
|
|
692
|
+
const choice = await ttySelect(options, isZh$5 ? "🤔检测到多个包管理环境,请选择当前 workspace 使用的安装方式" : "Multiple package managers were detected, choose one for this workspace.");
|
|
693
|
+
if (!choice) return { status: "cancelled" };
|
|
694
|
+
const tool = labelToTool.get(choice);
|
|
695
|
+
if (!tool) return { status: "cancelled" };
|
|
696
|
+
return {
|
|
697
|
+
status: "selected",
|
|
698
|
+
tool
|
|
699
|
+
};
|
|
700
|
+
}
|
|
701
|
+
function logAmbiguousToolFallback(candidates, tool) {
|
|
702
|
+
const names = candidates.map((candidate) => candidate.tool).join(", ");
|
|
703
|
+
console.log(color.yellow(isZh$5 ? `检测到多个包管理环境(${names}),当前不是交互式终端,临时使用 ${tool}。可在交互式终端运行一次 pi 以保存当前 workspace 的选择。` : `Detected multiple package managers (${names}). Using ${tool} for now because no interactive TTY is available. Run pi once in an interactive shell to save this workspace preference.`));
|
|
704
|
+
}
|
|
705
|
+
function getSourceLabel(source) {
|
|
706
|
+
switch (source) {
|
|
707
|
+
case "saved-preference": return isZh$5 ? "已保存的 workspace 选择" : "saved workspace choice";
|
|
708
|
+
case "fresh-selection": return isZh$5 ? "本次重新选择并保存" : "picked now and saved";
|
|
709
|
+
case "single-candidate": return isZh$5 ? "当前只检测到一种包管理器标记" : "single detected package-manager indicator";
|
|
710
|
+
case "env-default": return isZh$5 ? "PI_DEFAULT 兜底" : "PI_DEFAULT fallback";
|
|
711
|
+
case "detected-tool": return isZh$5 ? "环境自动检测结果" : "environment auto-detection";
|
|
712
|
+
case "non-interactive-fallback": return isZh$5 ? "非交互终端下的临时兜底" : "non-interactive fallback";
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
function logWorkspaceToolSelected(tool, forceChoose) {
|
|
716
|
+
console.log(color.green(forceChoose ? isZh$5 ? `当前 workspace 已切换为使用 ${tool},并保存了这个选择。` : `This workspace now uses ${tool}, and the choice has been saved.` : isZh$5 ? `已为当前 workspace 记住 ${tool} 作为包管理器。` : `Saved ${tool} as the package manager for this workspace.`));
|
|
717
|
+
}
|
|
718
|
+
function logWorkspaceToolResolved(tool) {
|
|
719
|
+
console.log(color.green(isZh$5 ? `当前 workspace 使用 ${tool} 作为包管理器。` : `This workspace uses ${tool} as the package manager.`));
|
|
720
|
+
}
|
|
721
|
+
function logStaleWorkspaceToolRemoved(tool) {
|
|
722
|
+
console.log(color.yellow(isZh$5 ? `检测到之前保存的 ${tool} 已不再适用于当前 workspace,已自动清除旧记录。` : `The saved ${tool} choice no longer matches this workspace and was removed automatically.`));
|
|
723
|
+
}
|
|
724
|
+
async function preparePkgToolContext(forgetPreference = false) {
|
|
725
|
+
const cwd = normalizeDir$1(process.cwd());
|
|
726
|
+
const originalDetected = await getPkgTool() || "npm";
|
|
727
|
+
const candidates = getToolCandidates(cwd);
|
|
728
|
+
const preferences = await readWorkspaceToolPreferences();
|
|
729
|
+
const workspaceKey = resolveWorkspaceKey(cwd, candidates, preferences);
|
|
730
|
+
let detected = originalDetected;
|
|
731
|
+
if (forgetPreference) await deleteWorkspaceToolPreference(workspaceKey);
|
|
732
|
+
if (forgetPreference) delete preferences.workspaces[workspaceKey];
|
|
733
|
+
let rememberedTool = preferences.workspaces[workspaceKey];
|
|
734
|
+
if (rememberedTool && !candidates.some((candidate) => candidate.tool === rememberedTool)) {
|
|
735
|
+
await deleteWorkspaceToolPreference(workspaceKey);
|
|
736
|
+
delete preferences.workspaces[workspaceKey];
|
|
737
|
+
logStaleWorkspaceToolRemoved(rememberedTool);
|
|
738
|
+
rememberedTool = void 0;
|
|
739
|
+
}
|
|
740
|
+
if (detected === "npm" && candidates.length === 1) detected = candidates[0].tool;
|
|
550
741
|
return {
|
|
551
742
|
detected,
|
|
552
|
-
|
|
743
|
+
candidates,
|
|
744
|
+
rememberedTool
|
|
553
745
|
};
|
|
554
746
|
}
|
|
747
|
+
async function getPkgToolStatus() {
|
|
748
|
+
const { detected, candidates, rememberedTool } = await preparePkgToolContext();
|
|
749
|
+
const candidateInfo = toCandidateInfo(candidates);
|
|
750
|
+
if (rememberedTool) return {
|
|
751
|
+
status: "resolved",
|
|
752
|
+
detected,
|
|
753
|
+
tool: rememberedTool,
|
|
754
|
+
source: "saved-preference",
|
|
755
|
+
candidates: candidateInfo
|
|
756
|
+
};
|
|
757
|
+
if (candidates.length <= 1) {
|
|
758
|
+
const fallback = process.env.PI_DEFAULT;
|
|
759
|
+
return {
|
|
760
|
+
status: "resolved",
|
|
761
|
+
detected,
|
|
762
|
+
tool: detected === "npm" && fallback ? fallback : detected,
|
|
763
|
+
source: detected === "npm" && fallback ? "env-default" : candidates.length === 1 ? "single-candidate" : "detected-tool",
|
|
764
|
+
candidates: candidateInfo
|
|
765
|
+
};
|
|
766
|
+
}
|
|
767
|
+
const envPreferredTool = getPreferredToolFromEnv(candidates);
|
|
768
|
+
if (envPreferredTool) return {
|
|
769
|
+
status: "resolved",
|
|
770
|
+
detected,
|
|
771
|
+
tool: envPreferredTool,
|
|
772
|
+
source: "env-default",
|
|
773
|
+
candidates: candidateInfo
|
|
774
|
+
};
|
|
775
|
+
if (!isInteractive()) return {
|
|
776
|
+
status: "resolved",
|
|
777
|
+
detected,
|
|
778
|
+
tool: getDetectedToolFallback(detected, candidates),
|
|
779
|
+
source: "non-interactive-fallback",
|
|
780
|
+
candidates: candidateInfo
|
|
781
|
+
};
|
|
782
|
+
return {
|
|
783
|
+
status: "needs-selection",
|
|
784
|
+
detected,
|
|
785
|
+
candidates: candidateInfo
|
|
786
|
+
};
|
|
787
|
+
}
|
|
788
|
+
function printPkgToolStatus(status, options = {}) {
|
|
789
|
+
if (options.json) {
|
|
790
|
+
console.log(JSON.stringify({
|
|
791
|
+
...status,
|
|
792
|
+
sourceLabel: status.status === "resolved" ? getSourceLabel(status.source) : void 0
|
|
793
|
+
}, null, 2));
|
|
794
|
+
return;
|
|
795
|
+
}
|
|
796
|
+
if (status.status === "resolved") {
|
|
797
|
+
const candidateNames = status.candidates.map((candidate) => candidate.tool).join(", ");
|
|
798
|
+
console.log(color.green(isZh$5 ? `当前 workspace 使用 ${status.tool} 作为包管理器。` : `This workspace uses ${status.tool} as the package manager.`));
|
|
799
|
+
console.log(color.cyan(`${isZh$5 ? "来源" : "Source"}: ${getSourceLabel(status.source)}`));
|
|
800
|
+
if (candidateNames) console.log(color.dim(`${isZh$5 ? "候选项" : "Candidates"}: ${candidateNames}`));
|
|
801
|
+
return;
|
|
802
|
+
}
|
|
803
|
+
const candidateNames = status.candidates.map((candidate) => candidate.tool).join(", ");
|
|
804
|
+
console.log(color.yellow(isZh$5 ? "当前 workspace 还没有固定包管理器选择。" : "This workspace does not have a locked package-manager choice yet."));
|
|
805
|
+
console.log(color.cyan(`${isZh$5 ? "原因" : "Reason"}: ${isZh$5 ? "检测到了多个包管理器标记,且当前没有保存的选择。" : "Multiple package-manager indicators were found and no saved choice exists yet."}`));
|
|
806
|
+
console.log(color.dim(`${isZh$5 ? "候选项" : "Candidates"}: ${candidateNames}`));
|
|
807
|
+
console.log(color.dim(isZh$5 ? "可执行 `pi --choose-tool` 来保存当前 workspace 的选择。" : "Run `pi --choose-tool` to save a choice for this workspace."));
|
|
808
|
+
}
|
|
809
|
+
function printPkgToolCandidates(status, options = {}) {
|
|
810
|
+
if (options.json) {
|
|
811
|
+
console.log(JSON.stringify({
|
|
812
|
+
...status,
|
|
813
|
+
sourceLabel: status.status === "resolved" ? getSourceLabel(status.source) : void 0
|
|
814
|
+
}, null, 2));
|
|
815
|
+
return;
|
|
816
|
+
}
|
|
817
|
+
if (status.status === "resolved") {
|
|
818
|
+
console.log(color.green(isZh$5 ? `当前 workspace 使用 ${status.tool} 作为包管理器。` : `This workspace uses ${status.tool} as the package manager.`));
|
|
819
|
+
console.log(color.cyan(`${isZh$5 ? "来源" : "Source"}: ${getSourceLabel(status.source)}`));
|
|
820
|
+
} else console.log(color.yellow(isZh$5 ? "当前 workspace 还没有固定包管理器选择。" : "This workspace does not have a locked package-manager choice yet."));
|
|
821
|
+
if (status.candidates.length === 0) {
|
|
822
|
+
console.log(color.dim(isZh$5 ? "当前 workspace 没有检测到明确的 lockfile / workspace 候选。" : "No explicit lockfile or workspace candidates were detected in this workspace."));
|
|
823
|
+
return;
|
|
824
|
+
}
|
|
825
|
+
console.log(color.bold(isZh$5 ? "候选工具:" : "Candidate tools:"));
|
|
826
|
+
for (const candidate of status.candidates) {
|
|
827
|
+
const indicators = candidate.indicators.join(", ");
|
|
828
|
+
console.log(`- ${candidate.tool}`);
|
|
829
|
+
console.log(color.dim(` ${isZh$5 ? "root" : "root"}: ${candidate.root}`));
|
|
830
|
+
console.log(color.dim(` ${isZh$5 ? "indicators" : "indicators"}: ${indicators}`));
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
async function resolvePkgTool(options = {}) {
|
|
834
|
+
const cwd = normalizeDir$1(process.cwd());
|
|
835
|
+
const forceChoose = options.forceChoose || isEnabled(process.env.PI_FORCE_PICK_TOOL);
|
|
836
|
+
const forgetPreference = options.forgetPreference || isEnabled(process.env.PI_FORGET_PICK_TOOL);
|
|
837
|
+
const preferredTool = getExplicitPreferredTool(options.preferredTool || process.env.PI_PREFERRED_TOOL);
|
|
838
|
+
const shouldBypassCache = forceChoose || forgetPreference || Boolean(preferredTool);
|
|
839
|
+
const cached = resolvedToolCache.get(cwd);
|
|
840
|
+
if (!shouldBypassCache && cached) return cached;
|
|
841
|
+
const pending = (async () => {
|
|
842
|
+
const { detected, candidates, rememberedTool } = await preparePkgToolContext(forgetPreference);
|
|
843
|
+
if (preferredTool) {
|
|
844
|
+
if (!validateExplicitPreferredTool(preferredTool, candidates)) {
|
|
845
|
+
logInvalidPreferredTool(preferredTool, candidates);
|
|
846
|
+
process.exit(1);
|
|
847
|
+
}
|
|
848
|
+
await writeWorkspaceToolPreference(resolveWorkspaceKey(normalizeDir$1(process.cwd()), candidates, await readWorkspaceToolPreferences()), preferredTool);
|
|
849
|
+
logWorkspaceToolSelected(preferredTool, true);
|
|
850
|
+
return {
|
|
851
|
+
detected,
|
|
852
|
+
tool: preferredTool,
|
|
853
|
+
source: "fresh-selection"
|
|
854
|
+
};
|
|
855
|
+
}
|
|
856
|
+
if (!forceChoose && rememberedTool) return {
|
|
857
|
+
detected,
|
|
858
|
+
tool: rememberedTool,
|
|
859
|
+
source: "saved-preference"
|
|
860
|
+
};
|
|
861
|
+
if (candidates.length <= 1) {
|
|
862
|
+
const fallback = process.env.PI_DEFAULT;
|
|
863
|
+
const tool = detected === "npm" && fallback ? fallback : detected;
|
|
864
|
+
if (forceChoose) logWorkspaceToolResolved(tool);
|
|
865
|
+
return {
|
|
866
|
+
detected,
|
|
867
|
+
tool,
|
|
868
|
+
source: detected === "npm" && fallback ? "env-default" : candidates.length === 1 ? "single-candidate" : "detected-tool"
|
|
869
|
+
};
|
|
870
|
+
}
|
|
871
|
+
const envPreferredTool = getPreferredToolFromEnv(candidates);
|
|
872
|
+
if (!forceChoose && envPreferredTool) return {
|
|
873
|
+
detected,
|
|
874
|
+
tool: envPreferredTool,
|
|
875
|
+
source: "env-default"
|
|
876
|
+
};
|
|
877
|
+
const cwdForSelection = normalizeDir$1(process.cwd());
|
|
878
|
+
const selection = await selectToolCandidate(candidates, cwd);
|
|
879
|
+
if (selection.status === "selected") {
|
|
880
|
+
await writeWorkspaceToolPreference(resolveWorkspaceKey(cwdForSelection, candidates, await readWorkspaceToolPreferences()), selection.tool);
|
|
881
|
+
logWorkspaceToolSelected(selection.tool, forceChoose);
|
|
882
|
+
return {
|
|
883
|
+
detected,
|
|
884
|
+
tool: selection.tool,
|
|
885
|
+
source: "fresh-selection"
|
|
886
|
+
};
|
|
887
|
+
}
|
|
888
|
+
if (selection.status === "cancelled") {
|
|
889
|
+
console.log(color.dim(isZh$5 ? "已取消" : "Cancelled"));
|
|
890
|
+
process.exit(0);
|
|
891
|
+
}
|
|
892
|
+
const tool = getDetectedToolFallback(detected, candidates);
|
|
893
|
+
logAmbiguousToolFallback(candidates, tool);
|
|
894
|
+
return {
|
|
895
|
+
detected,
|
|
896
|
+
tool,
|
|
897
|
+
source: "non-interactive-fallback"
|
|
898
|
+
};
|
|
899
|
+
})();
|
|
900
|
+
resolvedToolCache.set(cwd, pending);
|
|
901
|
+
return pending;
|
|
902
|
+
}
|
|
555
903
|
function getInstallCommand(tool, hasParams) {
|
|
556
904
|
const action = hasParams ? "add" : "install";
|
|
557
905
|
switch (tool) {
|
|
@@ -609,12 +957,7 @@ async function findUpAsync(startDir, predicate) {
|
|
|
609
957
|
async function getParams(params) {
|
|
610
958
|
const cwd = process.cwd();
|
|
611
959
|
try {
|
|
612
|
-
|
|
613
|
-
if (tool === "npm") {
|
|
614
|
-
if (findUpSync(cwd, (dir) => isFile(path.join(dir, "pnpm-workspace.yaml")) || isFile(path.join(dir, "pnpm-lock.yaml")))) tool = "pnpm";
|
|
615
|
-
else if (findUpSync(cwd, (dir) => isFile(path.join(dir, "yarn.lock")) || isFile(path.join(dir, ".yarnrc.yml")))) tool = "yarn";
|
|
616
|
-
else if (findUpSync(cwd, (dir) => isFile(path.join(dir, "bun.lockb")))) tool = "bun";
|
|
617
|
-
}
|
|
960
|
+
const { tool } = await resolvePkgTool();
|
|
618
961
|
switch (tool) {
|
|
619
962
|
case "pnpm": {
|
|
620
963
|
const pnpmWorkspaceRoot = findUpSync(cwd, (dir) => isFile(path.join(dir, "pnpm-workspace.yaml")));
|
|
@@ -737,11 +1080,11 @@ async function pushHistory(command) {
|
|
|
737
1080
|
historyFormat = "bash";
|
|
738
1081
|
}
|
|
739
1082
|
try {
|
|
740
|
-
if (!fs.existsSync(historyFile)) {
|
|
1083
|
+
if (!fs$1.existsSync(historyFile)) {
|
|
741
1084
|
log$1(color.yellow(`${isZh$4 ? `未找到 ${shellName} 历史文件` : `${shellName} history file not found`}`));
|
|
742
1085
|
return;
|
|
743
1086
|
}
|
|
744
|
-
const raw = fs.readFileSync(historyFile, "utf8");
|
|
1087
|
+
const raw = fs$1.readFileSync(historyFile, "utf8");
|
|
745
1088
|
const timestamp = Math.floor(Date.now() / 1e3);
|
|
746
1089
|
let newEntry = "";
|
|
747
1090
|
if (historyFormat === "zsh") newEntry = `: ${timestamp}:0;${command}`;
|
|
@@ -824,8 +1167,8 @@ async function pushHistory(command) {
|
|
|
824
1167
|
if (historyFormat === "fish") finalContent = `${newEntries.map((e) => e.trimEnd()).join("\n")}\n`;
|
|
825
1168
|
else finalContent = `${newEntries.join("\n")}\n`;
|
|
826
1169
|
const tmpPath = `${historyFile}.ccommand.tmp`;
|
|
827
|
-
fs.writeFileSync(tmpPath, finalContent, "utf8");
|
|
828
|
-
fs.renameSync(tmpPath, historyFile);
|
|
1170
|
+
fs$1.writeFileSync(tmpPath, finalContent, "utf8");
|
|
1171
|
+
fs$1.renameSync(tmpPath, historyFile);
|
|
829
1172
|
} catch (err) {
|
|
830
1173
|
log$1(color.red(`${isZh$4 ? `❌ 添加到 ${shellName} 历史记录失败` : `❌ Failed to add to ${shellName} history`}${err ? `: ${String(err)}` : ""}`));
|
|
831
1174
|
}
|
|
@@ -851,9 +1194,9 @@ async function pi(params, pkg, executor = "pi") {
|
|
|
851
1194
|
];
|
|
852
1195
|
let loading_status;
|
|
853
1196
|
const { PI_DEFAULT, PI_MaxSockets: sockets } = process.env;
|
|
854
|
-
const {
|
|
1197
|
+
const { tool } = await resolvePkgTool();
|
|
855
1198
|
const maxSockets = sockets || 4;
|
|
856
|
-
if (
|
|
1199
|
+
if (tool === "npm" && !PI_DEFAULT) stdio = "inherit";
|
|
857
1200
|
else loading_status = await loading(text, isSilent);
|
|
858
1201
|
executor = getInstallCommand(tool, Boolean(params));
|
|
859
1202
|
const newParams = isLatest ? "" : await getParams(params);
|
|
@@ -1059,11 +1402,11 @@ function ensurePrunAutoInit() {
|
|
|
1059
1402
|
} else return;
|
|
1060
1403
|
try {
|
|
1061
1404
|
const dir = path.dirname(rcFile);
|
|
1062
|
-
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
|
1063
|
-
const content = fs.existsSync(rcFile) ? fs.readFileSync(rcFile, "utf8") : "";
|
|
1405
|
+
if (!fs$1.existsSync(dir)) fs$1.mkdirSync(dir, { recursive: true });
|
|
1406
|
+
const content = fs$1.existsSync(rcFile) ? fs$1.readFileSync(rcFile, "utf8") : "";
|
|
1064
1407
|
if (!/prun\s+--init/.test(content)) {
|
|
1065
1408
|
const prefix = content.length && !content.endsWith("\n") ? "\n" : "";
|
|
1066
|
-
fs.appendFileSync(rcFile, `${prefix}${initLine}\n`, "utf8");
|
|
1409
|
+
fs$1.appendFileSync(rcFile, `${prefix}${initLine}\n`, "utf8");
|
|
1067
1410
|
}
|
|
1068
1411
|
} catch {}
|
|
1069
1412
|
}
|
|
@@ -1525,6 +1868,72 @@ const runMap = {
|
|
|
1525
1868
|
"pio.mjs": pio
|
|
1526
1869
|
};
|
|
1527
1870
|
const isZh = process.env.PI_Lang === "zh";
|
|
1871
|
+
const pkgToolFlagCommands = new Set([
|
|
1872
|
+
"pi",
|
|
1873
|
+
"pi.mjs",
|
|
1874
|
+
"pil",
|
|
1875
|
+
"pil.mjs",
|
|
1876
|
+
"pci",
|
|
1877
|
+
"pci.mjs"
|
|
1878
|
+
]);
|
|
1879
|
+
const supportedPkgTools = new Set(getSupportedPkgToolNames());
|
|
1880
|
+
function parsePkgToolFlags(argv) {
|
|
1881
|
+
const hasInspectFlag = argv.includes("--show-tool") || argv.includes("--list-tools");
|
|
1882
|
+
let chooseTool = false;
|
|
1883
|
+
let forgetTool = false;
|
|
1884
|
+
let listTools = false;
|
|
1885
|
+
let showTool = false;
|
|
1886
|
+
let showToolJson = false;
|
|
1887
|
+
let preferredTool = "";
|
|
1888
|
+
let invalidPreferredTool = "";
|
|
1889
|
+
const normalizedArgv = [];
|
|
1890
|
+
for (let i = 0; i < argv.length; i++) {
|
|
1891
|
+
const arg = argv[i];
|
|
1892
|
+
if (arg === "--forget-tool") {
|
|
1893
|
+
forgetTool = true;
|
|
1894
|
+
continue;
|
|
1895
|
+
}
|
|
1896
|
+
if (arg === "--show-tool") {
|
|
1897
|
+
showTool = true;
|
|
1898
|
+
continue;
|
|
1899
|
+
}
|
|
1900
|
+
if (arg === "--list-tools") {
|
|
1901
|
+
listTools = true;
|
|
1902
|
+
continue;
|
|
1903
|
+
}
|
|
1904
|
+
if (arg === "--json" && hasInspectFlag) {
|
|
1905
|
+
showToolJson = true;
|
|
1906
|
+
continue;
|
|
1907
|
+
}
|
|
1908
|
+
if (arg === "--choose-tool") {
|
|
1909
|
+
chooseTool = true;
|
|
1910
|
+
const next = argv[i + 1];
|
|
1911
|
+
if (next && supportedPkgTools.has(next)) {
|
|
1912
|
+
preferredTool = next;
|
|
1913
|
+
i++;
|
|
1914
|
+
}
|
|
1915
|
+
continue;
|
|
1916
|
+
}
|
|
1917
|
+
if (arg.startsWith("--choose-tool=")) {
|
|
1918
|
+
chooseTool = true;
|
|
1919
|
+
const value = arg.slice(14);
|
|
1920
|
+
if (supportedPkgTools.has(value)) preferredTool = value;
|
|
1921
|
+
else invalidPreferredTool = value;
|
|
1922
|
+
continue;
|
|
1923
|
+
}
|
|
1924
|
+
normalizedArgv.push(arg);
|
|
1925
|
+
}
|
|
1926
|
+
return {
|
|
1927
|
+
chooseTool,
|
|
1928
|
+
forgetTool,
|
|
1929
|
+
invalidPreferredTool,
|
|
1930
|
+
listTools,
|
|
1931
|
+
normalizedArgv,
|
|
1932
|
+
preferredTool,
|
|
1933
|
+
showTool,
|
|
1934
|
+
showToolJson
|
|
1935
|
+
};
|
|
1936
|
+
}
|
|
1528
1937
|
async function setup() {
|
|
1529
1938
|
const cmd = process.argv[1];
|
|
1530
1939
|
let exec = "";
|
|
@@ -1541,8 +1950,56 @@ async function setup() {
|
|
|
1541
1950
|
printPrunInit(argv.slice(1));
|
|
1542
1951
|
return;
|
|
1543
1952
|
}
|
|
1544
|
-
|
|
1545
|
-
|
|
1953
|
+
const supportsPkgToolFlags = pkgToolFlagCommands.has(exec);
|
|
1954
|
+
const { chooseTool, forgetTool, invalidPreferredTool, listTools, normalizedArgv, preferredTool, showTool, showToolJson } = supportsPkgToolFlags ? parsePkgToolFlags(argv) : {
|
|
1955
|
+
chooseTool: false,
|
|
1956
|
+
forgetTool: false,
|
|
1957
|
+
invalidPreferredTool: "",
|
|
1958
|
+
listTools: false,
|
|
1959
|
+
normalizedArgv: argv,
|
|
1960
|
+
preferredTool: "",
|
|
1961
|
+
showTool: false,
|
|
1962
|
+
showToolJson: false
|
|
1963
|
+
};
|
|
1964
|
+
if (invalidPreferredTool) {
|
|
1965
|
+
console.log(color.red(isZh ? `不支持直接指定 ${invalidPreferredTool},可选值为: ${getSupportedPkgToolNames().join(", ")}` : `Unsupported tool "${invalidPreferredTool}". Valid values: ${getSupportedPkgToolNames().join(", ")}`));
|
|
1966
|
+
return;
|
|
1967
|
+
}
|
|
1968
|
+
if (chooseTool) process.env.PI_FORCE_PICK_TOOL = "1";
|
|
1969
|
+
else delete process.env.PI_FORCE_PICK_TOOL;
|
|
1970
|
+
if (forgetTool) process.env.PI_FORGET_PICK_TOOL = "1";
|
|
1971
|
+
else delete process.env.PI_FORGET_PICK_TOOL;
|
|
1972
|
+
if (preferredTool) process.env.PI_PREFERRED_TOOL = preferredTool;
|
|
1973
|
+
else delete process.env.PI_PREFERRED_TOOL;
|
|
1974
|
+
let params = spaceFormat(normalizedArgv.join(" ")).trim();
|
|
1975
|
+
const hasPackage = await hasPkg(rootPath);
|
|
1976
|
+
if (supportsPkgToolFlags && (chooseTool || forgetTool || showTool || listTools)) {
|
|
1977
|
+
if (!hasPackage) {
|
|
1978
|
+
console.log(color.yellow(isZh ? "当前命令仅在 Node 项目的包管理场景下可用。" : "This option is only available for package-manager selection in Node projects."));
|
|
1979
|
+
return;
|
|
1980
|
+
}
|
|
1981
|
+
if (showTool || listTools) {
|
|
1982
|
+
if (forgetTool && !chooseTool) {
|
|
1983
|
+
const removed = await forgetPkgToolPreference();
|
|
1984
|
+
console.log(removed ? color.green(isZh ? "已清除当前 workspace 保存的包管理器选择。" : "Cleared the saved package-manager choice for this workspace.") : color.yellow(isZh ? "当前 workspace 没有保存的包管理器选择。" : "No saved package-manager choice was found for this workspace."));
|
|
1985
|
+
}
|
|
1986
|
+
if (chooseTool) await resolvePkgTool();
|
|
1987
|
+
const status = await getPkgToolStatus();
|
|
1988
|
+
if (listTools) printPkgToolCandidates(status, { json: showToolJson });
|
|
1989
|
+
else printPkgToolStatus(status, { json: showToolJson });
|
|
1990
|
+
return;
|
|
1991
|
+
}
|
|
1992
|
+
if (normalizedArgv.length === 0) {
|
|
1993
|
+
if (forgetTool && !chooseTool) {
|
|
1994
|
+
const removed = await forgetPkgToolPreference();
|
|
1995
|
+
console.log(removed ? color.green(isZh ? "已清除当前 workspace 保存的包管理器选择。" : "Cleared the saved package-manager choice for this workspace.") : color.yellow(isZh ? "当前 workspace 没有保存的包管理器选择。" : "No saved package-manager choice was found for this workspace."));
|
|
1996
|
+
return;
|
|
1997
|
+
}
|
|
1998
|
+
await resolvePkgTool();
|
|
1999
|
+
return;
|
|
2000
|
+
}
|
|
2001
|
+
}
|
|
2002
|
+
if (!hasPackage) {
|
|
1546
2003
|
if (await isGo(rootPath)) {
|
|
1547
2004
|
if (exec === "pi") {
|
|
1548
2005
|
const loading_status = await loading(`${isZh ? "正在为您安装" : "Installing"} ${params} ...\n`);
|
|
@@ -1594,7 +2051,7 @@ async function setup() {
|
|
|
1594
2051
|
else console.log(color.yellow(isZh ? "命令不存在, 请执行 pi -h 查看帮助" : "The command does not exist, please execute pi -h to view the help"));
|
|
1595
2052
|
return;
|
|
1596
2053
|
}
|
|
1597
|
-
const pkg =
|
|
2054
|
+
const pkg = normalizedArgv.filter((v) => !v.startsWith("-")).join(" ");
|
|
1598
2055
|
await handler(params, pkg);
|
|
1599
2056
|
}
|
|
1600
2057
|
if (!process.env.PI_TEST) setup().catch((error) => {
|