Skip to content

Commit a6a7b81

Browse files
Merge pull request #85 from netwrix/stuart/rename-script-error-handling
Rename Script Improvements
2 parents 03b404d + 5492bc5 commit a6a7b81

File tree

1 file changed

+84
-21
lines changed

1 file changed

+84
-21
lines changed

scripts/rename-md.js

Lines changed: 84 additions & 21 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,29 +143,46 @@ function main() {
97143
process.exit(1);
98144
}
99145

100-
try {
101-
// Move/rename the file or folder
102-
fse.moveSync(resolvedOldPath, resolvedNewPath, { overwrite: true });
103-
console.log(`Moved/renamed ${resolvedOldPath} to ${resolvedNewPath}`);
104-
105-
// Update markdown links
106-
console.log('Updating markdown links...');
107-
const markdownFiles = getAllMarkdownFiles(docsDir);
108-
console.log(`Found ${markdownFiles.length} markdown files to check`);
109-
110-
let updatedFilesCount = 0;
111-
markdownFiles.forEach(filePath => {
112-
if (updateMarkdownLinks(filePath, oldPath, newPath)) {
113-
updatedFilesCount++;
146+
if (fs.existsSync(resolvedNewPath)) {
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);
114152
}
115-
});
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+
}
171+
}
116172

117-
console.log(`Updated markdown links in ${updatedFilesCount} files under docs/.`);
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`);
118177

119-
} catch (error) {
120-
console.error('Error:', error.message);
121-
process.exit(1);
122-
}
178+
let updatedFilesCount = 0;
179+
markdownFiles.forEach(filePath => {
180+
if (updateMarkdownLinks(filePath, oldPath, newPath)) {
181+
updatedFilesCount++;
182+
}
183+
});
184+
185+
console.log(`Updated markdown links in ${updatedFilesCount} files under docs/.`);
123186
}
124187

125188
// Run the script

0 commit comments

Comments
 (0)