-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
403 lines (382 loc) · 11.4 KB
/
index.js
File metadata and controls
403 lines (382 loc) · 11.4 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/usr/bin/env node
import inquirer from 'inquirer';
import simpleGit from 'simple-git';
import fs from 'fs';
import path from 'path';
import { create } from 'xmlbuilder2';
import { fileURLToPath } from 'url';
import { isBinaryFileSync } from 'isbinaryfile';
import os from 'os';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Default exclusions
const DEFAULT_EXCLUDES = ['node_modules', '.git', '.DS_Store'];
const DEFAULT_FILE_SIZE_LIMIT = 1024 * 1024; // 1MB
// Enhanced logger
const log = {
info: (msg) => console.log(`\x1b[36m[INFO]\x1b[0m ${msg}`),
warn: (msg) => console.warn(`\x1b[33m[WARN]\x1b[0m ${msg}`),
error: (msg) => console.error(`\x1b[31m[ERROR]\x1b[0m ${msg}`)
};
async function promptRepoSource() {
const { source } = await inquirer.prompt([
{
type: 'list',
name: 'source',
message: 'Would you like to clone a GitHub repo or use a local path?',
choices: ['Clone from GitHub', 'Use local path'],
},
]);
return source;
}
async function promptRepoUrl() {
const { url } = await inquirer.prompt([
{
type: 'input',
name: 'url',
message: 'Enter the GitHub repository URL:',
},
]);
return url;
}
async function promptAuth() {
const { needsAuth } = await inquirer.prompt([
{
type: 'confirm',
name: 'needsAuth',
message: 'Is this a private repository (requires authentication)?',
default: false,
},
]);
if (!needsAuth) return null;
const { username, token } = await inquirer.prompt([
{ type: 'input', name: 'username', message: 'GitHub username:' },
{ type: 'password', name: 'token', message: 'GitHub personal access token:' },
]);
return { username, token };
}
async function promptLocalPath() {
const { localPath } = await inquirer.prompt([
{
type: 'input',
name: 'localPath',
message: 'Enter the local path to the repository:',
validate: input => fs.existsSync(input) ? true : 'Path does not exist.'
},
]);
return localPath;
}
async function promptExcludes(defaults) {
const { excludes } = await inquirer.prompt([
{
type: 'checkbox',
name: 'excludes',
message: 'Select folders/files to exclude from XML:',
choices: defaults,
default: defaults,
},
]);
return excludes;
}
async function promptFileSizeLimit() {
const { fileSizeLimit } = await inquirer.prompt([
{
type: 'input',
name: 'fileSizeLimit',
message: 'Enter max file size to include (in bytes, default 1048576 = 1MB):',
default: DEFAULT_FILE_SIZE_LIMIT,
validate: input => isNaN(Number(input)) || Number(input) <= 0 ? 'Enter a positive number.' : true
},
]);
return Number(fileSizeLimit);
}
async function promptOutputFormat() {
const { format } = await inquirer.prompt([
{
type: 'list',
name: 'format',
message: 'Select output format:',
choices: ['XML', 'TXT'],
default: 'XML',
},
]);
return format;
}
function walkDir(dir, excludes, fileSizeLimit, files = []) {
let dirEntries;
try {
dirEntries = fs.readdirSync(dir);
} catch (err) {
log.error(`Failed to read directory: ${dir} (${err.message})`);
return files;
}
for (const file of dirEntries) {
const fullPath = path.join(dir, file);
const relPath = path.relative(process.cwd(), fullPath);
if (excludes.some(ex => relPath.includes(ex))) {
log.info(`Excluded: ${relPath}`);
continue;
}
let stat;
try {
stat = fs.statSync(fullPath);
} catch (err) {
log.error(`Failed to stat: ${relPath} (${err.message})`);
continue;
}
if (stat.isDirectory()) {
files.push({ type: 'directory', name: file, path: relPath, children: walkDir(fullPath, excludes, fileSizeLimit, []) });
} else {
if (stat.size > fileSizeLimit) {
log.warn(`Skipped (too large): ${relPath} (${stat.size} bytes)`);
continue;
}
let isBinary = false;
try {
isBinary = isBinaryFileSync(fullPath);
} catch (err) {
log.error(`Binary check failed: ${relPath} (${err.message})`);
continue;
}
if (isBinary) {
log.warn(`Skipped (binary): ${relPath}`);
continue;
}
let content = '';
try {
content = fs.readFileSync(fullPath, 'utf8');
} catch (err) {
log.error(`Failed to read: ${relPath} (${err.message})`);
continue;
}
files.push({ type: 'file', name: file, path: relPath, content });
}
}
return files;
}
function buildXmlTree(files) {
function buildNode(node, xmlParent) {
if (node.type === 'directory') {
const dirElem = xmlParent.ele('directory', { name: node.name, path: node.path });
node.children.forEach(child => buildNode(child, dirElem));
} else {
xmlParent.ele('file', { name: node.name, path: node.path }).txt(node.content);
}
}
const root = create({ version: '1.0', encoding: 'UTF-8' }).ele('repository');
files.forEach(f => buildNode(f, root));
return root.end({ prettyPrint: true });
}
function buildTxtOutput(files, output = [], parentPath = '') {
for (const node of files) {
if (node.type === 'directory') {
buildTxtOutput(node.children, output, path.join(parentPath, node.name));
} else {
output.push(`--- FILE: ${path.join(parentPath, node.name)} ---\n`);
output.push(node.content + '\n');
}
}
return output.join('');
}
function walkDirFlat(dir, excludes, fileSizeLimit, files = [], parentPath = '') {
let dirEntries;
try {
dirEntries = fs.readdirSync(dir);
} catch (err) {
return files;
}
for (const file of dirEntries) {
const fullPath = path.join(dir, file);
const relPath = path.join(parentPath, file);
if (excludes.some(ex => relPath.includes(ex))) continue;
let stat;
try {
stat = fs.statSync(fullPath);
} catch (err) {
continue;
}
if (stat.isDirectory()) {
walkDirFlat(fullPath, excludes, fileSizeLimit, files, relPath);
} else {
if (stat.size > fileSizeLimit) continue;
let isBinary = false;
try {
isBinary = isBinaryFileSync(fullPath);
} catch (err) {
continue;
}
if (isBinary) continue;
let content = '';
try {
content = fs.readFileSync(fullPath, 'utf8');
} catch (err) {
continue;
}
files.push({ name: file, path: relPath, content });
}
}
return files;
}
function buildTxtOutputFlat(files) {
let output = [];
for (const node of files) {
output.push(`--- FILE: ${node.path} ---\n`);
output.push(node.content + '\n');
}
return output.join('');
}
async function nonInteractiveFlatten() {
const cwd = process.cwd();
const files = walkDirFlat(cwd, DEFAULT_EXCLUDES, DEFAULT_FILE_SIZE_LIMIT);
const output = buildTxtOutputFlat(files);
const outPath = path.join(cwd, 'repo.txt');
try {
fs.writeFileSync(outPath, output, 'utf8');
console.log('\x1b[32mSuccess!\x1b[0m repo.txt has been generated in this directory.');
} catch (err) {
console.error(`Failed to write repo.txt: ${err.message}`);
}
}
async function cliEntry() {
// Check for --flat or -f argument for non-interactive mode
const args = process.argv.slice(2);
if (args.includes('--flat') || args.includes('-f')) {
await nonInteractiveFlatten();
return;
}
// Otherwise, prompt user for mode
const { mode } = await inquirer.prompt([
{
type: 'list',
name: 'mode',
message: 'How would you like to run RepoToXML?',
choices: [
{ name: 'Flatten this directory to repo.txt (no prompts)', value: 'flat' },
{ name: 'Interactive mode (prompts, XML/TXT, etc.)', value: 'interactive' }
],
default: 'flat'
}
]);
if (mode === 'flat') {
await nonInteractiveFlatten();
} else {
await runApp();
}
}
async function main() {
try {
const source = await promptRepoSource();
let repoPath = '';
if (source === 'Clone from GitHub') {
const url = await promptRepoUrl();
const auth = await promptAuth();
const tempDir = path.join(process.cwd(), 'temp_repo');
if (fs.existsSync(tempDir)) fs.rmSync(tempDir, { recursive: true, force: true });
const git = simpleGit();
let cloneUrl = url;
if (auth) {
try {
const urlObj = new URL(url);
urlObj.username = auth.username;
urlObj.password = auth.token;
cloneUrl = urlObj.toString();
} catch (err) {
log.error(`Invalid URL: ${url} (${err.message})`);
return;
}
}
try {
log.info(`Cloning repository...`);
await git.clone(cloneUrl, tempDir);
} catch (err) {
log.error(`Git clone failed: ${err.message}`);
return;
}
repoPath = tempDir;
} else {
repoPath = await promptLocalPath();
}
const excludes = await promptExcludes(DEFAULT_EXCLUDES);
const fileSizeLimit = await promptFileSizeLimit();
log.info(`Walking directory and building file list...`);
const files = walkDir(repoPath, excludes, fileSizeLimit);
const outputFormat = await promptOutputFormat();
let outputData;
if (outputFormat === 'TXT') {
log.info('Building TXT output...');
outputData = buildTxtOutput(files);
} else {
log.info('Building XML tree...');
outputData = buildXmlTree(files);
}
// Prompt for output directory: Desktop or custom
const { outDirChoice } = await inquirer.prompt([
{
type: 'list',
name: 'outDirChoice',
message: 'Where do you want to save the output file?',
choices: [
{ name: "Desktop", value: "desktop" },
{ name: "Specify a directory", value: "custom" }
],
default: 'desktop'
}
]);
let outDir;
if (outDirChoice === 'desktop') {
outDir = path.join(os.homedir(), 'Desktop');
} else {
const { customDir } = await inquirer.prompt([
{
type: 'input',
name: 'customDir',
message: 'Enter the output directory for the output file:',
default: process.cwd()
}
]);
outDir = customDir;
}
const { outFile } = await inquirer.prompt([
{ type: 'input', name: 'outFile', message: `Enter output ${outputFormat} filename:`, default: outputFormat === 'TXT' ? 'repo.txt' : 'repo.xml' }
]);
const outPath = path.join(outDir, outFile);
try {
fs.writeFileSync(outPath, outputData, 'utf8');
log.info(`${outputFormat} file written to ${outPath}`);
console.log('\x1b[32mSuccess!\x1b[0m Your file has been generated.');
} catch (err) {
log.error(`Failed to write output: ${err.message}`);
}
if (repoPath.endsWith('temp_repo')) {
try {
fs.rmSync(repoPath, { recursive: true, force: true });
log.info(`Cleaned up temporary repo directory.`);
} catch (err) {
log.warn(`Failed to clean up temp repo: ${err.message}`);
}
}
} catch (err) {
log.error(`Fatal error: ${err.message}`);
}
}
async function runApp() {
let again = true;
while (again) {
await main();
const { nextAction } = await inquirer.prompt([
{
type: 'list',
name: 'nextAction',
message: 'What would you like to do next?',
choices: [
{ name: 'Transform another repo', value: 'again' },
{ name: 'Exit', value: 'exit' }
],
default: 'exit'
}
]);
again = nextAction === 'again';
}
console.log('Goodbye!');
}
cliEntry();