|
| 1 | +import { InteractiveCommand, InteractiveOption } from "interactive-commander"; |
| 2 | +import Ora from "ora"; |
| 3 | +import fs from "fs"; |
| 4 | +import path from "path"; |
| 5 | +import { fileURLToPath } from "url"; |
| 6 | +import { confirm } from "@inquirer/prompts"; |
| 7 | + |
| 8 | +// Get the directory of this file |
| 9 | +const __filename = fileURLToPath(import.meta.url); |
| 10 | +const __dirname = path.dirname(__filename); |
| 11 | +// Access agents.md from package root (works in both dev and production) |
| 12 | +// Resolve from current file location: try both paths to handle dev and bundled environments |
| 13 | +const AGENTS_MD = fs.existsSync(path.resolve(__dirname, "../agents.md")) |
| 14 | + ? path.resolve(__dirname, "../agents.md") |
| 15 | + : path.resolve(__dirname, "../../../../agents.md"); |
| 16 | +// Create .cursorrules in user's current working directory |
| 17 | +const CURSORRULES = path.resolve(process.cwd(), ".cursorrules"); |
| 18 | + |
| 19 | +export default new InteractiveCommand() |
| 20 | + .command("cursor") |
| 21 | + .description("Initialize .cursorrules with i18n-specific instructions for Cursor AI.") |
| 22 | + .addOption( |
| 23 | + new InteractiveOption("-f, --force", "Overwrite .cursorrules without prompt.") |
| 24 | + .default(false) |
| 25 | + ) |
| 26 | + .action(async (options) => { |
| 27 | + const spinner = Ora(); |
| 28 | + // Read agents.md |
| 29 | + let template: string; |
| 30 | + try { |
| 31 | + template = fs.readFileSync(AGENTS_MD, "utf-8"); |
| 32 | + } catch (err) { |
| 33 | + spinner.fail("Template file agents.md not found. Please reinstall the package."); |
| 34 | + return process.exit(1); |
| 35 | + } |
| 36 | + // Check for existing .cursorrules |
| 37 | + const exists = fs.existsSync(CURSORRULES); |
| 38 | + let shouldWrite; |
| 39 | + if (exists && !options.force) { |
| 40 | + shouldWrite = await confirm({ |
| 41 | + message: ".cursorrules already exists. Overwrite?", |
| 42 | + }); |
| 43 | + if (!shouldWrite) { |
| 44 | + spinner.info("Skipped: .cursorrules left unchanged."); |
| 45 | + return; |
| 46 | + } |
| 47 | + } |
| 48 | + try { |
| 49 | + fs.writeFileSync(CURSORRULES, template); |
| 50 | + spinner.succeed("Created .cursorrules"); |
| 51 | + spinner.info( |
| 52 | + ".cursorrules has been created with i18n-specific instructions for Cursor AI.", |
| 53 | + ); |
| 54 | + } catch (err) { |
| 55 | + spinner.fail(`Failed to write .cursorrules: ${err}`); |
| 56 | + process.exit(1); |
| 57 | + } |
| 58 | + }); |
0 commit comments