Created
February 12, 2024 16:25
-
-
Save joeskeen/ebe085b0a3d86c8b10a4a2a2510c4cf9 to your computer and use it in GitHub Desktop.
Name the default exports of modules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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