-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
182 lines (154 loc) · 6.24 KB
/
Copy pathindex.js
File metadata and controls
182 lines (154 loc) · 6.24 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const { spawn } = require('node:child_process');
const fs = require('node:fs/promises');
const { constants } = require('node:fs');
const path = require('node:path');
const os = require('node:os');
const { loadEnvFile } = require('node:process');
// 1. Safely load the configuration
try {
loadEnvFile(path.join(__dirname, 'config.env'));
} catch (err) {
console.error(`[Error] Failed to load config.env: ${err.message}`);
process.exit(1);
}
// 2. Helper function to safely resolve paths (including '~' home directory expansion)
const resolvePath = (p) => {
if (!p) return '';
if (p.startsWith('~/') || p === '~') {
return path.join(os.homedir(), p.slice(1));
}
return path.resolve(p);
};
// 3. Environment validation and configuration mapping
const requiredVars = [
'BACKUP_DIR',
'BACKUP_INTERVAL_MS',
'GAME_EXECUTABLE',
'MAX_BACKUPS',
'SOURCE_PATH'
];
const missingVars = requiredVars.filter(key => !process.env[key]);
if (missingVars.length > 0) {
console.error(`[Error] Missing required environment variables: ${missingVars.join(', ')}`);
console.error(`[Error] Please check your config.env file.`);
process.exit(1);
}
const config = {
backupDir: resolvePath(process.env.BACKUP_DIR),
sourcePath: resolvePath(process.env.SOURCE_PATH),
gameExecutable: resolvePath(process.env.GAME_EXECUTABLE),
backupIntervalMs: parseInt(process.env.BACKUP_INTERVAL_MS, 10),
maxBackups: parseInt(process.env.MAX_BACKUPS, 10),
saveBasename: path.basename(process.env.SOURCE_PATH, path.extname(process.env.SOURCE_PATH)),
saveExtension: path.extname(process.env.SOURCE_PATH)
};
if (isNaN(config.backupIntervalMs) || config.backupIntervalMs <= 0) {
console.error(`[Error] BACKUP_INTERVAL_MS must be a positive integer.`);
process.exit(1);
}
if (isNaN(config.maxBackups) || config.maxBackups <= 0) {
console.error(`[Error] MAX_BACKUPS must be a positive integer.`);
process.exit(1);
}
// 4. Timestamp formatting
const getLocalTimestamp = () => {
const now = new Date();
const pad = (num) => String(num).padStart(2, '0');
const date = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}`;
const time = `${pad(now.getHours())}-${pad(now.getMinutes())}-${pad(now.getSeconds())}`;
return `${date}_${time}`;
};
// 5. Encapsulate backup logic within a class
class BackupManager {
constructor(cfg) {
this.config = cfg;
this.intervalId = null;
}
async init() {
try {
await fs.mkdir(this.config.backupDir, { recursive: true });
} catch (err) {
console.error(`[Error] Failed to create backup directory: ${err.message}`);
process.exit(1);
}
}
async backup() {
try {
// Asynchronous and more efficient file existence check
try {
await fs.access(this.config.sourcePath, constants.F_OK);
} catch {
console.log(`[Backup] Source save file ${this.config.sourcePath} does not exist yet. Waiting...`);
return;
}
const timestamp = getLocalTimestamp();
const backupFileName = `${this.config.saveBasename}_${timestamp}${this.config.saveExtension}`;
const backupFilePath = path.join(this.config.backupDir, backupFileName);
await fs.copyFile(this.config.sourcePath, backupFilePath);
console.log(`[Backup] Successfully created: ${backupFileName}`);
await this.cleanOldBackups();
} catch (err) {
console.error(`[Backup] Failed to create backup: ${err.message}`);
}
}
async cleanOldBackups() {
try {
const files = await fs.readdir(this.config.backupDir);
const backupFiles = files
.filter(f => f.startsWith(`${this.config.saveBasename}_`) && f.endsWith(this.config.saveExtension))
// The timestamp in the filename ensures lexicographical sorting matches creation time sorting.
// This saves I/O operations as we don't need to call fs.stat().
.sort()
.reverse(); // Newest first
if (backupFiles.length > this.config.maxBackups) {
const filesToDelete = backupFiles.slice(this.config.maxBackups);
// Delete surplus backups concurrently
await Promise.all(filesToDelete.map(async (file) => {
try {
await fs.unlink(path.join(this.config.backupDir, file));
console.log(`[Cleanup] Deleted old backup: ${file}`);
} catch (err) {
console.error(`[Cleanup] Failed to delete ${file}: ${err.message}`);
}
}));
}
} catch (err) {
console.error(`[Cleanup] Failed to read directory: ${err.message}`);
}
}
start() {
this.backup(); // Trigger the first backup immediately
this.intervalId = setInterval(() => this.backup(), this.config.backupIntervalMs);
}
stop() {
if (this.intervalId) {
clearInterval(this.intervalId);
this.intervalId = null;
}
}
}
// 6. Asynchronous main entry point
async function main() {
const backupManager = new BackupManager(config);
await backupManager.init();
console.log(`[System] Launching ${config.gameExecutable}...`);
const gameProcess = spawn(config.gameExecutable, [], { stdio: 'inherit' });
backupManager.start();
gameProcess.on('close', (code) => {
console.log(`[System] Game closed (Code: ${code}). Stopping backups and exiting script.`);
backupManager.stop();
// Pass the game's exit code to the Node.js process
process.exit(code === null ? 0 : code);
});
gameProcess.on('error', (err) => {
console.error(`[Error] Failed to launch the game. Please verify GAME_EXECUTABLE path.`);
console.error(`[Error] Details: ${err.message}`);
backupManager.stop();
process.exit(1);
});
}
// Initialize the script with top-level error handling
main().catch(err => {
console.error(`[Fatal] Unexpected system error: ${err.message}`);
process.exit(1);
});