Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ruoniw committed Sep 12, 2024
1 parent 01913c6 commit 9cfabaf
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions packages/cli/src/commands/add/utils/index-file.ts
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit 9cfabaf

Please sign in to comment.