Skip to content

Instantly share code, notes, and snippets.

@calexandrepcjr
Last active March 16, 2021 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calexandrepcjr/c2f89becbbb4e293928227365324dce1 to your computer and use it in GitHub Desktop.
Save calexandrepcjr/c2f89becbbb4e293928227365324dce1 to your computer and use it in GitHub Desktop.
Deletes a folder, considerable retrocompatibility + OS usage
#!/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