-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprependFileName.js
56 lines (48 loc) · 1.83 KB
/
prependFileName.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
const fs = require("fs");
const path = require("path");
const directories = [
path.join(__dirname, "src", "app"),
path.join(__dirname, "src", "components"),
path.join(__dirname, "src", "services"),
path.join(__dirname, "src", "contexts"),
path.join(__dirname, "src", "pages"),
];
const commentSyntax = "//"; // Change to appropriate comment syntax for your files
function prependOrReplaceFilenameToFiles(directory) {
fs.readdir(directory, (err, files) => {
if (err) {
return console.log("Unable to scan directory: " + err);
}
files.forEach((file) => {
const filePath = path.join(directory, file);
const fileExtension = path.extname(file);
// Check if the file has a .tsx or .js extension
if (fs.lstatSync(filePath).isDirectory()) {
// Recursively process directories
prependOrReplaceFilenameToFiles(filePath);
} else if (fileExtension === ".tsx" || fileExtension === ".js") {
fs.readFile(filePath, "utf8", (err, data) => {
if (err) throw err;
const relativeFilePath = path.relative(__dirname, filePath);
const commentLine = `${commentSyntax} ${relativeFilePath}`;
const lines = data.split("\n");
// Check if the first line is a file name comment
if (
lines[0].startsWith(commentSyntax) &&
lines[0].includes(path.basename(filePath))
) {
lines[0] = commentLine;
} else {
lines.unshift(commentLine);
}
const updatedData = lines.join("\n");
fs.writeFile(filePath, updatedData, "utf8", (err) => {
if (err) throw err;
console.log(`Updated file path in ${relativeFilePath}`);
});
});
}
});
});
}
directories.forEach((directory) => prependOrReplaceFilenameToFiles(directory));