-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.js
More file actions
498 lines (422 loc) · 13.5 KB
/
init.js
File metadata and controls
498 lines (422 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
const fs = require('fs-extra');
const path = require('path');
const { execSync } = require('child_process');
const { debug, info, success, warn, error } = require('./logger');
const { validateRole } = require('./validator');
const role = process.argv[2];
const projectRoot = process.cwd();
const templateRoot = path.join(__dirname, '../templates');
const CONFIG_VERSION = '1.2.1';
// Track files that were copied for potential rollback
const copiedFiles = [];
const backupFiles = new Map();
const ROLES_DIR = 'roles';
const CURSOR_DIR = '.cursor';
const CURSOR_RULES_DIR = path.join(CURSOR_DIR, 'rules');
const GITHUB_DIR = '.github/workflows';
function checkConfigVersion(configPath) {
try {
const config = fs.readJsonSync(configPath);
if (config.version && config.version !== CONFIG_VERSION) {
warn(`Configuration version mismatch. Template version: ${CONFIG_VERSION}, Existing version: ${config.version}`);
return false;
}
return true;
} catch (err) {
debug(`No version found in existing config: ${err.message}`);
return true;
}
}
function ensureConfigVersion(configPath) {
try {
const config = fs.readJsonSync(configPath);
config.version = CONFIG_VERSION;
fs.writeJsonSync(configPath, config, { spaces: 2 });
debug(`Added version ${CONFIG_VERSION} to config: ${path.relative(projectRoot, configPath)}`);
} catch (err) {
error(`Failed to add version to config: ${err.message}`);
}
}
function backupExistingFile(filePath) {
if (fs.existsSync(filePath)) {
const backupPath = `${filePath}.backup-${Date.now()}`;
fs.copySync(filePath, backupPath);
backupFiles.set(filePath, backupPath);
warn(`Backed up existing file: ${path.relative(projectRoot, filePath)} → ${path.relative(projectRoot, backupPath)}`);
}
}
function restoreBackups() {
backupFiles.forEach((backupPath, originalPath) => {
try {
fs.moveSync(backupPath, originalPath, { overwrite: true });
debug(`Restored backup: ${path.relative(projectRoot, originalPath)}`);
} catch (err) {
error(`Failed to restore backup for ${originalPath}: ${err.message}`);
}
});
}
function validateEnvironment() {
debug('Validating environment...');
// Check Node.js version
const nodeVersion = process.version;
const requiredVersion = '14.0.0';
if (nodeVersion < requiredVersion) {
throw new Error(`Node.js version ${requiredVersion} or higher is required. Current version: ${nodeVersion}`);
}
// Git is optional, no need to check for it
debug('Environment validation completed');
}
function rollback() {
info('Rolling back changes...');
// Remove newly copied files
copiedFiles.forEach(file => {
try {
if (fs.existsSync(file)) {
fs.removeSync(file);
debug(`Removed: ${file}`);
}
} catch (err) {
error(`Failed to remove ${file}: ${err.message}`);
}
});
// Restore backups
restoreBackups();
}
function validateTemplates() {
debug('Validating templates...');
// Check if templates directory exists
if (!fs.existsSync(templateRoot)) {
throw new Error('Templates directory not found');
}
// Check if .cursor directory exists and is a directory
const cursorDir = path.join(templateRoot, '.cursor');
if (!fs.existsSync(cursorDir) || !fs.statSync(cursorDir).isDirectory()) {
throw new Error('Invalid .cursor directory in templates');
}
// Check if .cursor/rules directory exists
const rulesDir = path.join(cursorDir, 'rules');
if (!fs.existsSync(rulesDir) || !fs.statSync(rulesDir).isDirectory()) {
throw new Error('Invalid .cursor/rules directory in templates');
}
// Check if role template exists
const roleTemplatePath = path.join(templateRoot, 'roles', `${role}.cursor`);
if (!fs.existsSync(roleTemplatePath)) {
throw new Error(`Role template not found: ${roleTemplatePath}`);
}
// Check if base config exists
const baseConfigPath = path.join(cursorDir, 'config.json');
if (!fs.existsSync(baseConfigPath)) {
throw new Error('Base configuration not found in templates');
}
debug('Template validation completed');
}
function checkExistingConfigurations() {
const filesToCheck = [
'.cursor',
'.husky/pre-commit',
'.husky/commit-msg',
'.github/workflows/cursor-ai-review.yml'
];
const existingFiles = filesToCheck.filter(file => {
const fullPath = path.join(projectRoot, file);
return fs.existsSync(fullPath);
});
if (existingFiles.length > 0) {
warn('\n⚠️ Existing configurations found:');
existingFiles.forEach(file => {
warn(` - ${file}`);
});
warn('\nThese files will be backed up before proceeding.');
}
return existingFiles;
}
function copyAll() {
try {
// Validate environment first
validateEnvironment();
// Validate role
validateRole(role);
// Check for existing configurations
const existingFiles = checkExistingConfigurations();
// Check version compatibility for existing files
if (existingFiles.includes('.cursor')) {
const cursorPath = path.join(projectRoot, '.cursor');
if (!checkConfigVersion(cursorPath)) {
warn('Version mismatch detected. Consider using the merge command instead.');
if (!process.env.FORCE_INIT) {
error('Aborting due to version mismatch. Set FORCE_INIT=1 to override.');
process.exit(1);
}
}
}
// Validate templates
validateTemplates();
// Ensure all required directories exist
ensureDirectories();
// Define files to copy
const filesToCopy = [
{ src: path.join(templateRoot, '.cursor', 'config.json'), dest: path.join(projectRoot, '.cursor', 'config.json') },
{ src: path.join(templateRoot, '.github'), dest: path.join(projectRoot, '.github') }
];
// Backup existing files first
filesToCopy.forEach(({ dest }) => {
backupExistingFile(dest);
});
// Copy base files and track them
filesToCopy.forEach(({ src, dest }) => {
try {
fs.copySync(src, dest);
copiedFiles.push(dest);
debug(`Copied: ${path.relative(projectRoot, dest)}`);
} catch (err) {
throw new Error(`Failed to copy ${src} to ${dest}: ${err.message}`);
}
});
// Create role-specific .mdc file
const roleRulesTemplatePath = path.join(templateRoot, '.cursor', 'rules', `${role}.mdc`);
const roleRulesPath = path.join(projectRoot, CURSOR_RULES_DIR, `${role}.mdc`);
try {
// Read the template content
const templateContent = fs.readFileSync(roleRulesTemplatePath, 'utf8');
// Write the MDC file
fs.writeFileSync(roleRulesPath, templateContent);
copiedFiles.push(roleRulesPath);
debug(`Created role rules: ${path.relative(projectRoot, roleRulesPath)}`);
} catch (err) {
throw new Error(`Failed to create role rules: ${err.message}`);
}
// Ensure version is set in new configurations
ensureConfigVersion(path.join(projectRoot, '.cursor', 'config.json'));
// Validate copied files
const requiredFiles = ['.cursor/config.json', path.join(CURSOR_RULES_DIR, `${role}.mdc`)];
requiredFiles.forEach(file => {
const fullPath = path.join(projectRoot, file);
if (!fs.existsSync(fullPath)) {
throw new Error(`Required file not found after copy: ${file}`);
}
});
success('Initialization completed successfully!');
info('\nConfiguration files installed:');
info(' - .cursor/config.json (base configuration)');
info(` - .cursor/rules/${role}.mdc (role-specific rules)`);
info(' - .github (CI/CD workflows)');
if (backupFiles.size > 0) {
info('\n⚠️ Backups created for existing files:');
backupFiles.forEach((backupPath, originalPath) => {
info(` - ${path.relative(projectRoot, originalPath)} → ${path.relative(projectRoot, backupPath)}`);
});
}
info('\nNext steps:');
info(' 1. Review the installed configuration files');
info(' 2. Compare with backups if any were created');
info(' 3. Initialize git if you want to use git-related features');
} catch (err) {
error(`Error during initialization: ${err.message}`);
rollback();
process.exit(1);
}
}
function ensureDirectories() {
[ROLES_DIR, CURSOR_DIR, CURSOR_RULES_DIR, GITHUB_DIR].forEach(dir => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
debug(`Created directory: ${dir}`);
}
});
}
function setupGitHubWorkflow() {
const workflowContent = `name: Cursor Config Management
on:
push:
branches: [ main, master ]
paths:
- '.cursor/**'
- 'roles/**'
pull_request:
branches: [ main, master ]
paths:
- '.cursor/**'
- 'roles/**'
jobs:
config:
name: Manage Cursor Config
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Install Cursor Ops
run: npm install -g @liftoffllc/cursor-ops
- name: Audit configuration
run: cursor-ops audit
- name: Backup current config
if: github.event_name == 'push'
run: |
mkdir -p .cursor-backups
timestamp=$(date +%s)
cp .cursor/config.json .cursor-backups/config-$timestamp.json
- name: Merge configurations
if: github.event_name == 'push'
run: |
# Get changed roles from git diff
roles=$(git diff --name-only HEAD~1 HEAD -- roles/ | cut -d'/' -f2 | sort -u)
for role in $roles; do
cursor-ops merge --role $role
done
- name: Create backup
if: github.event_name == 'push'
run: cursor-ops backup list
- name: Cleanup old backups
if: github.event_name == 'push'
run: cursor-ops backup cleanup --days 30`;
fs.writeFileSync(path.join(GITHUB_DIR, 'cursor-config.yml'), workflowContent);
info('Created GitHub Actions workflow');
}
function setupGitIgnore() {
const gitignorePath = path.join(projectRoot, '.gitignore');
const gitignoreContent = `# Dependencies
node_modules/
package-lock.json
yarn.lock
pnpm-lock.yaml
# Environment variables
.env
.env.*
!.env.example
# IDE and Editor files
.idea/
.vscode/
*.swp
*.swo
.DS_Store
Thumbs.db
# Debug logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
debug.log
*.log
# Build and Distribution
dist/
build/
out/
.next/
.nuxt/
.cache/
.parcel-cache/
.turbo/
.vercel/
# Test coverage
coverage/
.nyc_output/
# Cursor specific backups and temporary files
.cursor-backups/
*.cursor-backup
*.pre-migration-*
.cursor.merged
.cursor-role-*.merged
# Operating System
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
# Misc
*.pid
*.seed
*.pid.lock
.lock-wscript
.node_repl_history
.npm
.eslintcache
.stylelintcache
.env.local
.env.development.local
.env.test.local
.env.production.local`;
try {
if (fs.existsSync(gitignorePath)) {
// If .gitignore exists, append our patterns if they're not already there
const existingContent = fs.readFileSync(gitignorePath, 'utf8');
const newPatterns = gitignoreContent.split('\n').filter(pattern =>
pattern && !existingContent.includes(pattern)
);
if (newPatterns.length > 0) {
fs.appendFileSync(gitignorePath, '\n' + newPatterns.join('\n'));
info('Updated existing .gitignore with Cursor patterns');
}
} else {
// Create new .gitignore
fs.writeFileSync(gitignorePath, gitignoreContent);
info('Created .gitignore file');
}
return true;
} catch (err) {
error(`Failed to setup .gitignore: ${err.message}`);
return false;
}
}
function setupPackageJson() {
const packageJsonPath = 'package.json';
if (!fs.existsSync(packageJsonPath)) {
const packageJson = {
name: 'cursor-config',
version: '1.1.0',
description: 'Cursor configuration management',
scripts: {
'cursor:init': 'cursor-ops init',
'cursor:merge': 'cursor-ops merge',
'cursor:audit': 'cursor-ops audit',
'cursor:backup': 'cursor-ops backup list'
},
devDependencies: {
'@liftoffllc/cursor-ops': 'latest'
}
};
fs.writeJsonSync(packageJsonPath, packageJson, { spaces: 2 });
info('Created package.json');
}
}
function initializeConfig(role) {
try {
validateRole(role);
ensureDirectories();
setupGitHubWorkflow();
setupGitIgnore();
setupPackageJson();
// Copy role-specific configuration
const roleConfigPath = path.join(ROLES_DIR, role, 'config.json');
if (fs.existsSync(roleConfigPath)) {
const config = fs.readJsonSync(roleConfigPath);
fs.writeJsonSync(path.join(CURSOR_DIR, 'config.json'), config, { spaces: 2 });
success(`Initialized Cursor configuration for role: ${role}`);
} else {
throw new Error(`Role configuration not found: ${role}`);
}
// Initialize git if not already initialized
if (!fs.existsSync('.git')) {
execSync('git init', { stdio: 'inherit' });
info('Initialized git repository');
}
// Create initial commit
execSync('git add .', { stdio: 'inherit' });
execSync('git commit -m "Initialize Cursor configuration"', { stdio: 'inherit' });
success('Created initial commit');
} catch (err) {
error(`Initialization failed: ${err.message}`);
throw err;
}
}
// Run the initialization
copyAll();
module.exports = {
initializeConfig
};