Skip to content

Instantly share code, notes, and snippets.

@joeskeen
Created February 12, 2024 16:25
Show Gist options
  • Save joeskeen/ebe085b0a3d86c8b10a4a2a2510c4cf9 to your computer and use it in GitHub Desktop.
Save joeskeen/ebe085b0a3d86c8b10a4a2a2510c4cf9 to your computer and use it in GitHub Desktop.
Name the default exports of modules
import { readFileSync, writeFileSync } from "fs";
import glob from "glob";
import { pascalCase } from "change-case";
const defaultExportPattern = /export default \{/;
glob.sync("lib/**/index.js").forEach((file) => {
const fileContents = readFileSync(file, "utf-8").toString();
if (!defaultExportPattern.test(fileContents)) {
return;
}
const parts = file.split("/");
const dir = parts.at(-2);
const moduleName = pascalCase(`${dir}-module`);
const newContents =
fileContents.replace(
defaultExportPattern,
`export const ${moduleName} = {`
) + `\nexport default ${moduleName};`;
writeFileSync(file, newContents, "utf-8");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment