main
cjs 114 lines 3.25 KB
Raw
1 #!/usr/bin/env node
2
3 const fs = require("fs");
4 const path = require("path");
5 const { execFileSync } = require("child_process");
6
7 /**
8 * shadcn 컴포넌트를 자동으로 업데이트하는 스크립트
9 * components.json 파일의 aliases.components 경로를 읽어서
10 * 해당 경로의 ui 폴더 안에 있는 모든 컴포넌트를 업데이트합니다.
11 */
12
13 function main() {
14 try {
15 // components.json 파일 읽기
16 const componentsJsonPath = path.join(process.cwd(), "components.json");
17
18 if (!fs.existsSync(componentsJsonPath)) {
19 console.error("❌ components.json 파일을 찾을 수 없습니다.");
20 process.exit(1);
21 }
22
23 const componentsConfig = JSON.parse(
24 fs.readFileSync(componentsJsonPath, "utf8")
25 );
26
27 // aliases.components 경로 가져오기
28 const componentsPath = componentsConfig.aliases?.components;
29 if (!componentsPath) {
30 console.error(
31 "❌ components.json에서 aliases.components 경로를 찾을 수 없습니다."
32 );
33 process.exit(1);
34 }
35
36 // ui 폴더 경로 구성
37 const uiPath = path.join(
38 process.cwd(),
39 "src",
40 componentsPath.replace("@/", ""),
41 "ui"
42 );
43
44 if (!fs.existsSync(uiPath)) {
45 console.error(`❌ UI 컴포넌트 폴더를 찾을 수 없습니다: ${uiPath}`);
46 process.exit(1);
47 }
48
49 // ui 폴더의 모든 .tsx 파일 찾기
50 const files = fs
51 .readdirSync(uiPath)
52 .filter((file) => file.endsWith(".tsx"))
53 .map((file) => path.basename(file, ".tsx"));
54
55 if (files.length === 0) {
56 console.log("📁 ui 폴더에서 .tsx 파일을 찾을 수 없습니다.");
57 return;
58 }
59
60 console.log(`🔍 발견된 shadcn 컴포넌트들 (${files.length}개):`);
61 files.forEach((file) => console.log(` - ${file}`));
62 console.log("");
63
64 // 각 컴포넌트 업데이트
65 let successCount = 0;
66 let failCount = 0;
67
68 for (const componentName of files) {
69 try {
70 console.log(`🔄 업데이트 중: ${componentName}...`);
71
72 if (!/^[a-z0-9_-]+$/i.test(componentName)) {
73 throw new Error(`invalid component name: ${componentName}`);
74 }
75
76 execFileSync(
77 "npx",
78 ["shadcn@latest", "add", "-o", "-y", componentName],
79 {
80 stdio: "pipe",
81 encoding: "utf8",
82 env: { ...process.env, npm_config_legacy_peer_deps: "true" },
83 }
84 );
85
86 console.log(`✅ ${componentName} 업데이트 완료`);
87 successCount++;
88 } catch (error) {
89 console.error(`❌ ${componentName} 업데이트 실패:`, error.message);
90 failCount++;
91 }
92 }
93
94 console.log("");
95 console.log("📊 업데이트 결과:");
96 console.log(` ✅ 성공: ${successCount}개`);
97 console.log(` ❌ 실패: ${failCount}개`);
98 console.log(` 📁 총 컴포넌트: ${files.length}개`);
99
100 if (failCount === 0) {
101 console.log("🎉 모든 shadcn 컴포넌트가 성공적으로 업데이트되었습니다!");
102 }
103 } catch (error) {
104 console.error("❌ 스크립트 실행 중 오류가 발생했습니다:", error.message);
105 process.exit(1);
106 }
107 }
108
109 // 스크립트가 직접 실행될 때만 main 함수 호출
110 if (require.main === module) {
111 main();
112 }
113
114 module.exports = { main };