Skip to content

Commit

Permalink
feat: make the lowercase-directories plugin recursive (#31)
Browse files Browse the repository at this point in the history
Co-authored-by: Naomi <[email protected]>
  • Loading branch information
a2937 and Naomi authored Apr 17, 2024
1 parent 004514b commit 007eb2e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
55 changes: 43 additions & 12 deletions src/plugins/lowercase-directories.ts
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);
};

0 comments on commit 007eb2e

Please sign in to comment.