@doom-ui/cli
Version:
CLI for Doom UI
131 lines (122 loc) • 4.94 kB
JavaScript
// src/index.ts
import { Command } from "commander";
// src/commands/init.ts
import path from "node:path";
import fs from "fs-extra";
import ora from "ora";
import { execSync } from "node:child_process";
// src/utils/logger.ts
import chalk from "chalk";
var logger = {
info: (message) => console.log(chalk.blue("info"), message),
success: (message) => console.log(chalk.green("success"), message),
warning: (message) => console.log(chalk.yellow("warning"), message),
error: (message) => console.log(chalk.red("error"), message instanceof Error ? message.message : message)
};
// src/commands/init.ts
import { fileURLToPath } from "node:url";
var __filename = fileURLToPath(import.meta.url);
var __dirname = path.dirname(__filename);
async function init() {
const spinner = ora("Initializing Doom UI...").start();
try {
const componentDir = path.join(process.cwd(), "components", "doom-ui");
await fs.ensureDir(componentDir);
const configDir = path.join(process.cwd());
const tailwindConfigPathJS = path.join(configDir, "tailwind.config.js");
const tailwindConfigPathTS = path.join(configDir, "tailwind.config.ts");
const existingConfigPath = await fs.pathExists(tailwindConfigPathTS) ? tailwindConfigPathTS : await fs.pathExists(tailwindConfigPathJS) ? tailwindConfigPathJS : null;
if (!existingConfigPath) {
await fs.copyFile(
path.join(__dirname, "../../templates/config/tailwind.config.ts"),
tailwindConfigPathTS
);
} else {
let tailwindConfig = await fs.readFile(existingConfigPath, "utf8");
if (!tailwindConfig.includes("@doom-ui/core")) {
const isTS = path.extname(existingConfigPath) === ".ts";
const importStatement = isTS ? `import { doomUI } from "@doom-ui/core";
` : `const { doomUI } = require('@doom-ui/core');
`;
if (isTS && tailwindConfig.includes("export default")) {
tailwindConfig = tailwindConfig.replace(
"export default",
`${importStatement}export default`
);
} else {
tailwindConfig = tailwindConfig.replace(
"module.exports = {",
`${importStatement}module.exports = {`
);
}
tailwindConfig = tailwindConfig.replace(
"plugins: [",
"plugins: [\n doomUI(),"
);
await fs.writeFile(existingConfigPath, tailwindConfig);
}
}
spinner.text = "Installing dependencies...";
execSync("npm install @doom-ui/core --force", { stdio: "inherit" });
spinner.succeed("Doom UI initialized successfully!");
logger.info("You can now add components using: npx @doom-ui/cli add <component>");
} catch (error) {
spinner.fail("Failed to initialize Doom UI");
logger.error(error);
process.exit(1);
}
}
// src/commands/add.ts
import path2 from "node:path";
import fs2 from "fs-extra";
import ora2 from "ora";
import { execSync as execSync2 } from "node:child_process";
import { fileURLToPath as fileURLToPath2 } from "node:url";
var __filename2 = fileURLToPath2(import.meta.url);
var __dirname2 = path2.dirname(__filename2);
var TEMPLATES_DIR = path2.join(__dirname2, "../templates/components");
var COMPONENTS_MAP = {
button: {
files: [
"Button/index.ts",
"Button/Button.tsx",
"Button/button.styles.ts",
"Button/types.ts"
],
dependencies: ["@doom-ui/core", "@iconify/react"]
}
};
async function add(componentName) {
const spinner = ora2(`Adding ${componentName} component...`).start();
try {
const component = COMPONENTS_MAP[componentName.toLowerCase()];
if (!component) {
spinner.fail(`Component "${componentName}" not found`);
return;
}
if (component.dependencies.length > 0) {
spinner.text = "Installing dependencies...";
execSync2(`npm install ${component.dependencies.join(" ")} --force`, { stdio: "inherit" });
}
const componentDir = path2.join(process.cwd(), "components", "doom-ui", componentName);
await fs2.ensureDir(componentDir);
for (const file of component.files) {
const sourcePath = path2.join(TEMPLATES_DIR, file);
const destPath = path2.join(componentDir, path2.basename(file));
await fs2.copyFile(sourcePath, destPath);
}
spinner.succeed(`${componentName} component added successfully!`);
logger.info(`Component added to: components/doom-ui/${componentName}`);
} catch (error) {
spinner.fail(`Failed to add ${componentName} component`);
logger.error(error);
process.exit(1);
}
}
// src/index.ts
var program = new Command();
program.name("doom-ui").description("CLI for Doom UI").version("0.1.0");
program.command("init").description("Initialize Doom UI in your project").action(init);
program.command("add").description("Add a Doom UI component to your project").argument("<component>", "component name to add").action(add);
program.parse(process.argv);