-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.mjs
More file actions
248 lines (222 loc) · 8.16 KB
/
cli.mjs
File metadata and controls
248 lines (222 loc) · 8.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env node
import fs from 'node:fs/promises';
import process from 'node:process';
import { DEFAULT_MAX_FILE_SIZE } from './js/config.js';
function normalizeRepoInput(repoInput) {
const trimmed = (repoInput || '').trim();
if (!trimmed) return { input: trimmed, autoFixed: false, original: trimmed };
const lower = trimmed.toLowerCase();
if (lower.endsWith('.gi') && !lower.endsWith('.git')) {
return {
input: `${trimmed}t`,
autoFixed: true,
original: trimmed
};
}
return { input: trimmed, autoFixed: false, original: trimmed };
}
function printHelp() {
const helpText = [
'Usage:',
' node cli.mjs <repo-input> [options]',
'',
'Repo input:',
' https://github.com/owner/repo',
' owner/repo.git',
'',
'Options:',
' --out <file> Write output to file instead of stdout',
' --service-url <url> Hosted API endpoint URL (or set GIT2DOC_SERVICE_URL)',
' --branch <name> Override branch/tag/sha',
' --max-kb <number> Max file size in KB (default: 250)',
' --no-max-size Disable max size limit on the API call',
' --include-dotfiles Include hidden files/folders',
' --include-data-files Include .csv/.tsv/.log/.env',
' --include-toc Include Markdown table of contents',
' --include-paths <value> Include path patterns (comma/newline list)',
' --exclude-paths <value> Exclude path patterns (comma/newline list)',
' --detect-binary-sample Detect binary files by sampled bytes and skip likely binary',
' --quiet Disable progress logs (stderr)',
' --help Show this help',
'',
'Examples:',
' node cli.mjs vivekreddy9036/APES.git',
' node cli.mjs https://github.com/octocat/Hello-World --out repo.md',
' node cli.mjs owner/repo --service-url https://your-site.pages.dev/api --out repo.md',
' node cli.mjs owner/repo --include-dotfiles --max-kb 1000',
' node cli.mjs owner/repo --include-toc --out repo.md'
].join('\n');
console.log(helpText);
}
function parseArgs(argv) {
const result = {
repoInput: '',
outFile: '',
serviceUrl: '',
branch: '',
includeDotfiles: false,
includeDataFiles: false,
includeToc: false,
includePaths: '',
excludePaths: '',
detectBinaryBySample: false,
noMaxSize: false,
maxFileSize: DEFAULT_MAX_FILE_SIZE,
quiet: false,
help: false
};
const args = [...argv];
while (args.length > 0) {
const current = args.shift();
if (!current) break;
if (!result.repoInput && !current.startsWith('--')) {
result.repoInput = current;
continue;
}
if (current === '--help') {
result.help = true;
continue;
}
if (current === '--quiet') {
result.quiet = true;
continue;
}
if (current === '--include-dotfiles') {
result.includeDotfiles = true;
continue;
}
if (current === '--include-data-files') {
result.includeDataFiles = true;
continue;
}
if (current === '--include-toc') {
result.includeToc = true;
continue;
}
if (current === '--include-paths') {
result.includePaths = args.shift() || '';
continue;
}
if (current === '--exclude-paths') {
result.excludePaths = args.shift() || '';
continue;
}
if (current === '--detect-binary-sample') {
result.detectBinaryBySample = true;
continue;
}
if (current === '--no-max-size') {
result.noMaxSize = true;
continue;
}
if (current === '--out') {
result.outFile = args.shift() || '';
continue;
}
if (current === '--service-url') {
result.serviceUrl = args.shift() || '';
continue;
}
if (current === '--branch') {
result.branch = args.shift() || '';
continue;
}
if (current === '--max-kb') {
const raw = args.shift() || '';
const asNumber = Number(raw);
if (Number.isFinite(asNumber) && asNumber > 0) {
result.maxFileSize = Math.floor(asNumber * 1024);
}
continue;
}
}
return result;
}
function toBooleanString(value) {
return value ? 'true' : 'false';
}
function buildServiceRequestUrl(serviceUrl, args, normalizedRepoInput) {
const url = new URL(serviceUrl);
url.searchParams.set('repo', normalizedRepoInput);
url.searchParams.set('includeDotfiles', toBooleanString(args.includeDotfiles));
url.searchParams.set('includeDataFiles', toBooleanString(args.includeDataFiles));
url.searchParams.set('includeToc', toBooleanString(args.includeToc));
if (args.includePaths) {
url.searchParams.set('includePaths', args.includePaths);
}
if (args.excludePaths) {
url.searchParams.set('excludePaths', args.excludePaths);
}
url.searchParams.set('detectBinaryBySample', toBooleanString(args.detectBinaryBySample));
if (args.noMaxSize) {
url.searchParams.set('noMax', 'true');
} else {
url.searchParams.set('maxKb', String(Math.floor(args.maxFileSize / 1024)));
}
if (args.branch) {
url.searchParams.set('branch', args.branch);
}
return url.toString();
}
async function fetchFromHostedService(args, normalizedRepoInput, log) {
const requestUrl = buildServiceRequestUrl(args.serviceUrl, args, normalizedRepoInput);
log(`Calling hosted service: ${requestUrl}`);
const response = await fetch(requestUrl, {
headers: {
Accept: 'text/markdown, text/plain;q=0.9, application/json;q=0.8'
}
});
const body = await response.text();
if (!response.ok) {
const contentType = String(response.headers.get('content-type') || '').toLowerCase();
if (contentType.includes('application/json')) {
try {
const payload = JSON.parse(body);
const errorText = String(payload?.error || '').trim();
const detailsText = String(payload?.details || '').trim();
const combined = [errorText, detailsText].filter(Boolean).join(' | ');
throw new Error(`Hosted service request failed (${response.status}): ${combined || body.slice(0, 500)}`);
} catch {
throw new Error(`Hosted service request failed (${response.status}): ${body.slice(0, 500)}`);
}
}
throw new Error(`Hosted service request failed (${response.status}): ${body.slice(0, 500)}`);
}
return body;
}
async function main() {
const args = parseArgs(process.argv.slice(2));
if (args.help || !args.repoInput) {
printHelp();
return args.help ? 0 : 1;
}
const normalized = normalizeRepoInput(args.repoInput);
const log = (...messages) => {
if (!args.quiet) {
console.error(...messages);
}
};
if (normalized.autoFixed) {
console.error(`Note: corrected repo input '${normalized.original}' to '${normalized.input}'.`);
}
const serviceUrl = args.serviceUrl || process.env.GIT2DOC_SERVICE_URL || '';
if (!serviceUrl) {
console.error('Missing service URL. Use --service-url <url> or set GIT2DOC_SERVICE_URL.');
return 1;
}
const output = await fetchFromHostedService({ ...args, serviceUrl }, normalized.input, log);
if (args.outFile) {
await fs.writeFile(args.outFile, output, 'utf8');
log(`Saved output to ${args.outFile}`);
} else {
process.stdout.write(output);
}
log('Completed successfully via hosted service.');
return 0;
}
main().catch((error) => {
console.error(`Error: ${error.message}`);
return 1;
}).then((code) => {
process.exitCode = typeof code === 'number' ? code : 1;
});