Skip to content

Instantly share code, notes, and snippets.

@3mcd
Created January 11, 2023 15:26
Show Gist options
  • Save 3mcd/a1bba683cad65d631edae899e45ab466 to your computer and use it in GitHub Desktop.
Save 3mcd/a1bba683cad65d631edae899e45ab466 to your computer and use it in GitHub Desktop.
A node script to convert snake_case to camelCase in all files in a directory
import { resolve, extname } from "path";
import { readdir, readFile, writeFile } from "fs/promises";
async function* getFiles(dir, exclude) {
const dirents = await readdir(dir, { withFileTypes: true });
for (const dirent of dirents) {
const res = resolve(dir, dirent.name);
if (exclude.test(res)) continue;
if (dirent.isDirectory()) {
yield* getFiles(res, exclude);
} else {
yield res;
}
}
}
const camelize = (str) =>
str.replace(/"[^"]+"|(_[a-z])/g, (match, group) => {
if (!group) return match;
else return group[1].toUpperCase();
});
const exclude = /(\.changeset)/g;
for await (const filePath of getFiles(".", exclude)) {
let ext = extname(filePath);
if (ext === ".ts" || ext === ".md") {
let fileContents = await readFile(filePath);
let camelizedFileContents = camelize(fileContents.toString());
await writeFile(filePath, camelizedFileContents);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment