tops-bmad
Version:
CLI tool to install BMAD workflow files into any project with integrated Shai-Hulud 2.0 security scanning
83 lines (69 loc) โข 2.46 kB
JavaScript
import fs from "fs-extra";
import path from "path";
const TEMP_DIR = path.join(process.cwd(), "bmad-temp");
/**
* Copies BMAD files to the target project
* @returns {Promise<void>}
*/
export async function copyBMADFiles() {
try {
// Verify the extracted directory exists
if (!(await fs.pathExists(TEMP_DIR))) {
throw new Error("BMAD source directory not found. Download may have failed.");
}
// Find the extracted root directory (first subdirectory in bmad-temp)
const tempContents = await fs.readdir(TEMP_DIR);
let extractedRoot = null;
for (const item of tempContents) {
const itemPath = path.join(TEMP_DIR, item);
const stats = await fs.stat(itemPath);
if (stats.isDirectory()) {
extractedRoot = item;
break;
}
}
if (!extractedRoot) {
throw new Error("Could not find extracted BMAD directory structure.");
}
const BMAD_SOURCE_DIR = path.join(TEMP_DIR, extractedRoot);
// Create necessary directories
await fs.ensureDir("./.cursor/rules");
// Copy .bmad directory
if (await fs.pathExists(path.join(BMAD_SOURCE_DIR, ".bmad"))) {
console.log("๐ Copying .bmad directory...");
await fs.copy(
path.join(BMAD_SOURCE_DIR, ".bmad"),
"./.bmad"
);
}
// Copy BMAD rules
const rulesSource = path.join(BMAD_SOURCE_DIR, ".cursor", "rules", "bmad");
if (await fs.pathExists(rulesSource)) {
console.log("๐ Copying BMAD rules...");
await fs.copy(rulesSource, "./.cursor/rules/bmad");
}
// Copy workflow command files
console.log("๐ Copying workflow commands...");
const workflowFiles = [
"ba-workflow-init-command.mdc",
"dev-workflow-init-command.mdc",
"qa-workflow-init-command.mdc"
];
for (const file of workflowFiles) {
const source = path.join(BMAD_SOURCE_DIR, ".cursor", "rules", file);
const dest = path.join("./.cursor/rules", file);
if (await fs.pathExists(source)) {
await fs.copy(source, dest);
console.log(` โ ${file}`);
}
}
// Clean up temporary files (only remove the temp directory, not the project!)
if (await fs.pathExists(TEMP_DIR)) {
console.log("๐งน Cleaning up temporary files...");
await fs.remove(TEMP_DIR);
}
} catch (error) {
console.error("โ Error copying BMAD files:", error.message);
throw error;
}
}