sync-shared.js脚本优化,现在先清空后拷贝。

This commit is contained in:
janing
2025-12-18 13:26:59 +08:00
parent 2dbb1e8d05
commit 90175a1665

View File

@@ -35,6 +35,10 @@ async function syncShared() {
// 确保目标目录存在 // 确保目标目录存在
await fs.ensureDir(CLIENT_SHARED_PATH); await fs.ensureDir(CLIENT_SHARED_PATH);
// 清空目标目录(删除旧文件)
console.log('🗑️ 正在清空目标目录...');
await fs.emptyDir(CLIENT_SHARED_PATH);
// 复制目录 // 复制目录
console.log('📦 正在复制文件...'); console.log('📦 正在复制文件...');
await fs.copy(SERVER_SHARED_PATH, CLIENT_SHARED_PATH, { await fs.copy(SERVER_SHARED_PATH, CLIENT_SHARED_PATH, {
@@ -52,7 +56,7 @@ async function syncShared() {
console.log('✅ 同步完成!'); console.log('✅ 同步完成!');
console.log(''); console.log('');
// 列出同步的文件 // 列出同步的文件
const files = await getFileList(CLIENT_SHARED_PATH); const files = await getFileList(CLIENT_SHARED_PATH);
console.log(`共同步 ${files.length} 个文件:`); console.log(`共同步 ${files.length} 个文件:`);
@@ -60,10 +64,10 @@ async function syncShared() {
const relativePath = path.relative(CLIENT_SHARED_PATH, file); const relativePath = path.relative(CLIENT_SHARED_PATH, file);
console.log(' -', relativePath); console.log(' -', relativePath);
}); });
console.log(''); console.log('');
console.log('=========================================='); console.log('==========================================');
} catch (error) { } catch (error) {
console.error('❌ 同步失败:', error.message); console.error('❌ 同步失败:', error.message);
process.exit(1); process.exit(1);
@@ -75,14 +79,14 @@ async function syncShared() {
*/ */
async function getFileList(dir) { async function getFileList(dir) {
const files = []; const files = [];
async function walk(currentPath) { async function walk(currentPath) {
const items = await fs.readdir(currentPath); const items = await fs.readdir(currentPath);
for (const item of items) { for (const item of items) {
const itemPath = path.join(currentPath, item); const itemPath = path.join(currentPath, item);
const stat = await fs.stat(itemPath); const stat = await fs.stat(itemPath);
if (stat.isDirectory()) { if (stat.isDirectory()) {
await walk(itemPath); await walk(itemPath);
} else { } else {
@@ -90,7 +94,7 @@ async function getFileList(dir) {
} }
} }
} }
await walk(dir); await walk(dir);
return files; return files;
} }