@satu-ui/react-ts
Version:
Komponen SatuUI untuk React + TypeScript
64 lines (50 loc) • 1.81 kB
JavaScript
import { writeFile, mkdir, access } from "fs/promises";
import fs from "fs"; // untuk fs.existsSync()
import path from "path";
import { fileURLToPath } from "url";
import https from "https";
// Ambil argumen CLI
const args = process.argv.slice(2); // contoh: ['get', 'Button']
const command = args[0];
const component = args[1];
if (command !== "get" || !component) {
console.log("❌ Gunakan perintah: satu-ui get <component>");
process.exit(1);
}
// Ganti ini sesuai akun & repo GitHub kamu:
const githubUser = "diarcode11";
const githubRepo = "satu-ui";
const githubBranch = "main";
// Buat URL mentah GitHub
const fileUrl = `https://raw.githubusercontent.com/${githubUser}/${githubRepo}/${githubBranch}/packages/react-ts/components/${component}.tsx`;
// Tentukan path output lokal
const targetDir = path.resolve("src/components");
const targetFile = path.join(targetDir, `${component}.tsx`);
// Fungsi download file
function downloadFile(url) {
return new Promise((resolve, reject) => {
https.get(url, (res) => {
if (res.statusCode !== 200) {
return reject(new Error(`HTTP ${res.statusCode} - ${res.statusMessage}`));
}
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => resolve(data));
}).on("error", reject);
});
}
try {
const content = await downloadFile(fileUrl);
if (!fs.existsSync(targetDir)) {
await mkdir(targetDir, { recursive: true });
}
await writeFile(targetFile, content);
console.log(`✅ Komponen "${component}" berhasil di-clone ke ${targetFile}`);
} catch (err) {
console.error(`❌ Gagal mengunduh komponen "${component}"`);
console.error(err.message);
process.exit(1);
}