-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbulk-replace.js.backup
More file actions
263 lines (223 loc) · 8.52 KB
/
Copy pathbulk-replace.js.backup
File metadata and controls
263 lines (223 loc) · 8.52 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
const fs = require('fs');
const path = require('path');
// Function to check if a file is binary
function isBinaryFile(filePath) {
try {
const buffer = fs.readFileSync(filePath);
const bytes = buffer.slice(0, Math.min(1024, buffer.length));
for (let i = 0; i < bytes.length; i++) {
const byte = bytes[i];
// Check for null bytes or other control characters that indicate binary
if (byte === 0 || (byte < 32 && byte !== 9 && byte !== 10 && byte !== 13)) {
return true;
}
}
return false;
} catch (error) {
return false;
}
}
// Function to recursively find all files with specific extensions
function findFiles(dir, extensions, excludeDirs = []) {
let results = [];
try {
const files = fs.readdirSync(dir);
for (const file of files) {
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
// Skip common directories that shouldn't be processed
if (!file.startsWith('.') &&
!excludeDirs.includes(file) &&
file !== 'node_modules' &&
file !== 'dist' &&
file !== 'build' &&
file !== 'out' &&
file !== 'coverage') {
results = results.concat(findFiles(fullPath, extensions, excludeDirs));
}
} else if (extensions.some(ext => file.endsWith(ext)) && !isBinaryFile(fullPath)) {
results.push(fullPath);
}
}
} catch (error) {
console.error(`Error accessing directory ${dir}:`, error.message);
}
return results;
}
// Function to create backup of file
function createBackup(filePath) {
const backupPath = filePath + '.backup';
try {
fs.copyFileSync(filePath, backupPath);
return backupPath;
} catch (error) {
console.error(`Failed to create backup for ${filePath}:`, error.message);
return null;
}
}
// Function to replace text in file
function replaceInFile(filePath, replacements, createBackups = false) {
try {
let content = fs.readFileSync(filePath, 'utf8');
let originalContent = content;
let changeCount = 0;
for (const [search, replace] of replacements) {
const matches = content.match(search);
if (matches) {
content = content.replace(search, replace);
changeCount += matches.length;
}
}
if (content !== originalContent) {
if (createBackups) {
createBackup(filePath);
}
fs.writeFileSync(filePath, content, 'utf8');
console.log(`Updated: ${filePath} (${changeCount} replacements)`);
return true;
}
return false;
} catch (error) {
console.error(`Error processing ${filePath}:`, error.message);
return false;
}
}
// Main replacement logic
const rootDir = process.cwd();
const extensions = ['.ts', '.tsx', '.js', '.jsx', '.json', '.md', '.mdx', '.sh', '.proto', '.txt', '.yml', '.yaml', '.html', '.css', '.scss', '.less'];
// Define replacements - converting remaining "cline" instances to "infinity"
// Order matters - more specific patterns first
const replacements = [
// URLs and domains - be very specific
[/cline\.bot/g, 'infinity.bot'],
[/github\.com\/cline\/cline/g, 'github.com/infinity/infinity'],
[/saoudrizwan\.cline/g, 'saoudrizwan.infinity'],
[/marketplace\.visualstudio\.com\/items\?itemName=saoudrizwan\.cline/g, 'marketplace.visualstudio.com/items?itemName=saoudrizwan.infinity'],
// Package and module names
[/package cline/g, 'package infinity'],
[/cline\/cline\/lint/g, 'infinity/infinity/lint'],
[/cline package/g, 'infinity package'],
[/bot\.cline\./g, 'bot.infinity.'],
// File and directory patterns
[/\.clinerules/g, '.infinityrules'],
[/clinerules/g, 'infinityrules'],
[/\.clineignore/g, '.infinityignore'],
[/clineignore/g, 'infinityignore'],
[/cline-/g, 'infinity-'],
[/cline_/g, 'infinity_'],
// Environment variables and constants
[/CLINE_ENVIRONMENT/g, 'INFINITY_ENVIRONMENT'],
[/CLINE_DIR/g, 'INFINITY_DIR'],
[/CLINE_/g, 'INFINITY_'],
// Class names and identifiers (PascalCase)
[/\bClineWalkthrough\b/g, 'InfinityWalkthrough'],
[/\bClineAccount\b/g, 'InfinityAccount'],
[/\bClineAuth\b/g, 'InfinityAuth'],
[/\bClineError\b/g, 'InfinityError'],
[/\bClineIgnore\b/g, 'InfinityIgnore'],
[/\bClineRules\b/g, 'InfinityRules'],
[/\bClineLogo\b/g, 'InfinityLogo'],
// Whole word replacements with word boundaries (most common cases)
[/\bCline\b/g, 'Infinity'],
[/\bcline\b/g, 'infinity'],
[/\bCLINE\b/g, 'INFINITY'],
// VS Code specific patterns
[/claude-dev/g, 'infinity-dev'],
[/claude\.dev/g, 'infinity.dev'],
];
// Process command line arguments
const args = process.argv.slice(2);
const createBackups = args.includes('--backup');
const dryRun = args.includes('--dry-run');
const verbose = args.includes('--verbose');
if (args.includes('--help') || args.includes('-h')) {
console.log(`
Cline to Infinity Bulk Replacement Script
Usage: node bulk-replace.js [options]
Options:
--backup Create backup files (.backup extension) before making changes
--dry-run Show what would be changed without actually making changes
--verbose Show detailed output
--help, -h Show this help message
This script will replace all remaining occurrences of "cline" with "infinity"
while preserving proper capitalization and avoiding partial word matches.
`);
process.exit(0);
}
console.log('Cline to Infinity Bulk Replacement Script');
console.log('========================================');
if (dryRun) {
console.log('DRY RUN MODE - No files will be modified');
}
if (createBackups && !dryRun) {
console.log('Backup files will be created');
}
// Get all files to process
console.log('\nScanning for files...');
const files = findFiles(rootDir, extensions);
console.log(`\nFound ${files.length} files to process`);
if (verbose) {
console.log('\nFile types found:');
const extCounts = {};
files.forEach(file => {
const ext = path.extname(file);
extCounts[ext] = (extCounts[ext] || 0) + 1;
});
Object.entries(extCounts).forEach(([ext, count]) => {
console.log(` ${ext}: ${count} files`);
});
}
console.log('\nProcessing files...\n');
// Process each file
let processedCount = 0;
let changedCount = 0;
for (const file of files) {
if (dryRun) {
// For dry run, just check what would change
try {
let content = fs.readFileSync(file, 'utf8');
let hasMatches = false;
let totalMatches = 0;
for (const [search, replace] of replacements) {
const matches = content.match(search);
if (matches) {
hasMatches = true;
totalMatches += matches.length;
}
}
if (hasMatches) {
console.log(`WOULD UPDATE: ${file} (${totalMatches} replacements)`);
changedCount++;
} else if (verbose) {
console.log(`No changes: ${file}`);
}
} catch (error) {
console.error(`Error checking ${file}:`, error.message);
}
} else {
// Actually process the file
if (replaceInFile(file, replacements, createBackups)) {
changedCount++;
} else if (verbose) {
console.log(`No changes: ${file}`);
}
}
processedCount++;
}
console.log('\n========================================');
console.log('Replacement Summary');
console.log('========================================');
console.log(`Files processed: ${processedCount}`);
console.log(`Files ${dryRun ? 'that would be ' : ''}changed: ${changedCount}`);
if (!dryRun && changedCount > 0) {
console.log('\nReplacement complete!');
if (createBackups) {
console.log('Backup files have been created with .backup extension');
console.log('You can restore files by renaming .backup files back to original names');
}
} else if (dryRun && changedCount > 0) {
console.log('\nRun without --dry-run to make actual changes');
} else {
console.log('\nNo files needed changes');
}