diff --git a/README.md b/README.md index 745f8c3..d6ef172 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ The action is designed to be modular, with multiple plugins to choose from for e | `hide-curriculum-strings` | This plugin runs through all of the strings in the specified project and hides them if they meet specific conditions. | none | | `hide-renpy-strings` | This plugin runs through all of the strings in the specified project and hides them if they meet specific conditions. | none | | `hide-string` | Looks for a specific string in a specific file and marks it as hidden. | `FILE_NAME`, `STRING_CONTENT` | -| `lowercase-directories` | This plugin will walk through the `FILE_PATHS` directories and make sure all directories are set to lowercase | `FILE_PATHS` | | +| `lowercase-directories` | This plugin will walk through the `FILE_PATH` directory and make sure all sub-directories are set to lowercase | `FILE_PATHS` | | | `pull-request` | Creates a pull request from the specified branch and targets the specified base, or main by default. | `GH_TOKEN`, `BRANCH`, `REPOSITORY`, `BASE`, `TITLE`, `BODY` | | `remove-deleted-files` | This will recursively walk through the `FILE_PATHS` directories and remove any files from Crowdin that are no longer present in the repo. Note that `FILE_PATHS` should be a stringified array, as GitHub `yaml` doesn't accept array values: `'["curriculum/challenges/english", "curriculum/dictionaries/english"]'` | `FILE_PATHS` | | `unhide-string` | Looks for a specific string in a specific file and marks it as visible. | `FILE_NAME`, `STRING_CONTENT` | diff --git a/src/index.ts b/src/index.ts index 5f0db32..a2b3c9b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -83,11 +83,11 @@ import { validateEnvironment } from "./utils/validate-environment"; ); break; case "lowercase-directories": - if (!process.env.FILE_PATHS) { + if (!process.env.FILE_PATH) { setFailed("Missing file paths."); break; } - lowercaseDirectories(JSON.parse(process.env.FILE_PATHS)); + lowercaseDirectories(JSON.parse(process.env.FILE_PATH)); break; case "pull-request": if ( diff --git a/src/plugins/lowercase-directories.ts b/src/plugins/lowercase-directories.ts index ac1f8c8..5a34150 100644 --- a/src/plugins/lowercase-directories.ts +++ b/src/plugins/lowercase-directories.ts @@ -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 { + 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); };