Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: lowercase directories, not files #42

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
};
Loading