Created
August 2, 2022 01:59
-
-
Save darmawan01/06ec8551512fccac8bf378571a26968a to your computer and use it in GitHub Desktop.
Duplicate file with replacer content using typescript
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
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