Skip to content

Instantly share code, notes, and snippets.

@matthewoestreich
Created October 11, 2019 13:38
Show Gist options
  • Select an option

  • Save matthewoestreich/d7e1889cbd422840c1f3cd5d60076aa8 to your computer and use it in GitHub Desktop.

Select an option

Save matthewoestreich/d7e1889cbd422840c1f3cd5d60076aa8 to your computer and use it in GitHub Desktop.
Cleans React Build
const fs = require('fs');
function deleteFolderRecursive(path) {
if (fs.existsSync(path) && fs.lstatSync(path).isDirectory()) {
fs.readdirSync(path).forEach(function (file, index) {
var curPath = path + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) {
deleteFolderRecursive(curPath);
} else {
fs.unlinkSync(curPath);
}
});
console.log(`\x1b[36mDeleting directory:\x1b[0m "${path}"...`);
fs.rmdirSync(path);
}
};
function copySingleFile(source, destination) {
if (fs.existsSync(source) && fs.lstatSync(source).isFile() && !fs.existsSync(destination)) {
console.log(`\x1b[36mStarting file copy from ${source} to ${destination}\x1b[0m`)
fs.copyFileSync(source, destination, (err) => {
if (err) throw err;
});
} else {
console.log(`\x1b[31mUnable to copy file ${source} - please ensure it is a file and that it exists. Also, verify it does not already exists in ${destination}\r\n\x1b[0m`)
}
}
function renameDirectory(source, destination) {
if (fs.lstatSync(source).isDirectory() && fs.existsSync(source) && !fs.existsSync(destination)) {
console.log(`\x1b[36mStarting directory rename from ${source} to ${destination}\x1b[0m`)
fs.rename(source, destination, function (err) {
if (err) throw err;
});
} else {
console.log(`\x1b[31mUnable to rename directory from ${source} to ${destination} - please ensure the source exists, as well as the destination does not already exist!\r\n\x1b[0m`);
}
}
console.log("\x1b[33mCleaning working tree...\x1b[0m");
deleteFolderRecursive("./docs");
console.log("\x1b[32mSuccessfully cleaned working tree!\x1b[0m\r\n");
console.log("\x1b[33mRenaming 'build' to 'docs'\x1b[0m");
renameDirectory("./build", "./docs");
console.log("\x1b[32mSuccessfully renamed 'build' to 'docs'!\x1b[0m\r\n")
console.log("\x1b[33mCopying CNAME file\x1b[0m");
copySingleFile("./CNAME", "./docs/CNAME");
console.log("\x1b[32mSuccessfully copied CNAME!\x1b[0m\r\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment