'What is the best practice for replacing a directory in it's contents using javascript?
I've written quite a few node scripts over the years using CJS but am in the process of converting a few of my projects to ESM. One thing that occurs to me is that I may have some problems with synchronicity now. I have not had any issues yet but I'm curious to know, is the following code still good practice?
The purpose of the code is to delete and recreate a folder and it's files and then write a file into it, which is a common task I need to do for this project.
// Replaces old CJS __dirname with variable mapped to project root
const __dirname = dirname(resolve('./package.json'));
if (!existsSync(resolve(__dirname, output))) {
mkdirSync(resolve(__dirname, output));
} else {
// If the output dir exists, delete all files in it so we can start fresh
rmSync(resolve(__dirname, output), { force: true, recursive: true });
mkdirSync(resolve(__dirname, output));
}
// Create the root README file
writeFileSync(resolve(__dirname, output, 'README.md'), rootReadMe, 'utf8');
Everything does work but I suspect that it's only a matter of time before I run into issues.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
