Skip to content

Commit

Permalink
fix: lowercase directories, not files
Browse files Browse the repository at this point in the history
  • Loading branch information
ojeytonwilliams committed Jun 11, 2024
1 parent fdd89a4 commit 2dffc90
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 60 deletions.
37 changes: 8 additions & 29 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40584,49 +40584,28 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.lowercaseDirectories = void 0;
const promises_1 = __nccwpck_require__(3292);
const path_1 = __nccwpck_require__(1017);
function readAllFiles(dir) {
// Recursive function to process directories and subdirectories
function lowerCaseSubDirectories(dir) {
return __awaiter(this, void 0, void 0, function* () {
const files = yield (0, promises_1.readdir)(dir, { withFileTypes: true });
let allFiles = [];
for (const file of files) {
const filePath = (0, path_1.join)(dir, file.name);
if (file.isDirectory()) {
const subFiles = yield readAllFiles(filePath);
allFiles = allFiles.concat(subFiles);
}
else {
allFiles.push(filePath);
const filePath = (0, path_1.join)(dir, file.name);
const newFilePath = (0, path_1.join)(dir, file.name.toLowerCase());
console.log(`Renaming directory ${filePath} to ${newFilePath}`);
yield (0, promises_1.rename)(filePath, newFilePath);
yield lowerCaseSubDirectories(newFilePath);
}
}
return allFiles;
});
}
// Recursive function to process directories and subdirectories
const processDirectory = (dirPath) => __awaiter(void 0, void 0, void 0, function* () {
// Get all files recursively
const allFiles = yield 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 === null || dirName === void 0 ? void 0 : dirName.toLowerCase();
if (lowercasedDirName === undefined) {
throw new Error("Lowercased DirName Required");
}
if (lowercasedDirName !== dirName) {
const newPath = (0, path_1.join)(dirPath, lowercasedDirName);
console.log(`Renaming directory ${filePath} to ${newPath}`);
yield (0, promises_1.rename)(filePath, newPath);
}
}
});
/**
*
* @param {string} directory The directory tree that must be sorted through.
*/
const lowercaseDirectories = (directory) => {
console.info("Getting directory list...");
processDirectory(directory);
lowerCaseSubDirectories(directory);
};
exports.lowercaseDirectories = lowercaseDirectories;

Expand Down
40 changes: 9 additions & 31 deletions src/plugins/lowercase-directories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,26 @@ import { Dirent } from "fs";
import { readdir, rename } from "fs/promises";
import { join } from "path";

async function readAllFiles(dir: string): Promise<string[]> {
// Recursive function to process directories and subdirectories
async function lowerCaseSubDirectories(dir: string): Promise<void> {
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");
}
const filePath = join(dir, file.name);
const newFilePath = join(dir, file.name.toLowerCase());

if (lowercasedDirName !== dirName) {
const newPath = join(dirPath, lowercasedDirName);
console.log(`Renaming directory ${filePath} to ${newPath}`);
await rename(filePath, newPath);
console.log(`Renaming directory ${filePath} to ${newFilePath}`);
await rename(filePath, newFilePath);
await lowerCaseSubDirectories(newFilePath);
}
}
};
}

/**
*
* @param {string} directory The directory tree that must be sorted through.
*/
export const lowercaseDirectories = (directory: string) => {
console.info("Getting directory list...");
processDirectory(directory);
lowerCaseSubDirectories(directory);
};

0 comments on commit 2dffc90

Please sign in to comment.