Created
August 12, 2017 14:01
-
-
Save sguzunov/b7c13eb02a4d6a1fd16cc34ad7bc092f to your computer and use it in GitHub Desktop.
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
function clearDirectorySync(dirAbsPath) { | |
const fs = require('fs'); // Node.js package for working with file system (required). | |
const rmDirContent = function rmDir(dirAbsPath) { | |
if (!fs.existsSync(dirAbsPath)) { | |
return; | |
} | |
const dirContent = fs.readdirSync(dirAbsPath); | |
if (!dirContent.length) return; | |
dirContent.forEach((el) => { | |
const pathToElement = `${dirAbsPath}/${el}`; | |
const isDirectory = fs.lstatSync(pathToElement).isDirectory(); | |
if (isDirectory) { | |
rmDir(pathToElement); | |
fs.rmdirSync(pathToElement); | |
} else { | |
fs.unlinkSync(pathToElement); | |
} | |
}); | |
}; | |
rmDirContent(dirAbsPath); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment