-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport-files.js
64 lines (62 loc) · 2.22 KB
/
export-files.js
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
const fs = require('fs');
const path = require('path');
const mime = require('mime-types');
function walkSync(dir, filelist = [], depth = 0) {
const files = fs.readdirSync(dir);
for (const file of files) {
const filePath = path.join(dir, file);
if (fs.statSync(filePath).isDirectory()) {
if (file === 'node_modules' || file.startsWith('.git')) {
continue; // Skip node_modules and Git-related directories
}
filelist.push({
name: `${'| '.repeat(depth)}${file}/`,
path: filePath
});
walkSync(filePath, filelist, depth + 1);
} else {
const mimeType = mime.lookup(filePath);
if (mimeType && mimeType.startsWith('text/')) {
// If the file contains plain text, add its contents to the output file
const content = fs.readFileSync(filePath, 'utf8');
const placeholderContent = `[ ${file} ]\n${content}\n---\n`;
filelist.push({
name: `${'| '.repeat(depth + 1)}${file}`,
path: filePath,
content: placeholderContent
});
} else if (file.match(/^\.env.*/)) {
// If the file is an .env file, copy the variable names and replace the contents with [PLACEHOLDER]
const content = fs.readFileSync(filePath, 'utf8');
const variableNames = content.match(/^\w+/gm) || [];
const placeholderContent = variableNames.map(name => `${name}=[PLACEHOLDER]`).join('\n');
filelist.push({
name: `${'| '.repeat(depth + 1)}${file}`,
path: filePath,
content: placeholderContent
});
} else {
filelist.push({
name: `${'| '.repeat(depth + 1)}${file}`,
path: filePath
});
}
}
}
return filelist;
}
const files = walkSync('C:\\Users\\max72\\Documents\\GitHub\\AnmaSoftV3');
let text = '';
for (const file of files) {
text += `${file.name}\n`;
if (file.content) {
text += `${file.content}\n`;
} else if (file.path && fs.statSync(file.path).isFile()) { // Check if file.path is a file
const contents = fs.readFileSync(file.path, 'utf8');
text += `${contents}\n---\n`;
}
}
fs.writeFile('file-list.txt', text, (err) => {
if (err) throw err;
console.log('The file list has been saved to file-list.txt');
});