cheetah-framework
Version:
Cheetah Framework JS used in all our applications
60 lines (50 loc) • 929 B
JavaScript
const path = require('path')
const fs = require('fs-extra')
class File {
/**
* Create a new instance.
*
* @param {string} filePath
*/
constructor (filePath) {
this.absolutePath = path.resolve(filePath)
}
/**
* Get the size of the file.
*/
size () {
return fs.statSync(this.path()).size
}
/**
* Determine if the given file exists.
*
* @param {string} file
*/
static exists (file) {
return fs.existsSync(file)
}
/**
* Get the absolute path to the file.
*/
path () {
return this.absolutePath
}
/**
* Write the given contents to the file.
*
* @param {string} body
*/
write (body) {
const fd = fs.openSync(this.absolutePath, 'w')
fs.writeSync(fd, body)
fs.closeSync(fd)
return this
}
/**
* Read the file's contents.
*/
read () {
return fs.readFileSync(this.path(), 'utf8')
}
}
module.exports = File