mirror of
https://github.com/prdlk/leetcode.git
synced 2026-09-17 07:26:27 +00:00
60 lines
1.9 KiB
TypeScript
60 lines
1.9 KiB
TypeScript
/**
|
|||
|
|
* Shared work/ solution picker for the test and submit entries: fuzzy-select
|
||
|
|
* a solution file, then hand it to leetcode-cli. Library: side-effect-free on
|
||
|
|
* import — nothing scans the filesystem until runOnSolution() is called.
|
||
|
|
*/
|
||
|
|
import { autocomplete, cancel, isCancel, log } from "@clack/prompts";
|
||
|
|
import { join, relative } from "node:path";
|
||
|
|
|
||
|
|
const ROOT = join(import.meta.dir, "..", "..");
|
||
|
|
const WORK_DIR = join(ROOT, "work");
|
||
|
|
|
||
|
|
/** Fuzzy-pick a solution under work/ and run `leetcode <command>` on it. */
|
||
|
|
export async function runOnSolution(command: "test" | "submit", message: string): Promise<never> {
|
||
|
|
const files = [...new Bun.Glob("**/*.{js,ts,py,java,c,cpp,go,rs,rb,swift,kt,cs}").scanSync({ cwd: WORK_DIR })]
|
||
|
|
.sort((a, b) => a.localeCompare(b, undefined, { numeric: true }));
|
||
|
|
|
||
|
|
if (files.length === 0) {
|
||
|
|
log.error(`No solution files found in ${relative(process.cwd(), WORK_DIR)}/`);
|
||
|
|
process.exit(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
const picked = await autocomplete<string>({
|
||
|
|
message,
|
||
|
|
placeholder: "Type to search...",
|
||
|
|
maxItems: 12,
|
||
|
|
options: files.map((f) => {
|
||
|
|
// work layout: Difficulty/Category/<id>.<slug>.<ext>
|
||
|
|
const [difficulty, category, name] = f.split("/");
|
||
|
|
return {
|
||
|
|
value: f,
|
||
|
|
label: name ?? f,
|
||
|
|
hint: category ? `${difficulty} · ${category}` : difficulty,
|
||
|
|
};
|
||
|
|
}),
|
||
|
|
filter: (search, option) => {
|
||
|
|
const haystack = option.value.toLowerCase();
|
||
|
|
return search
|
||
|
|
.toLowerCase()
|
||
|
|
.split(/\s+/)
|
||
|
|
.every((token) => haystack.includes(token));
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
if (isCancel(picked)) {
|
||
|
|
cancel("Nothing selected.");
|
||
|
|
process.exit(0);
|
||
|
|
}
|
||
|
|
|
||
|
|
const proc = Bun.spawn(
|
||
|
|
[
|
||
|
|
// bun installs workspace-dep bins into this package's node_modules, not the root's
|
||
|
|
join(import.meta.dir, "node_modules", ".bin", "leetcode"),
|
||
|
|
command,
|
||
|
|
join(WORK_DIR, picked),
|
||
|
|
],
|
||
|
|
{ cwd: ROOT, stdio: ["inherit", "inherit", "inherit"] },
|
||
|
|
);
|
||
|
|
process.exit(await proc.exited);
|
||
|
|
}
|