Skip to content

Commit c8ecaf7

Browse files
authored
feat: automatically generate index.tsx file (#6)
Automatically generate the `index.tsx` file in the ui package when a new component is added.
1 parent 7cad0de commit c8ecaf7

File tree

6 files changed

+51
-26
lines changed

6 files changed

+51
-26
lines changed

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
auto-install-peers = true
2+
enable-pre-post-scripts=true # Enable pre/post scripts (for postui:add)

packages/ui/index.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
import * as React from 'react';
2-
3-
// Components export
4-
export * from '@ui/components/button';
1+
export * from './components/ui/button';

packages/ui/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
"license": "MIT",
77
"scripts": {
88
"lint": "eslint \"**/*.ts*\"",
9-
"ui:add": "pnpm dlx shadcn-ui@latest add"
9+
"ui:add": "pnpm dlx shadcn-ui@latest add",
10+
"postui:add": "ts-node --esm ./scripts/generateIndex.mts"
1011
},
1112
"devDependencies": {
13+
"@types/node": "^17.0.12",
1214
"@types/react": "^18.2.0",
1315
"@types/react-dom": "^18.2.0",
1416
"autoprefixer": "^10.4.14",

packages/ui/scripts/generateIndex.mts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import fs from "fs";
2+
import path from "path";
3+
import { fileURLToPath } from "url";
4+
5+
// Get the current file's directory path
6+
const __filename = fileURLToPath(import.meta.url);
7+
const __dirname = path.dirname(__filename);
8+
9+
const componentsPath = path.join(__dirname, "../components/ui");
10+
const indexFilePath = path.join(componentsPath, "../../index.tsx");
11+
12+
// Generate the index.tsx file by looping through all the files in the components folder
13+
const componentFiles = fs.readdirSync(componentsPath);
14+
const exportStatements = componentFiles.map((file) => {
15+
const componentName = path.basename(file, ".tsx");
16+
return `export * from './components/ui/${componentName}';`;
17+
});
18+
const indexFileContent = exportStatements.join("\n") + "\n";
19+
20+
fs.writeFileSync(indexFilePath, indexFileContent, "utf8");
21+
22+
console.log("\x1b[32m%s\x1b[0m", "index.tsx file generated successfully.");

pnpm-lock.yaml

Lines changed: 24 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)