Skip to content

Commit 3fe90c3

Browse files
committed
feat(sync): 🧹 add directory cleanup before sync operation
- Add getConfigDirectories() function to read all directories from config folder - Add cleanDirectories() function to remove old directories before sync - Clean directories list includes all config subdirectories plus 'skills' directory - Execute cleanup before sync to avoid overlay issues - Add cleanup preview in dry-run mode
1 parent 9aa9b9a commit 3fe90c3

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

scripts/sync-config.mjs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,61 @@ function createBackup(targetDir) {
169169
}
170170
}
171171

172+
/**
173+
* 获取 config 目录下的所有目录列表
174+
* @returns {Array<string>} 目录名称数组
175+
*/
176+
function getConfigDirectories() {
177+
try {
178+
const items = fs.readdirSync(configDir);
179+
const directories = items.filter(item => {
180+
const itemPath = path.join(configDir, item);
181+
return fs.statSync(itemPath).isDirectory();
182+
});
183+
return directories;
184+
} catch (error) {
185+
console.error('获取 config 目录列表失败:', error.message);
186+
return [];
187+
}
188+
}
189+
190+
/**
191+
* 清理目标目录中的指定目录
192+
* @param {string} targetDir 目标目录
193+
* @param {Array<string>} dirsToClean 要清理的目录列表
194+
*/
195+
function cleanDirectories(targetDir, dirsToClean) {
196+
if (!fs.existsSync(targetDir)) {
197+
return;
198+
}
199+
200+
console.log(` 🧹 清理目标目录中的旧目录...`);
201+
let cleanedCount = 0;
202+
203+
for (const dirName of dirsToClean) {
204+
const dirPath = path.join(targetDir, dirName);
205+
206+
if (fs.existsSync(dirPath)) {
207+
try {
208+
const stat = fs.statSync(dirPath);
209+
if (stat.isDirectory()) {
210+
fs.rmSync(dirPath, { recursive: true, force: true });
211+
console.log(` 🗑️ 已删除: ${dirName}`);
212+
cleanedCount++;
213+
}
214+
} catch (error) {
215+
console.error(` ❌ 删除目录失败: ${dirName}`, error.message);
216+
}
217+
}
218+
}
219+
220+
if (cleanedCount > 0) {
221+
console.log(` ✅ 已清理 ${cleanedCount} 个目录`);
222+
} else {
223+
console.log(` ℹ️ 没有需要清理的目录`);
224+
}
225+
}
226+
172227
/**
173228
* 主同步函数
174229
*/
@@ -204,6 +259,11 @@ async function syncConfigs(options = {}) {
204259
console.log(`📋 共需要同步 ${templateConfigs.length} 个模板`);
205260
console.log(`🔧 模式: ${dryRun ? '干运行' : '实际执行'}\n`);
206261

262+
// 获取要清理的目录列表(config 目录下的所有目录 + skills 目录)
263+
const configDirectories = getConfigDirectories();
264+
const dirsToClean = [...configDirectories, 'skills'];
265+
console.log(`📋 将清理的目录: ${dirsToClean.join(', ')}\n`);
266+
207267
let successCount = 0;
208268
let skipCount = 0;
209269

@@ -229,6 +289,16 @@ async function syncConfigs(options = {}) {
229289

230290
if (dryRun) {
231291
console.log(` 🔍 [干运行] 将同步到: ${targetDir}`);
292+
// 显示将要清理的目录
293+
const existingDirs = dirsToClean.filter(dirName => {
294+
const dirPath = path.join(targetDir, dirName);
295+
return fs.existsSync(dirPath) && fs.statSync(dirPath).isDirectory();
296+
});
297+
if (existingDirs.length > 0) {
298+
console.log(` 🔍 [干运行] 将清理目录: ${existingDirs.join(', ')}`);
299+
} else {
300+
console.log(` 🔍 [干运行] 没有需要清理的目录`);
301+
}
232302
successCount++;
233303
continue;
234304
}
@@ -243,6 +313,9 @@ async function syncConfigs(options = {}) {
243313
fs.mkdirSync(targetDir, { recursive: true });
244314
}
245315

316+
// 清理目标目录中的旧目录
317+
cleanDirectories(targetDir, dirsToClean);
318+
246319
// 同步config目录下的所有内容
247320
if (includePatterns) {
248321
// 如果有包含模式,只同步指定的目录和文件

0 commit comments

Comments
 (0)