-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename.js
63 lines (52 loc) · 1.9 KB
/
rename.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
const fs = require("fs");
const path = require("path");
// Function to convert a string to CamelCase format
function toCamelCase(str) {
// Remove "-" symbols and split the string into words
const words = str.split("-");
// Convert each word to CamelCase format
const camelCaseWords = words.map((word, index) => {
if (index === 0) {
// The first word remains unchanged
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
} else {
// For the rest of the words, the first letter is in lowercase
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}
});
// Join the words back into a string
const camelCaseStr = camelCaseWords.join("");
return camelCaseStr;
}
// Get the current directory
const rootDir = process.cwd();
// Get the list of files in the current directory
fs.readdir(rootDir, (err, files) => {
if (err) {
console.error("Error reading directory:", err);
return;
}
// Filter the list of files, keeping only files without .js and .ts extensions and not the rename.js script
const filteredFiles = files.filter((file) => {
return (
fs.statSync(path.join(rootDir, file)).isFile() &&
!file.endsWith(".js") &&
!file.endsWith(".ts") &&
file !== "rename.js"
);
});
// Process each file
filteredFiles.forEach((file) => {
// Create a folder with a CamelCase name
const folderName = toCamelCase(file.slice(0, file.lastIndexOf(".")));
const folderPath = path.join(rootDir, folderName);
fs.mkdirSync(folderPath);
// Move the file to this folder
const oldFilePath = path.join(rootDir, file);
const newFilePath = path.join(folderPath, "index.tsx");
fs.renameSync(oldFilePath, newFilePath);
console.log(
`File "${file}" has been moved to folder "${folderName}" and renamed to "index.tsx"`
);
});
});