Skip to content

Commit 4860cda

Browse files
committed
Rename, added logs
1 parent ee86fff commit 4860cda

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/base/repositories/AbstractFileRepository.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ export default abstract class AbstractFileRepository<T = any> {
1313

1414
writeFileQueue = new PromiseQueue();
1515

16+
private get logPrefix() {
17+
const filePath = !this.saveInRoot ? this.dirWithProfileData : '';
18+
return `FileRepository [${filePath}/${this.fileName}]:`;
19+
}
20+
1621
private static get appDataFolder() {
1722
return process.env.APPDATA || '';
1823
}
@@ -34,8 +39,9 @@ export default abstract class AbstractFileRepository<T = any> {
3439
}
3540

3641
public restore(defaultValue: T): T {
42+
console.log(`${this.logPrefix} restore`);
3743
if (fs.existsSync(this.filePath)) {
38-
const data = fs.readFileSync(this.filePath);
44+
const data = fs.readFileSync(this.filePath, { encoding: 'utf-8' });
3945
// TODO handle parse error. Backup file with issues and return defaultValue
4046
return JSON.parse(data);
4147
}
@@ -44,13 +50,12 @@ export default abstract class AbstractFileRepository<T = any> {
4450

4551
public save(data: T) {
4652
FsHelper.mkdirIfNotExists(this.destFolder);
47-
this.writeFileQueue.add(() =>
48-
FsHelper.writeFile(this.filePath, data).catch(() => {
49-
console.error(
50-
`AbstractFileRepository: can't save file ${this.fileName} ${this.filePath}`
51-
);
52-
})
53-
);
54-
this.writeFileQueue.run();
53+
this.writeFileQueue.add(() => {
54+
console.log(`${this.logPrefix} save`);
55+
return FsHelper.writeFile(this.filePath, data).catch(() => {
56+
console.error(`${this.logPrefix} can't save file '${this.filePath}'`);
57+
});
58+
});
59+
this.writeFileQueue.execute();
5560
}
5661
}

src/helpers/PromiseQueueHellper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default class PromiseQueue {
1818
});
1919
}
2020

21-
run() {
21+
execute() {
2222
if (this.pendingPromise) {
2323
return;
2424
}
@@ -40,7 +40,7 @@ export default class PromiseQueue {
4040
})
4141
.finally(() => {
4242
this.pendingPromise = false;
43-
this.run();
43+
this.execute();
4444
});
4545
}
4646
}

0 commit comments

Comments
 (0)