github-search-cli 0.8.1 → 0.9.0
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 +111 -39
- package/package.json +16 -11
- package/scripts/brew.rb +32 -0
- package/scripts/build.ts +19 -0
- package/scripts/generate-svg-demo.js +168 -0
- package/scripts/postinstall.js +106 -0
- package/src/bookmarks.ts +4 -17
- package/src/cli.ts +90 -61
- package/src/config.ts +2 -2
- package/src/history.ts +16 -52
- package/src/notifications.ts +5 -12
- package/src/output.ts +124 -55
- package/src/saved-searches.ts +4 -12
- package/src/search-state.ts +229 -0
- package/src/search.ts +99 -2
- package/src/session.ts +6 -18
- package/src/storage.ts +106 -0
- package/src/themes.ts +47 -7
- package/src/trending.ts +27 -150
- package/src/tui.ts +1110 -278
- package/src/types.ts +2 -0
- package/src/update-check.ts +162 -0
- package/src/watch.ts +23 -11
- package/vendor/bun.exe +0 -0
package/src/cli.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/** CLI entry point — routes between TUI browser and non-interactive modes. */
|
|
3
|
-
import { parseQuery, applyFlagFilters, rankRepos, createGitHubSearch } from "./search";
|
|
4
|
-
import {
|
|
5
|
-
import { formatLines, pipeExec, type FormatLine } from "./pipe";
|
|
3
|
+
import { parseQuery, applyFlagFilters, rankRepos, createGitHubSearch, createTrendingSearch } from "./search";
|
|
4
|
+
import { format as formatOutput, exportToFile, pipeExec, type Format, type ExportFormat, type FormatLine } from "./output";
|
|
6
5
|
import { runWatch } from "./watch";
|
|
7
6
|
import { runInitWizard } from "./init";
|
|
8
7
|
import { readFileSync } from "fs";
|
|
@@ -158,13 +157,13 @@ Options:
|
|
|
158
157
|
}
|
|
159
158
|
|
|
160
159
|
// Determine output format
|
|
161
|
-
const
|
|
160
|
+
const outputFormat: ExportFormat | undefined =
|
|
162
161
|
flags.json ? "json" : flags.csv ? "csv" : flags.markdown ? "markdown" : undefined;
|
|
163
|
-
const isNonInteractive =
|
|
162
|
+
const isNonInteractive = outputFormat || flags.count || flags.format || flags.pipe || flags.watch;
|
|
164
163
|
|
|
165
164
|
// Non-interactive mode
|
|
166
165
|
if (isNonInteractive) {
|
|
167
|
-
await runNonInteractive(flags,
|
|
166
|
+
await runNonInteractive(flags, outputFormat);
|
|
168
167
|
return;
|
|
169
168
|
}
|
|
170
169
|
|
|
@@ -172,65 +171,95 @@ Options:
|
|
|
172
171
|
await launchBrowser();
|
|
173
172
|
}
|
|
174
173
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
if (flags.watch) {
|
|
181
|
-
let tick = 0;
|
|
182
|
-
await runWatch(
|
|
183
|
-
{ query, sort: flags.sort, limit: flags.limit, token: flags.token, intervalMs: flags.interval * 1000, trending: flags.trending },
|
|
184
|
-
(repos, delta) => {
|
|
185
|
-
tick++;
|
|
186
|
-
if (format) {
|
|
187
|
-
console.log(`[Watch #${tick}] ${repos.length} results (${delta >= 0 ? "+" : ""}${delta} since last check)`);
|
|
188
|
-
console.log(formatRepos(repos, format));
|
|
189
|
-
} else {
|
|
190
|
-
console.log(`[Watch #${tick}] ${repos.length} results — ${delta >= 0 ? "+" : ""}${delta} since last check`);
|
|
191
|
-
}
|
|
192
|
-
},
|
|
193
|
-
(err) => console.error(`[Watch error] ${err.message}`),
|
|
194
|
-
);
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// --pipe mode
|
|
199
|
-
if (flags.pipe) {
|
|
200
|
-
const parsed = applyFlagFilters(parseQuery(query), {});
|
|
201
|
-
const response = await provider.search(parsed, { limit: flags.limit, sort: flags.sort, json: false, verbose: false, token: flags.token });
|
|
202
|
-
const repos = rankRepos(response.repos, flags.sort);
|
|
203
|
-
await pipeExec(repos, flags.pipe);
|
|
204
|
-
return;
|
|
205
|
-
}
|
|
174
|
+
interface SearchContext {
|
|
175
|
+
query: string;
|
|
176
|
+
parsed: ReturnType<typeof parseQuery> & { raw: string };
|
|
177
|
+
provider: ReturnType<typeof createGitHubSearch>;
|
|
178
|
+
}
|
|
206
179
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
180
|
+
function buildSearchContext(flags: CLIFlags): SearchContext {
|
|
181
|
+
const parsed = applyFlagFilters(parseQuery(flags.query), {});
|
|
182
|
+
return {
|
|
183
|
+
query: flags.query,
|
|
184
|
+
parsed,
|
|
185
|
+
provider: createGitHubSearch(undefined, flags.token ? [flags.token] : []),
|
|
186
|
+
};
|
|
187
|
+
}
|
|
215
188
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
const
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
189
|
+
async function runNonInteractive(flags: CLIFlags, outputFormat?: ExportFormat) {
|
|
190
|
+
// Dispatch table: each handler receives shared context + flags, returns void.
|
|
191
|
+
const handlers: Record<string, (ctx: SearchContext) => Promise<void>> = {
|
|
192
|
+
trending: async (ctx) => {
|
|
193
|
+
const since = flags.since === "weekly" ? "weekly" : flags.since === "monthly" ? "monthly" : "daily";
|
|
194
|
+
const trendingSearch = createTrendingSearch();
|
|
195
|
+
const response = await trendingSearch.search(
|
|
196
|
+
{ keywords: [], qualifiers: [], raw: "trending" },
|
|
197
|
+
{ limit: 25, sort: "stars", json: false, verbose: false, trendingSince: since },
|
|
198
|
+
);
|
|
199
|
+
if (outputFormat) {
|
|
200
|
+
console.log(formatOutput(response.repos, outputFormat));
|
|
201
|
+
} else if (flags.count) {
|
|
202
|
+
console.log(response.totalCount);
|
|
203
|
+
} else {
|
|
204
|
+
for (const r of response.repos) {
|
|
205
|
+
console.log(r.fullName + " ★ " + r.stars + " ▲ +" + r.score + " " + since + " ● " + (r.language ?? ""));
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
},
|
|
209
|
+
watch: async (ctx) => {
|
|
210
|
+
let tick = 0;
|
|
211
|
+
await runWatch(
|
|
212
|
+
{ query: ctx.query, sort: flags.sort, limit: flags.limit, token: flags.token, intervalMs: flags.interval * 1000, trending: flags.trending },
|
|
213
|
+
(repos, delta) => {
|
|
214
|
+
tick++;
|
|
215
|
+
if (outputFormat) {
|
|
216
|
+
console.log(`[Watch #${tick}] ${repos.length} results (${delta >= 0 ? "+" : ""}${delta} since last check)`);
|
|
217
|
+
console.log(formatOutput(repos, outputFormat));
|
|
218
|
+
} else {
|
|
219
|
+
console.log(`[Watch #${tick}] ${repos.length} results — ${delta >= 0 ? "+" : ""}${delta} since last check`);
|
|
220
|
+
}
|
|
221
|
+
},
|
|
222
|
+
(err) => console.error(`[Watch error] ${err.message}`),
|
|
223
|
+
);
|
|
224
|
+
},
|
|
225
|
+
pipe: async (ctx) => {
|
|
226
|
+
const response = await ctx.provider.search(ctx.parsed, {
|
|
227
|
+
limit: flags.limit, sort: flags.sort, json: false, verbose: false, token: flags.token,
|
|
228
|
+
});
|
|
229
|
+
const repos = rankRepos(response.repos, flags.sort);
|
|
230
|
+
await pipeExec(repos, flags.pipe!);
|
|
231
|
+
},
|
|
232
|
+
format: async (ctx) => {
|
|
233
|
+
const response = await ctx.provider.search(ctx.parsed, {
|
|
234
|
+
limit: flags.limit, sort: flags.sort, json: false, verbose: false, token: flags.token,
|
|
235
|
+
});
|
|
236
|
+
const repos = rankRepos(response.repos, flags.sort);
|
|
237
|
+
console.log(formatOutput(repos, flags.format as Format));
|
|
238
|
+
},
|
|
239
|
+
search: async (ctx) => {
|
|
240
|
+
const response = await ctx.provider.search(ctx.parsed, {
|
|
241
|
+
limit: flags.limit, sort: flags.sort, json: false, verbose: false, token: flags.token,
|
|
242
|
+
});
|
|
243
|
+
const repos = rankRepos(response.repos, flags.sort);
|
|
244
|
+
if (flags.count) {
|
|
245
|
+
console.log(response.totalCount);
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
if (outputFormat) {
|
|
249
|
+
console.log(formatOutput(repos, outputFormat));
|
|
250
|
+
}
|
|
251
|
+
},
|
|
252
|
+
};
|
|
222
253
|
|
|
223
|
-
//
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
254
|
+
// Determine which handler to run
|
|
255
|
+
const key = flags.trending && !flags.watch ? "trending"
|
|
256
|
+
: flags.watch ? "watch"
|
|
257
|
+
: flags.pipe ? "pipe"
|
|
258
|
+
: flags.format ? "format"
|
|
259
|
+
: "search";
|
|
228
260
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
console.log(formatRepos(repos, format));
|
|
232
|
-
return;
|
|
233
|
-
}
|
|
261
|
+
const ctx = buildSearchContext(flags);
|
|
262
|
+
await handlers[key](ctx);
|
|
234
263
|
}
|
|
235
264
|
|
|
236
265
|
main().catch((err) => {
|
package/src/config.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
import type { Config } from "./types";
|
|
10
10
|
import { homedir } from "os";
|
|
11
11
|
import { join } from "path";
|
|
12
|
-
import {
|
|
12
|
+
import { mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
13
13
|
|
|
14
14
|
/** Directory for persistent state (history, bookmarks, session). */
|
|
15
15
|
export function stateDir(): string {
|
|
@@ -27,7 +27,7 @@ export function cacheDir(): string {
|
|
|
27
27
|
const DEFAULTS: Config = {
|
|
28
28
|
defaultSort: "best-match",
|
|
29
29
|
defaultLimit: 50,
|
|
30
|
-
theme: "
|
|
30
|
+
theme: "premium-dark", // New default: premium dark with sharp edges
|
|
31
31
|
cacheTtlSeconds: 300,
|
|
32
32
|
defaultTab: "search",
|
|
33
33
|
};
|
package/src/history.ts
CHANGED
|
@@ -1,53 +1,30 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Persistent search history — append-only JSONL with rotation.
|
|
3
3
|
*/
|
|
4
|
-
import { existsSync, mkdirSync, readFileSync, appendFileSync, writeFileSync, unlinkSync } from "fs";
|
|
5
|
-
import { join } from "path";
|
|
6
4
|
import type { HistoryEntry } from "./types";
|
|
7
|
-
import {
|
|
5
|
+
import { readJSONL, appendJSONL, writeJSON, writeRaw } from "./storage";
|
|
8
6
|
|
|
9
|
-
const HISTORY_FILE =
|
|
7
|
+
const HISTORY_FILE = "history.jsonl";
|
|
10
8
|
const MAX_ENTRIES = 500;
|
|
11
9
|
|
|
12
|
-
function ensureDir() {
|
|
13
|
-
mkdirSync(stateDir(), { recursive: true });
|
|
14
|
-
}
|
|
15
|
-
|
|
16
10
|
/** Append one entry to the history log. */
|
|
17
11
|
export function appendHistory(entry: HistoryEntry): void {
|
|
18
|
-
|
|
19
|
-
const line = JSON.stringify(entry) + "\n";
|
|
20
|
-
try {
|
|
21
|
-
appendFileSync(HISTORY_FILE, line, "utf-8");
|
|
22
|
-
} catch {
|
|
23
|
-
// non-critical
|
|
24
|
-
}
|
|
12
|
+
appendJSONL(HISTORY_FILE, entry);
|
|
25
13
|
}
|
|
26
14
|
|
|
27
15
|
/** Read all history entries, newest first, deduplicating consecutive identical queries. */
|
|
28
16
|
export function readHistory(): HistoryEntry[] {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
// Deduplicate consecutive identical queries (keep newest)
|
|
39
|
-
const deduped: HistoryEntry[] = [];
|
|
40
|
-
let lastQuery: string | undefined;
|
|
41
|
-
for (const e of entries) {
|
|
42
|
-
if (e.query !== lastQuery) {
|
|
43
|
-
deduped.push(e);
|
|
44
|
-
lastQuery = e.query;
|
|
45
|
-
}
|
|
17
|
+
const entries = readJSONL<HistoryEntry>(HISTORY_FILE);
|
|
18
|
+
entries.reverse();
|
|
19
|
+
const deduped: HistoryEntry[] = [];
|
|
20
|
+
let lastQuery: string | undefined;
|
|
21
|
+
for (const e of entries) {
|
|
22
|
+
if (e.query !== lastQuery) {
|
|
23
|
+
deduped.push(e);
|
|
24
|
+
lastQuery = e.query;
|
|
46
25
|
}
|
|
47
|
-
return deduped;
|
|
48
|
-
} catch {
|
|
49
|
-
return [];
|
|
50
26
|
}
|
|
27
|
+
return deduped;
|
|
51
28
|
}
|
|
52
29
|
|
|
53
30
|
/** Delete a single history entry by index (0 = newest). */
|
|
@@ -55,20 +32,13 @@ export function deleteHistoryEntry(index: number): void {
|
|
|
55
32
|
const entries = readHistory();
|
|
56
33
|
if (index < 0 || index >= entries.length) return;
|
|
57
34
|
const removed = entries[index];
|
|
58
|
-
// Rewrite file without the removed entry
|
|
59
35
|
const remaining = entries.filter((e) => e.timestamp !== removed.timestamp || e.query !== removed.query);
|
|
60
|
-
|
|
61
|
-
// Reverse back to oldest-first for file order
|
|
62
|
-
writeFileSync(HISTORY_FILE, remaining.reverse().map((e) => JSON.stringify(e)).join("\n") + "\n");
|
|
36
|
+
writeRaw(HISTORY_FILE, remaining.reverse().map((e) => JSON.stringify(e)).join("\n") + "\n");
|
|
63
37
|
}
|
|
64
38
|
|
|
65
39
|
/** Clear all history. */
|
|
66
40
|
export function clearHistory(): void {
|
|
67
|
-
|
|
68
|
-
writeFileSync(HISTORY_FILE, "");
|
|
69
|
-
} catch {
|
|
70
|
-
// non-critical
|
|
71
|
-
}
|
|
41
|
+
writeRaw(HISTORY_FILE, "");
|
|
72
42
|
}
|
|
73
43
|
|
|
74
44
|
/** Rotate history to max entries (trim oldest). */
|
|
@@ -76,16 +46,10 @@ export function rotateHistory(): void {
|
|
|
76
46
|
const entries = readHistory();
|
|
77
47
|
if (entries.length <= MAX_ENTRIES) return;
|
|
78
48
|
const trimmed = entries.slice(0, MAX_ENTRIES);
|
|
79
|
-
|
|
80
|
-
writeFileSync(HISTORY_FILE, trimmed.reverse().map((e) => JSON.stringify(e)).join("\n") + "\n");
|
|
49
|
+
writeRaw(HISTORY_FILE, trimmed.reverse().map((e) => JSON.stringify(e)).join("\n") + "\n");
|
|
81
50
|
}
|
|
82
51
|
|
|
83
52
|
/** Count entries in history file. */
|
|
84
53
|
export function historyCount(): number {
|
|
85
|
-
|
|
86
|
-
const raw = readFileSync(HISTORY_FILE, "utf-8").trim();
|
|
87
|
-
return raw ? raw.split("\n").length : 0;
|
|
88
|
-
} catch {
|
|
89
|
-
return 0;
|
|
90
|
-
}
|
|
54
|
+
return readJSONL<HistoryEntry>(HISTORY_FILE).length;
|
|
91
55
|
}
|
package/src/notifications.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
/** Notification center — ephemeral alerts for rate limits, watch changes, etc. */
|
|
2
|
-
import {
|
|
3
|
-
import { join } from "path";
|
|
4
|
-
import { stateDir } from "./config";
|
|
2
|
+
import { readJSON, writeJSON } from "./storage";
|
|
5
3
|
|
|
6
4
|
export interface Notification {
|
|
7
5
|
id: number;
|
|
@@ -14,20 +12,15 @@ export interface Notification {
|
|
|
14
12
|
let nextId = 1;
|
|
15
13
|
let notifs: Notification[] = [];
|
|
16
14
|
const MAX_NOTIFS = 50;
|
|
17
|
-
const FILE =
|
|
15
|
+
const FILE = "notifications.json";
|
|
18
16
|
|
|
19
17
|
function load(): void {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
nextId = (Math.max(...notifs.map((n) => n.id), 0) + 1);
|
|
23
|
-
} catch {
|
|
24
|
-
notifs = [];
|
|
25
|
-
}
|
|
18
|
+
notifs = readJSON<Notification[]>(FILE, []);
|
|
19
|
+
nextId = Math.max(...notifs.map((n) => n.id), 0) + 1;
|
|
26
20
|
}
|
|
27
21
|
|
|
28
22
|
function save(): void {
|
|
29
|
-
|
|
30
|
-
writeFileSync(FILE, JSON.stringify(notifs));
|
|
23
|
+
writeJSON(FILE, notifs);
|
|
31
24
|
}
|
|
32
25
|
|
|
33
26
|
export function addNotification(type: Notification["type"], message: string): Notification {
|
package/src/output.ts
CHANGED
|
@@ -1,68 +1,137 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Output module — unified formatting, export, and pipe execution.
|
|
3
|
+
*
|
|
4
|
+
* All format functions (JSON, CSV, Markdown, text, urls, names, ssh-urls,
|
|
5
|
+
* clone-commands, ids) register in a single registry. One `format()` function
|
|
6
|
+
* dispatches. `exportToFile` and `pipeExec` are methods on the same module.
|
|
7
|
+
*/
|
|
8
|
+
import { writeFileSync } from "fs";
|
|
9
|
+
import type { Repo } from "./types";
|
|
10
|
+
import { openUrl } from "./open-url";
|
|
11
|
+
|
|
12
|
+
export type Format =
|
|
13
|
+
| "json" | "csv" | "markdown" | "text"
|
|
14
|
+
| "urls" | "names" | "ssh-urls" | "clone-commands" | "ids";
|
|
15
|
+
|
|
16
|
+
export type FormatFn = (repos: Repo[]) => string;
|
|
17
|
+
|
|
18
|
+
/** Registry of all format functions. Adding a format = 1 line here. */
|
|
19
|
+
const FORMATS: Map<Format, FormatFn> = new Map([
|
|
20
|
+
// Export formats
|
|
21
|
+
["json", formatJson],
|
|
22
|
+
["csv", formatCsv],
|
|
23
|
+
["markdown", formatMarkdown],
|
|
24
|
+
["text", formatText],
|
|
25
|
+
// Pipe formats
|
|
26
|
+
["urls", formatUrls],
|
|
27
|
+
["names", formatNames],
|
|
28
|
+
["ssh-urls", formatSshUrls],
|
|
29
|
+
["clone-commands", formatCloneCommands],
|
|
30
|
+
["ids", formatIds],
|
|
31
|
+
]);
|
|
32
|
+
|
|
33
|
+
// ── Export format functions ────────────────────────────────────────────
|
|
34
|
+
|
|
35
|
+
export function formatJson(repos: Repo[]): string {
|
|
36
|
+
return JSON.stringify(repos, null, 2);
|
|
10
37
|
}
|
|
11
38
|
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
if (
|
|
15
|
-
|
|
39
|
+
function csvEscape(val: unknown): string {
|
|
40
|
+
const str = String(val ?? "");
|
|
41
|
+
if (str.includes('"') || str.includes(",") || str.includes("\n")) {
|
|
42
|
+
return `"${str.replace(/"/g, '""')}"`;
|
|
43
|
+
}
|
|
44
|
+
return str;
|
|
16
45
|
}
|
|
17
46
|
|
|
18
|
-
export function
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
totalCount: response.totalCount,
|
|
23
|
-
count: response.repos.length,
|
|
24
|
-
rateLimited: response.rateLimited,
|
|
25
|
-
results: response.repos,
|
|
26
|
-
},
|
|
27
|
-
null,
|
|
28
|
-
2,
|
|
47
|
+
export function formatCsv(repos: Repo[]): string {
|
|
48
|
+
const header = "rank,full_name,stars,forks,language,url";
|
|
49
|
+
const rows = repos.map((r, i) =>
|
|
50
|
+
`${i + 1},${csvEscape(r.fullName)},${r.stars},${r.forks},${csvEscape(r.language ?? "")},${csvEscape(r.url)}`,
|
|
29
51
|
);
|
|
52
|
+
return [header, ...rows].join("\n");
|
|
30
53
|
}
|
|
31
54
|
|
|
32
|
-
export function
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
55
|
+
export function formatMarkdown(repos: Repo[]): string {
|
|
56
|
+
const header = "| # | Repo | Stars | Forks | Language |";
|
|
57
|
+
const sep = "|---|------|-------|-------|----------|";
|
|
58
|
+
const rows = repos.map(
|
|
59
|
+
(r, i) =>
|
|
60
|
+
`| ${i + 1} | [${r.fullName}](${r.url}) | ${r.stars.toLocaleString()} | ${r.forks.toLocaleString()} | ${r.language ?? "—"} |`,
|
|
61
|
+
);
|
|
62
|
+
return [header, sep, ...rows].join("\n");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function formatText(repos: Repo[]): string {
|
|
66
|
+
return repos.map((r, i) => `${i + 1}. ${r.fullName} ★ ${r.stars.toLocaleString()} ${r.url}`).join("\n");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// ── Pipe format functions ──────────────────────────────────────────────
|
|
70
|
+
|
|
71
|
+
export function formatUrls(repos: Repo[]): string {
|
|
72
|
+
return repos.map((r) => r.url).join("\n");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function formatNames(repos: Repo[]): string {
|
|
76
|
+
return repos.map((r) => r.fullName).join("\n");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function formatSshUrls(repos: Repo[]): string {
|
|
80
|
+
return repos.map((r) => `git@github.com:${r.fullName}.git`).join("\n");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function formatCloneCommands(repos: Repo[]): string {
|
|
84
|
+
return repos.map((r) => `git clone git@github.com:${r.fullName}.git`).join("\n");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function formatIds(repos: Repo[]): string {
|
|
88
|
+
return repos.map((r) => String(r.id)).join("\n");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// ── Public API ─────────────────────────────────────────────────────────
|
|
40
92
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
93
|
+
/** Format repos using the registered format function. */
|
|
94
|
+
export function format(repos: Repo[], format: Format): string {
|
|
95
|
+
const fn = FORMATS.get(format);
|
|
96
|
+
if (!fn) throw new Error(`Unknown format: ${format}`);
|
|
97
|
+
return fn(repos);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Export repos to a file. Returns the file path. */
|
|
101
|
+
export function exportToFile(repos: Repo[], fmt: Format): string {
|
|
102
|
+
const ext = fmt === "markdown" ? "md" : fmt;
|
|
103
|
+
const ts = Date.now();
|
|
104
|
+
const path = `./ghfind-export-${ts}.${ext}`;
|
|
105
|
+
writeFileSync(path, format(repos, fmt), "utf-8");
|
|
106
|
+
return path;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Execute a pipe target (clone, open) on repos. */
|
|
110
|
+
export async function pipeExec(repos: Repo[], target: string): Promise<void> {
|
|
111
|
+
for (const repo of repos) {
|
|
112
|
+
const url = repo.url;
|
|
113
|
+
switch (target) {
|
|
114
|
+
case "open":
|
|
115
|
+
await openUrl(url);
|
|
116
|
+
break;
|
|
117
|
+
case "clone":
|
|
118
|
+
console.log(`git clone git@github.com:${repo.fullName}.git`);
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
44
121
|
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// ─── Backward-compatible re-exports ─────────────────────────────────────
|
|
45
125
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
lines.push("");
|
|
49
|
-
});
|
|
126
|
+
export type ExportFormat = "json" | "csv" | "markdown" | "text";
|
|
127
|
+
export type FormatLine = "urls" | "names" | "ssh-urls" | "clone-commands" | "ids";
|
|
50
128
|
|
|
51
|
-
|
|
129
|
+
/** @deprecated Use `format()` instead. */
|
|
130
|
+
export function formatRepos(repos: Repo[], fmt: ExportFormat): string {
|
|
131
|
+
return format(repos, fmt);
|
|
52
132
|
}
|
|
53
133
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const tags: string[] = [];
|
|
59
|
-
if (repo.archived) tags.push("archived");
|
|
60
|
-
if (repo.isFork) tags.push("fork");
|
|
61
|
-
const tagStr = tags.length ? ` [${tags.join(", ")}]` : "";
|
|
62
|
-
|
|
63
|
-
const header = `${index}. ${repo.fullName}${tagStr}`;
|
|
64
|
-
const meta = ` ★ ${stars} • ${lang} • updated ${updated}`;
|
|
65
|
-
const desc = repo.description ? ` ${repo.description}` : " (no description)";
|
|
66
|
-
const url = ` ${repo.url}`;
|
|
67
|
-
return [header, meta, desc, url].join("\n");
|
|
68
|
-
}
|
|
134
|
+
/** @deprecated Use `format()` instead. */
|
|
135
|
+
export function formatLines(repos: Repo[], fmt: FormatLine): string {
|
|
136
|
+
return format(repos, fmt);
|
|
137
|
+
}
|
package/src/saved-searches.ts
CHANGED
|
@@ -1,25 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Saved searches — named queries you can re-run anytime.
|
|
3
3
|
*/
|
|
4
|
-
import { mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
5
|
-
import { join } from "path";
|
|
6
4
|
import type { SavedSearch, SortStrategy } from "./types";
|
|
7
|
-
import {
|
|
5
|
+
import { readJSON, writeJSON } from "./storage";
|
|
8
6
|
|
|
9
|
-
const SAVED_FILE =
|
|
7
|
+
const SAVED_FILE = "saved-searches.json";
|
|
10
8
|
|
|
11
9
|
function loadRaw(): SavedSearch[] {
|
|
12
|
-
|
|
13
|
-
try {
|
|
14
|
-
return JSON.parse(readFileSync(SAVED_FILE, "utf-8")) as SavedSearch[];
|
|
15
|
-
} catch {
|
|
16
|
-
return [];
|
|
17
|
-
}
|
|
10
|
+
return readJSON<SavedSearch[]>(SAVED_FILE, []);
|
|
18
11
|
}
|
|
19
12
|
|
|
20
13
|
function saveRaw(searches: SavedSearch[]): void {
|
|
21
|
-
|
|
22
|
-
writeFileSync(SAVED_FILE, JSON.stringify(searches, null, 2));
|
|
14
|
+
writeJSON(SAVED_FILE, searches);
|
|
23
15
|
}
|
|
24
16
|
|
|
25
17
|
/** Get all saved searches, sorted by last run time (most recent first). */
|