Skip to content

Commit

Permalink
feat: add functional to copdir function
Browse files Browse the repository at this point in the history
now "copyDir" function can copy not only files but also folders with files inside recursively
  • Loading branch information
Daniil Kochkin committed May 22, 2022
1 parent 36daa6b commit dddd8de
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions 04-copy-directory/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@ const fs = require('fs/promises');
const filesPath = path.join(__dirname, 'files');
const copyFilesPath = path.join(__dirname, 'copy-files');

process.on('exit', () => {
process.stdout.write('created a full copy');
});

function copyDir(fromPath, toPath) {
fs.rm(toPath, { force: true, recursive: true })
.then(() => {
fs.mkdir(toPath);
fs.readdir(fromPath)
.then((data) => {
data.forEach(file => {
fs.copyFile(path.join(fromPath, file), path.join(toPath, file));
if (path.extname(file) === '') {
copyDir(path.join(fromPath, file), path.join(toPath, file));
} else {
fs.copyFile(path.join(fromPath, file), path.join(toPath, file));
}
});
});
})
.catch((err) => {
throw err;
});

process.stdout.write('created a full copy');
}

copyDir(filesPath, copyFilesPath);

0 comments on commit dddd8de

Please sign in to comment.