import process from 'node:process'; import { C as CLI_TEMP_DIR, w as writeFileSafe, r as runCli, q as limitText, y as prompts, e as parseNr } from './shared/ni.CtJepYoh.mjs'; import { F as Fzf, b as byLengthAsc } from './shared/ni.DA7akRvA.mjs'; import { g as getPackageJSON } from './shared/ni.C4mrCGPc.mjs'; import { existsSync, promises } from 'node:fs'; import { resolve } from 'node:path'; import 'readline'; import 'events'; import 'node:module'; import 'child_process'; import 'path'; import 'process'; import 'stream'; import 'os'; import 'tty'; import 'node:os'; import 'fs'; import 'fs/promises'; import 'node:fs/promises'; let storage; const storagePath = resolve(CLI_TEMP_DIR, "_storage.json"); async function load(fn) { if (!storage) { storage = existsSync(storagePath) ? JSON.parse(await promises.readFile(storagePath, "utf-8") || "{}") || {} : {}; } return storage; } async function dump() { if (storage) await writeFileSafe(storagePath, JSON.stringify(storage)); } runCli(async (agent, args, ctx) => { const storage = await load(); if (args[0] === "-") { if (!storage.lastRunCommand) { if (!ctx?.programmatic) { console.error("No last command found"); process.exit(1); } throw new Error("No last command found"); } args[0] = storage.lastRunCommand; } if (args.length === 0 && !ctx?.programmatic) { const pkg = getPackageJSON(ctx); const scripts = pkg.scripts || {}; const scriptsInfo = pkg["scripts-info"] || {}; const names = Object.entries(scripts); if (!names.length) return; const raw = names.filter((i) => !i[0].startsWith("?")).map(([key, cmd]) => ({ key, cmd, description: scriptsInfo[key] || scripts[`?${key}`] || cmd })); const terminalColumns = process.stdout?.columns || 80; const choices = raw.map(({ key, description }) => ({ title: key, value: key, description: limitText(description, terminalColumns - 15) })); const fzf = new Fzf(raw, { selector: (item) => `${item.key} ${item.description}`, casing: "case-insensitive", tiebreakers: [byLengthAsc] }); if (storage.lastRunCommand) { const last = choices.find((i) => i.value === storage.lastRunCommand); if (last) choices.unshift(last); } try { const { fn } = await prompts({ name: "fn", message: "script to run", type: "autocomplete", choices, async suggest(input, choices2) { if (!input) return choices2; const results = fzf.find(input); return results.map((r) => choices2.find((c) => c.value === r.item.key)); } }); if (!fn) return; args.push(fn); } catch { process.exit(1); } } if (storage.lastRunCommand !== args[0]) { storage.lastRunCommand = args[0]; dump(); } return parseNr(agent, args); });