microfox
Version:
Universal CLI tool for creating modern TypeScript packages with npm availability checking
89 lines (87 loc) • 3.23 kB
JavaScript
import {
getWorkingDirectory
} from "./chunk-TZQZMKHP.mjs";
// src/commands/add.ts
import { Command } from "commander";
import fs from "fs";
import path from "path";
import chalk from "chalk";
import inquirer from "inquirer";
import readlineSync from "readline-sync";
async function addBackgroundAgentFunctions(name) {
const workingDir = getWorkingDirectory();
const functionsDir = path.join(workingDir, "src", "functions", name);
console.log(
chalk.blue(
`\u{1F680} Adding background agent functions ${chalk.bold(name)} at ${functionsDir}
`
)
);
fs.mkdirSync(functionsDir, { recursive: true });
const templateDir = path.resolve(__dirname, "background-agent", "src", "functions");
const entries = fs.readdirSync(templateDir, { withFileTypes: true });
for (const entry of entries) {
const srcPath = path.join(templateDir, entry.name);
const destPath = path.join(functionsDir, entry.name.replace(/\.txt$/, ""));
if (!entry.isDirectory() && entry.name.endsWith(".txt")) {
const templateContent = fs.readFileSync(srcPath, "utf-8");
const content = templateContent.replace(/<%= agentName %>/g, name);
fs.writeFileSync(destPath, content);
console.log(chalk.green(`\u2705 Created ${path.relative(workingDir, destPath)}`));
}
}
}
async function addAction() {
console.log(chalk.cyan("\u2728 Add new features to your project!\n"));
const workingDir = getWorkingDirectory();
const microfoxConfigPath = path.join(workingDir, "microfox.json");
if (!fs.existsSync(microfoxConfigPath)) {
console.log(chalk.red("Error: `microfox.json` not found in the current directory."));
console.log(chalk.red("Please run this command from the root of a Microfox project."));
return;
}
const { featureType } = await inquirer.prompt([
{
type: "list",
name: "featureType",
message: "Select what you want to add:",
choices: ["background_agent_functions"]
}
]);
if (!featureType) {
console.log(chalk.yellow("Operation cancelled."));
return;
}
if (featureType === "background_agent_functions") {
const functionName = readlineSync.question(
chalk.yellow("\u{1F4E6} Enter a name for the new functions: ")
);
if (!functionName.trim()) {
throw new Error("Function name cannot be empty");
}
await addBackgroundAgentFunctions(functionName.trim());
console.log(
chalk.green(
`
\u{1F389} Successfully added background agent functions ${chalk.bold(functionName)}!`
)
);
console.log(chalk.gray(`\u{1F4CD} Located at src/functions/${functionName.trim()}`));
console.log(chalk.yellow("\n\u{1F4A1} Next steps:"));
console.log(chalk.yellow(" 1. Check the new files in src/functions/"));
console.log(chalk.yellow(" 2. Update your agent logic to use the new functions."));
}
}
var addCommand = new Command("add").description("Add features to a Microfox project").action(async () => {
try {
await addAction();
} catch (error) {
console.error(chalk.red("\u274C Error:"), error instanceof Error ? error.message : String(error));
process.exit(1);
}
});
export {
addCommand
};
//# sourceMappingURL=chunk-UYROVW53.mjs.map