mirror of
https://github.com/prdlk/leetcode.git
synced 2026-09-17 07:26:27 +00:00
55 lines
1.5 KiB
TypeScript
55 lines
1.5 KiB
TypeScript
#!/usr/bin/env bun
|
|||
|
|
/**
|
||
|
|
* Fuzzy-select a solution file from work/ and run leetcode-cli tests on it.
|
||
|
|
*/
|
||
|
|
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");
|
||
|
|
|
||
|
|
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: "Test which solution?",
|
||
|
|
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(
|
||
|
|
[
|
||
|
|
join(ROOT, "node_modules", ".bin", "leetcode"),
|
||
|
|
"test",
|
||
|
|
join(WORK_DIR, picked),
|
||
|
|
],
|
||
|
|
{ cwd: ROOT, stdio: ["inherit", "inherit", "inherit"] },
|
||
|
|
);
|
||
|
|
process.exit(await proc.exited);
|