Fix logic in saveAsBackup

This commit is contained in:
Bet4 2021-04-23 17:15:14 +08:00 committed by Labhansh Agrawal
parent 5e375b61c3
commit 5224bc5c41

View file

@ -23,17 +23,12 @@ const _write = (path: string, data: string) => {
const saveAsBackup = (src: string) => {
let attempt = 1;
while (attempt < 100) {
try {
const backupPath = `${src}.backup${attempt === 1 ? '' : attempt}`;
const backupPath = `${src}.backup${attempt === 1 ? '' : attempt}`;
if (!existsSync(backupPath)) {
moveSync(src, backupPath);
return backupPath;
} catch (e) {
if (e.code === 'EEXIST') {
attempt++;
} else {
throw e;
}
}
attempt++;
}
throw new Error('Failed to create backup for config file. Too many backups');
};