Skip to content

Instantly share code, notes, and snippets.

@darmawan01
Created August 2, 2022 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darmawan01/06ec8551512fccac8bf378571a26968a to your computer and use it in GitHub Desktop.
Save darmawan01/06ec8551512fccac8bf378571a26968a to your computer and use it in GitHub Desktop.
Duplicate file with replacer content using typescript
export const createACopy = (
sourceFile: string,
destFile: string,
matchers: string[],
replacer: string[],
) => {
// Import the filesystem module
const filePath = path.join(`${process.cwd()}/path/`, sourceFile);
let programSource = readFileSync(filePath).toString();
matchers.forEach((val, key) => {
programSource = programSource.replace(val, replacer[key]);
});
const dstPath = path.join(`${process.cwd()}/path/`, destFile);
writeFileSync(dstPath, programSource);
};
export const removeFileCopy = (fileName: string) => {
const dstPath = path.join(`${process.cwd()}/path/`, fileName);
try {
unlinkSync(dstPath);
} catch (error) {
console.log(error);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment