#!/usr/bin/env node
import {
generateIocComponentFiles,
validateComponentName,
} from "../tools/generate_ioc_component.js";
import * as fs from "fs";
import * as path from "path";
import { fileURLToPath } from "url";
// Get CLI arguments
const args = process.argv.slice(2);
const command = args[0];
const componentName = args[1];
// Show help if no args or invalid command
if (command !== "create" || !componentName) {
console.log(`
Usage: npm run ioc create [ComponentName]
Arguments:
componentName The name of the component to create (must be PascalCase, e.g., ProductList)
Example:
npm run ioc create ProductCard
`);
process.exit(1);
}
// Validate component name
const validation = validateComponentName(componentName);
if (!validation.valid) {
console.error(`\n❌ Error: ${validation.message}`);
process.exit(1);
}
console.log(`\n🚀 Generating component: ${componentName}...`);
// Generate file contents
const files = generateIocComponentFiles({
componentName,
displayName: componentName, // Use component name as default display name
description: "Component generated via CLI",
author: "developer",
});
// Determine target directory
// Priority: ./src/components/card_components -> ./components/card_components
const cwd = process.cwd();
let targetBaseDir = path.join(cwd, "components", "card_components");
if (fs.existsSync(path.join(cwd, "src"))) {
targetBaseDir = path.join(cwd, "src", "components", "card_components");
}
const componentDir = path.join(targetBaseDir, componentName);
// Create directory
if (!fs.existsSync(componentDir)) {
fs.mkdirSync(componentDir, { recursive: true });
console.log(`Created directory: ${componentDir}`);
} else {
console.log(`Directory already exists: ${componentDir}`);
}
// Write files
files.forEach((file) => {
const filePath = path.join(componentDir, file.path);
const dir = path.dirname(filePath);
// Ensure subdirectory exists
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.writeFileSync(filePath, file.content);
console.log(`✅ Created: ${path.relative(cwd, filePath)}`);
});
console.log(`\n✨ Component ${componentName} created successfully!\n`);