2021-06-22 11:07:32 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
class File {
|
|
|
|
constructor(path) {
|
|
|
|
this.path = path;
|
2021-10-06 12:15:05 +00:00
|
|
|
this.content = null;
|
2021-06-22 11:07:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
read() {
|
|
|
|
try {
|
|
|
|
const content = fs.readFileSync(this.path, { encoding: 'utf-8' });
|
|
|
|
this.content = content;
|
|
|
|
return this.content;
|
|
|
|
} catch (err) {
|
|
|
|
return err.message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 12:15:05 +00:00
|
|
|
write(data, isJSON) {
|
2021-06-22 11:07:32 +00:00
|
|
|
this.content = data;
|
2021-10-06 12:15:05 +00:00
|
|
|
fs.writeFileSync(
|
|
|
|
this.path,
|
|
|
|
isJSON ? JSON.stringify(this.content) : this.content
|
|
|
|
);
|
2021-06-22 11:07:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 12:15:05 +00:00
|
|
|
module.exports = File;
|