-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.js
More file actions
37 lines (31 loc) · 1.19 KB
/
init.js
File metadata and controls
37 lines (31 loc) · 1.19 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
const fs = require('fs-extra');
const path = require('path');
async function initializeFiles() {
console.log('Initializing application files...');
// 确保目录存在
await fs.ensureDir('./images');
await fs.ensureDir('./public');
// 初始化空的JSON文件
const files = [
{ path: './list.json', content: {} },
{ path: './images-details.json', content: [] },
{ path: './list.stats.json', content: { generated: new Date().toISOString(), stats: { totalImages: 0 } } }
];
for (const file of files) {
if (!await fs.pathExists(file.path)) {
await fs.writeJson(file.path, file.content, { spaces: 2 });
console.log(`Created ${file.path}`);
} else {
// 检查文件是否损坏
try {
await fs.readJson(file.path);
console.log(`${file.path} is valid`);
} catch (error) {
console.log(`${file.path} is corrupted, recreating...`);
await fs.writeJson(file.path, file.content, { spaces: 2 });
}
}
}
console.log('Initialization complete!');
}
initializeFiles().catch(console.error);