qansigliere-fs-utils
Version:
The main idea of this library, created in JavaScript, is to provide a ready-made set of methods for creating and updating files.
50 lines (43 loc) • 1.37 kB
JavaScript
const fs = require('fs');
function createFileFromString(fileName, stringValue) {
if (typeof stringValue == 'string') {
fs.writeFile(fileName, stringValue, 'utf-8', err => {
if (err) {
console.log(`Error writing file: ${err}`);
} else {
console.log(`File "${fileName}" is written successfully!`);
}
});
} else {
console.log(`Value should have "String" type. Now it is ${typeof err}`);
}
}
function saveBase64AsImage(pngBase64String, pathToFile) {
fs.writeFile(pathToFile, pngBase64String, { encoding: 'base64' });
fs.readFileSync(pathToFile);
}
function fileExists(pathToFile) {
return fs.existsSync(pathToFile) ? true : false;
}
function deleteFile(pathToFile) {
try {
fs.unlinkSync(pathToFile);
return true;
} catch {
return false;
}
}
function readFile(pathToFile, encoding) {
try {
const file = fs.readFileSync(pathToFile, encoding);
return file;
} catch (error) {
console.error('Error reading JSON:', error.message);
return null;
}
}
module.exports.createFileFromString = createFileFromString;
module.exports.saveBase64AsImage = saveBase64AsImage;
module.exports.fileExists = fileExists;
module.exports.deleteFile = deleteFile;
module.exports.readFile = readFile;