-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make the lowercase-directories plugin recursive (#31)
Co-authored-by: Naomi <[email protected]>
- Loading branch information
Showing
3 changed files
with
46 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,49 @@ | ||
import { rename } from "fs/promises"; | ||
import { Dirent } from "fs"; | ||
import { readdir, rename } from "fs/promises"; | ||
import { join } from "path"; | ||
|
||
async function readAllFiles(dir: string): Promise<string[]> { | ||
const files: Dirent[] = await readdir(dir, { withFileTypes: true }); | ||
let allFiles: string[] = []; | ||
for (const file of files) { | ||
const filePath = join(dir, file.name); | ||
if (file.isDirectory()) { | ||
const subFiles = await readAllFiles(filePath); | ||
allFiles = allFiles.concat(subFiles); | ||
} else { | ||
allFiles.push(filePath); | ||
} | ||
} | ||
return allFiles; | ||
} | ||
|
||
// Recursive function to process directories and subdirectories | ||
const processDirectory = async (dirPath: string) => { | ||
// Get all files recursively | ||
const allFiles = await readAllFiles(dirPath); | ||
|
||
// Lowercase directory names | ||
for (const filePath of allFiles) { | ||
const dirPath = filePath.split("/").slice(0, -1).join("/"); | ||
const dirName = filePath.split("/").pop(); | ||
const lowercasedDirName = dirName?.toLowerCase(); | ||
if (lowercasedDirName === undefined) { | ||
throw new Error("Lowercased DirName Required"); | ||
} | ||
|
||
if (lowercasedDirName !== dirName) { | ||
const newPath = join(dirPath, lowercasedDirName); | ||
console.log(`Renaming directory ${filePath} to ${newPath}`); | ||
await rename(filePath, newPath); | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* | ||
* @param {string[]} directories The directories that must be sorted through. | ||
* @param {string} directory The directory tree that must be sorted through. | ||
*/ | ||
export const lowercaseDirectories = async (directories: string[]) => { | ||
console.info("Getting file list..."); | ||
for (const directory of directories) { | ||
if (directory.toLocaleLowerCase() !== directory) { | ||
const oldPath = join(process.cwd(), directory); | ||
const newPath = join(process.cwd(), directory.toLocaleLowerCase()); | ||
console.log(`Renaming ${directory}`); | ||
await rename(oldPath, newPath); | ||
} | ||
} | ||
export const lowercaseDirectories = (directory: string) => { | ||
console.info("Getting directory list..."); | ||
processDirectory(directory); | ||
}; |