purvex-ui 1.0.3 ā 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/commands/add.js +79 -32
package/package.json
CHANGED
package/src/commands/add.js
CHANGED
|
@@ -4,77 +4,124 @@ const chalk = require('chalk');
|
|
|
4
4
|
|
|
5
5
|
async function addComponent(componentName, options) {
|
|
6
6
|
try {
|
|
7
|
-
console.log(chalk.blue.bold(
|
|
7
|
+
console.log(chalk.blue.bold(`\\nš Adding ${componentName}...\\n`));
|
|
8
8
|
|
|
9
|
-
// 1.
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
// 1. Cari registry file
|
|
10
|
+
const possiblePaths = [
|
|
11
|
+
path.join(__dirname, '..', 'registry', 'components.json'),
|
|
12
|
+
path.join(__dirname, 'registry', 'components.json'),
|
|
13
|
+
path.join(process.cwd(), 'node_modules', 'purvex-ui', 'registry', 'components.json'),
|
|
14
|
+
path.join(process.cwd(), 'registry', 'components.json')
|
|
15
|
+
];
|
|
13
16
|
|
|
14
|
-
|
|
17
|
+
let registryPath = null;
|
|
18
|
+
for (const p of possiblePaths) {
|
|
19
|
+
if (fs.existsSync(p)) {
|
|
20
|
+
registryPath = p;
|
|
21
|
+
console.log(chalk.gray(`Found registry at: ${p}`));
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (!registryPath) {
|
|
15
27
|
console.log(chalk.red('ā Registry not found'));
|
|
28
|
+
console.log(chalk.yellow('\\nAvailable components:'));
|
|
29
|
+
console.log(chalk.cyan(' ⢠button'));
|
|
30
|
+
console.log(chalk.cyan(' ⢠card'));
|
|
31
|
+
console.log(chalk.cyan(' ⢠avatar'));
|
|
16
32
|
return;
|
|
17
33
|
}
|
|
18
34
|
|
|
35
|
+
// 2. Load registry
|
|
19
36
|
const registryContent = fs.readFileSync(registryPath, 'utf-8');
|
|
20
37
|
const registry = JSON.parse(registryContent);
|
|
21
38
|
|
|
22
|
-
//
|
|
39
|
+
// 3. Cari component
|
|
23
40
|
const component = registry.components.find(c => c.name === componentName);
|
|
24
41
|
|
|
25
42
|
if (!component) {
|
|
26
43
|
console.log(chalk.red(`ā Component "${componentName}" not found`));
|
|
27
|
-
console.log(chalk.yellow('
|
|
44
|
+
console.log(chalk.yellow('\\nAvailable components:'));
|
|
28
45
|
registry.components.forEach(c => {
|
|
29
46
|
console.log(chalk.cyan(` - ${c.name}`));
|
|
30
47
|
});
|
|
31
48
|
return;
|
|
32
49
|
}
|
|
33
50
|
|
|
34
|
-
//
|
|
51
|
+
// 4. Buat target directory
|
|
35
52
|
const userProjectRoot = process.cwd();
|
|
36
53
|
const targetDir = path.join(userProjectRoot, 'src', 'components', 'ui');
|
|
37
54
|
|
|
38
|
-
// Auto create directory jika belum ada
|
|
39
55
|
if (!fs.existsSync(targetDir)) {
|
|
40
56
|
fs.mkdirSync(targetDir, { recursive: true });
|
|
57
|
+
console.log(chalk.green(`ā
Created: ${path.relative(userProjectRoot, targetDir)}`));
|
|
41
58
|
}
|
|
42
59
|
|
|
43
|
-
//
|
|
60
|
+
// 5. Cari templates directory
|
|
61
|
+
let templatesDir = null;
|
|
62
|
+
const possibleTemplateDirs = [
|
|
63
|
+
path.join(__dirname, '..', 'templates'),
|
|
64
|
+
path.join(__dirname, 'templates'),
|
|
65
|
+
path.join(process.cwd(), 'node_modules', 'purvex-ui', 'templates'),
|
|
66
|
+
path.join(process.cwd(), 'templates')
|
|
67
|
+
];
|
|
68
|
+
|
|
69
|
+
for (const dir of possibleTemplateDirs) {
|
|
70
|
+
if (fs.existsSync(dir)) {
|
|
71
|
+
templatesDir = dir;
|
|
72
|
+
console.log(chalk.gray(`Found templates at: ${dir}`));
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (!templatesDir) {
|
|
78
|
+
console.log(chalk.red('ā Templates directory not found'));
|
|
79
|
+
console.log(chalk.yellow('Creating simple component file...'));
|
|
80
|
+
|
|
81
|
+
// Fallback: buat file simple
|
|
82
|
+
const componentContent = `import React from "react";
|
|
83
|
+
import { cn } from "@/lib/utils";
|
|
84
|
+
|
|
85
|
+
export const ${componentName.charAt(0).toUpperCase() + componentName.slice(1)} = ({ className, children, ...props }) => {
|
|
86
|
+
return (
|
|
87
|
+
<div className={cn("${componentName}", className)} {...props}>
|
|
88
|
+
{children}
|
|
89
|
+
</div>
|
|
90
|
+
);
|
|
91
|
+
};`;
|
|
92
|
+
|
|
93
|
+
const targetPath = path.join(targetDir, \`\${componentName}.jsx\`);
|
|
94
|
+
fs.writeFileSync(targetPath, componentContent);
|
|
95
|
+
|
|
96
|
+
console.log(chalk.green(\`ā
Created: \${path.relative(userProjectRoot, targetPath)}\`));
|
|
97
|
+
console.log(chalk.green.bold(\`\\nš Component ${componentName} added!\`));
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// 6. Copy template files
|
|
44
102
|
for (const file of component.files) {
|
|
45
|
-
const sourcePath = path.join(
|
|
103
|
+
const sourcePath = path.join(templatesDir, file);
|
|
46
104
|
const targetPath = path.join(targetDir, path.basename(file));
|
|
47
105
|
|
|
48
106
|
if (!fs.existsSync(sourcePath)) {
|
|
49
|
-
console.log(chalk.yellow(
|
|
107
|
+
console.log(chalk.yellow(\`ā ļø Template \${file} not found\`));
|
|
50
108
|
continue;
|
|
51
109
|
}
|
|
52
110
|
|
|
53
|
-
|
|
54
|
-
let content = fs.readFileSync(sourcePath, 'utf-8');
|
|
55
|
-
|
|
56
|
-
// Update import path untuk src/
|
|
57
|
-
content = content.replace(/@\/lib\/utils/g, '@/lib/utils');
|
|
58
|
-
content = content.replace(/@\/components\/ui/g, '@/components/ui');
|
|
59
|
-
|
|
60
|
-
// Write to target
|
|
111
|
+
const content = fs.readFileSync(sourcePath, 'utf-8');
|
|
61
112
|
fs.writeFileSync(targetPath, content);
|
|
62
113
|
|
|
63
|
-
|
|
64
|
-
console.log(chalk.green('ā
Created: ') + relativePath);
|
|
114
|
+
console.log(chalk.green(\`ā
Created: \${path.relative(userProjectRoot, targetPath)}\`));
|
|
65
115
|
}
|
|
66
116
|
|
|
67
|
-
//
|
|
68
|
-
console.log(chalk.green.bold(
|
|
69
|
-
console.log(chalk.white('
|
|
70
|
-
console.log(chalk.cyan(
|
|
71
|
-
|
|
72
|
-
console.log(chalk.white('\nStart your dev server:'));
|
|
73
|
-
console.log(chalk.cyan(' npm run dev'));
|
|
117
|
+
// 7. Success message
|
|
118
|
+
console.log(chalk.green.bold(\`\\nš Component \${componentName} added successfully!\`));
|
|
119
|
+
console.log(chalk.white('\\nUsage:'));
|
|
120
|
+
console.log(chalk.cyan(\` import { \${componentName.charAt(0).toUpperCase() + componentName.slice(1)} } from "@/components/ui/\${componentName}";\`));
|
|
74
121
|
|
|
75
122
|
} catch (error) {
|
|
76
|
-
console.error(chalk.red('ā Error
|
|
123
|
+
console.error(chalk.red('ā Error:'), error.message);
|
|
77
124
|
}
|
|
78
125
|
}
|
|
79
126
|
|
|
80
|
-
module.exports = { addComponent };
|
|
127
|
+
module.exports = { addComponent };
|