diff --git a/packages/cli/src/commands/add/utils/index-file.ts b/packages/cli/src/commands/add/utils/index-file.ts index 677619a..7b130d3 100644 --- a/packages/cli/src/commands/add/utils/index-file.ts +++ b/packages/cli/src/commands/add/utils/index-file.ts @@ -1,20 +1,10 @@ -import { writeFileSync } from "node:fs"; -import { readdir, rm, unlink } from "node:fs/promises"; +import { readdir, rm, unlink, writeFile } from "node:fs/promises"; import { basename, extname, join } from "node:path"; +import { camel } from "@/helpers/strings/camel.ts"; +import { pascal } from "@/helpers/strings/pascal.ts"; import { alphabetical } from "@atmx-org/registry/helpers/arrays/alphabetical.ts"; -function convertToFunctionName(fileName: string, isTypesRegistry: boolean): string { - const words = fileName.split('-'); - - return words.map((word, index) => { - if (index === 0 && !isTypesRegistry) { - return word; - } - return word.charAt(0).toUpperCase() + word.slice(1); - }).join(''); -} - export async function generateIndexFile(registryPath: string, isESM: boolean, isTs: boolean) { const files: string[] = await readdir(registryPath); @@ -24,10 +14,11 @@ export async function generateIndexFile(registryPath: string, isESM: boolean, is ); const exportExtension = isESM ? '.js' : ''; - const exports = validFiles.map((file) => { + const typePrefix = registryPath.includes('types') ? 'type ' : ''; + const exports = validFiles.map((file) => { const fileName = basename(file, extname(file)); - const functionName = convertToFunctionName(fileName, registryPath.includes('types')); - return `export { ${functionName} } from './${fileName}${exportExtension}';`; + const functionName = registryPath.includes('types') ? pascal(fileName) : camel(fileName) + return `export { ${typePrefix}${functionName} } from './${fileName}${exportExtension}';`; }); // Sort the exports alphabetically and join them into a string @@ -37,7 +28,7 @@ export async function generateIndexFile(registryPath: string, isESM: boolean, is (v) => v.match(/\{ (\w+) \}/)?.[1].toLowerCase() ?? v, ).join("\n") + "\n"; - writeFileSync(join(registryPath, "index" + fileExtension), indexContent); + await writeFile(join(registryPath, "index" + fileExtension), indexContent); } export async function deleteIndexFile(registryPath: string, isTs: boolean) {