Skip to content

Instantly share code, notes, and snippets.

@squamuglia
Created December 29, 2021 14:56
Show Gist options
  • Save squamuglia/dda5706bdaf9a68b238cc0197d5034cc to your computer and use it in GitHub Desktop.
Save squamuglia/dda5706bdaf9a68b238cc0197d5034cc to your computer and use it in GitHub Desktop.
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