File size: 543 Bytes
6c07b9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const fs = require('fs');
const path = require('path');
class TextFileSync {
constructor(filename) {
this.filename = filename;
this.tempFilename = path.join(path.dirname(filename), `.${path.basename(filename)}.tmp`);
}
read() {
let data;
try {
data = fs.readFileSync(this.filename, 'utf-8');
}
catch (e) {
if (e.code === 'ENOENT') {
return null;
}
throw e;
}
return data;
}
write(str) {
fs.writeFileSync(this.tempFilename, str);
fs.renameSync(this.tempFilename, this.filename);
}
}
module.exports = { TextFileSync };