Created
December 29, 2021 14:56
-
-
Save squamuglia/dda5706bdaf9a68b238cc0197d5034cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from "fs"; | |
const readFileToString = (path: string): Promise<string | undefined> => | |
new Promise((res, rej) => | |
fs.readFile(path, (err, data) => { | |
if (err) return rej(err); | |
res(data?.toString()); | |
}), | |
); | |
const writeFile = (path: string, data: any): Promise<void> => | |
new Promise((res, rej) => | |
fs.writeFile( | |
path, | |
typeof data === "string" ? data : JSON.stringify(data), | |
(err) => { | |
if (err) { | |
console.error("log error: ", err); | |
rej(); | |
} | |
res(); | |
}, | |
), | |
); | |
const moveFile = (path: string, target: string) => | |
new Promise((res) => | |
fs.rename(path, target, (err) => { | |
if (err) { | |
console.log(err); | |
res(undefined); | |
} | |
res(undefined); | |
}), | |
); | |
const replaceAll = ( | |
string: string | undefined, | |
search: string, | |
replacement: string, | |
) => (!string ? string : string.replace(new RegExp(search, "g"), replacement)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment