Skip to content

Commit 5492bc5

Browse files
Added merge option to script
1 parent 7f16928 commit 5492bc5

File tree

1 file changed

+82
-25
lines changed

1 file changed

+82
-25
lines changed

scripts/rename-md.js

Lines changed: 82 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,58 @@ function updateMarkdownLinks(filePath, oldPath, newPath) {
6969
return updatedLinks.length > 0;
7070
}
7171

72+
// Helper to recursively merge directories, skipping files that exist in the destination
73+
function mergeDirectories(src, dest) {
74+
const entries = fs.readdirSync(src, { withFileTypes: true });
75+
entries.forEach(entry => {
76+
const srcPath = path.join(src, entry.name);
77+
const destPath = path.join(dest, entry.name);
78+
if (entry.isDirectory()) {
79+
if (!fs.existsSync(destPath)) {
80+
fs.mkdirSync(destPath);
81+
}
82+
mergeDirectories(srcPath, destPath);
83+
} else {
84+
if (!fs.existsSync(destPath)) {
85+
fs.copyFileSync(srcPath, destPath);
86+
fs.unlinkSync(srcPath);
87+
} else {
88+
// Skip conflicting file
89+
console.log(` Skipped (exists): ${destPath}`);
90+
}
91+
}
92+
});
93+
}
94+
95+
// Helper to recursively remove a directory if it is empty
96+
function removeEmptyDirs(dir) {
97+
if (!fs.existsSync(dir)) return;
98+
const files = fs.readdirSync(dir);
99+
if (files.length === 0) {
100+
fs.rmdirSync(dir);
101+
return;
102+
}
103+
files.forEach(file => {
104+
const fullPath = path.join(dir, file);
105+
if (fs.statSync(fullPath).isDirectory()) {
106+
removeEmptyDirs(fullPath);
107+
}
108+
});
109+
// After removing subdirs, check again
110+
if (fs.existsSync(dir) && fs.readdirSync(dir).length === 0) {
111+
fs.rmdirSync(dir);
112+
}
113+
}
114+
72115
// Main function
73116
function main() {
74117
// Parse command line arguments
75118
const args = process.argv.slice(2);
119+
const mergeFlagIndex = args.indexOf('--merge');
120+
const mergeMode = mergeFlagIndex !== -1;
121+
if (mergeMode) args.splice(mergeFlagIndex, 1);
76122
if (args.length !== 2) {
77-
console.error('Usage: node scripts/rename-md.js <old_path> <new_path>');
123+
console.error('Usage: node scripts/rename-md.js <old_path> <new_path> [--merge]');
78124
process.exit(1);
79125
}
80126

@@ -97,35 +143,46 @@ function main() {
97143
process.exit(1);
98144
}
99145

100-
// New check: prevent overwriting if destination exists
101146
if (fs.existsSync(resolvedNewPath)) {
102-
console.error(`Error: Destination already exists: ${resolvedNewPath}`);
103-
process.exit(1);
147+
if (mergeMode) {
148+
// Only merge if both are directories
149+
if (!fs.statSync(resolvedOldPath).isDirectory() || !fs.statSync(resolvedNewPath).isDirectory()) {
150+
console.error('Error: --merge can only be used when both source and destination are directories');
151+
process.exit(1);
152+
}
153+
console.log(`Merging ${resolvedOldPath} into ${resolvedNewPath} (skipping conflicts)...`);
154+
mergeDirectories(resolvedOldPath, resolvedNewPath);
155+
// Remove the source directory only if empty (after merge)
156+
removeEmptyDirs(resolvedOldPath);
157+
console.log(`Merge complete. Skipped files remain in source if any conflicts occurred.`);
158+
} else {
159+
console.error(`Error: Destination already exists: ${resolvedNewPath}`);
160+
process.exit(1);
161+
}
162+
} else {
163+
try {
164+
// Move/rename the file or folder
165+
fse.moveSync(resolvedOldPath, resolvedNewPath);
166+
console.log(`Moved/renamed ${resolvedOldPath} to ${resolvedNewPath}`);
167+
} catch (error) {
168+
console.error('Error:', error.message);
169+
process.exit(1);
170+
}
104171
}
105172

106-
try {
107-
// Move/rename the file or folder
108-
fse.moveSync(resolvedOldPath, resolvedNewPath);
109-
console.log(`Moved/renamed ${resolvedOldPath} to ${resolvedNewPath}`);
110-
111-
// Update markdown links
112-
console.log('Updating markdown links...');
113-
const markdownFiles = getAllMarkdownFiles(docsDir);
114-
console.log(`Found ${markdownFiles.length} markdown files to check`);
173+
// Update markdown links
174+
console.log('Updating markdown links...');
175+
const markdownFiles = getAllMarkdownFiles(docsDir);
176+
console.log(`Found ${markdownFiles.length} markdown files to check`);
115177

116-
let updatedFilesCount = 0;
117-
markdownFiles.forEach(filePath => {
118-
if (updateMarkdownLinks(filePath, oldPath, newPath)) {
119-
updatedFilesCount++;
120-
}
121-
});
122-
123-
console.log(`Updated markdown links in ${updatedFilesCount} files under docs/.`);
178+
let updatedFilesCount = 0;
179+
markdownFiles.forEach(filePath => {
180+
if (updateMarkdownLinks(filePath, oldPath, newPath)) {
181+
updatedFilesCount++;
182+
}
183+
});
124184

125-
} catch (error) {
126-
console.error('Error:', error.message);
127-
process.exit(1);
128-
}
185+
console.log(`Updated markdown links in ${updatedFilesCount} files under docs/.`);
129186
}
130187

131188
// Run the script

0 commit comments

Comments
 (0)