Created
November 22, 2023 01:21
-
-
Save jsonbytes/6216eb6057cd8ac2421a453dcbcd3743 to your computer and use it in GitHub Desktop.
Generate index.ts files for module exports
This file contains 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 * as fs from "fs/promises"; | |
import * as path from "path"; | |
// usage (compiled) | |
// node dist/index-generator.js /home/jason/Development/project/src/moduledirectory | |
const generateIndex = async (folderPath: string) => { | |
try { | |
const files = await fs.readdir(folderPath); | |
const exportStatements = files | |
.filter((file) => file.endsWith(".ts")) | |
.map((file) => { | |
const fileNameWithoutExtension = path.basename(file, ".ts"); | |
return `export * from "./${fileNameWithoutExtension}";`; | |
}) | |
.join("\n"); | |
const outputFile = path.join(folderPath, "index.ts"); | |
await fs.writeFile(outputFile, exportStatements); | |
console.log(`Created ${outputFile} with export statements.`); | |
} catch (error) { | |
console.error("Error:", error); | |
} | |
}; | |
const main = async () => { | |
const folderPath = process.argv[2]; | |
if (!folderPath) { | |
console.error("Please provide a folder path as a command line argument."); | |
return; | |
} | |
await generateIndex(folderPath); | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment