Last active
March 16, 2021 02:57
-
-
Save calexandrepcjr/c2f89becbbb4e293928227365324dce1 to your computer and use it in GitHub Desktop.
Deletes a folder, considerable retrocompatibility + OS usage
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
#!/usr/bin/node | |
const folderPath = "dist", | |
nodeMajorVersion = process.version.match(/\d+/g)[0], | |
hasMajorVersionGreatherThanOrEqual = (aVersion) => | |
nodeMajorVersion >= aVersion, | |
successMessage = () => console.log("Dist fold deleted!"), | |
removeFolder = (path) => { | |
const fs = require("fs"); | |
if (hasMajorVersionGreatherThanOrEqual(14)) { | |
fs.rm( | |
path, | |
{ | |
recursive: true, | |
force: true, | |
}, | |
() => {} | |
); | |
return; | |
} | |
try { | |
fs.rmdirSync(path, { | |
recursive: true, | |
}); | |
} catch { | |
const isUnixBasedOS = require("os").platform().match(/linux|bsd/g).length > 0, | |
exec = require("child_process").exec; | |
if (isUnixBasedOS) { | |
exec("rm -rf " + path); | |
return; | |
} | |
exec("rmdir " + path + " /s /q"); | |
} | |
}; | |
removeFolder(folderPath); | |
successMessage(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment