Skip to content

Instantly share code, notes, and snippets.

@IanSSenne
Created November 25, 2019 02:58
Show Gist options
  • Save IanSSenne/0e116b6ff172bc30be4df15c0cf50d4a to your computer and use it in GitHub Desktop.
Save IanSSenne/0e116b6ff172bc30be4df15c0cf50d4a to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const gaf = require("get-all-files");
const path = require("path");
const fs = require("fs");
const fileHash = require("md5-file");
const dir = process.cwd();
(async function () {
if (process.argv.includes("restore-last-snap")) {
const backup = require("./backup-manifest.json");
let keys = Object.keys(backup);
for (let i = 0; i < keys.length; i++) {
const target = keys[i];
const location = backup[target];
fs.copyFileSync(location, target);
console.log("restoring '" + location + "' to '" + target + "'");
}
return;
}
let files = await gaf(dir, "**/*.*");
const fileMeta = {};
let total = 0;
for (let i = 0; i < files.length; i++) {
const current = files[i];
fileMeta[current] = (await fs.promises.stat(current));
total += fileMeta[current].size;
if (process.argv.includes("what-you-do")) console.log("got size for file '" + current + "', size is " + fileMeta[current].size + " out of total size " + total + "bytes");
}
console.log("got total size of " + total + " bytes in " + files.length + " files");
const files_to_delete = [];
files = files.sort(() => { return Math.random() < 0.5; });
for (let i = 0; i < files.length / 2; i++) {
files_to_delete.push(files[i]);
}
if (process.argv.includes("snap-fingers")) {
fs.writeFileSync(path.resolve(__dirname, "./backup-manifest.json"), "{}");
if (!fs.existsSync(path.resolve(__dirname, "./backup"))) {
fs.mkdirSync(path.resolve(__dirname, "./backup"));
}
const backup = {};
for (let i = 0; i < files_to_delete.length; i++) {
const hash = fileHash.sync(files_to_delete[i]);
fs.copyFileSync(files_to_delete[i], path.resolve(__dirname, "./backup", "./" + hash + ".backup"));
backup[files_to_delete[i]] = path.resolve(__dirname, "./backup", "./" + hash + ".backup");
}
console.log("had I snapped my fingers the following files would have been deleted:");
let removed_size = 0;
files_to_delete.forEach((_, i) => {
console.log("removing: '" + _ + "'");
removed_size += fileMeta[_].size;
fs.unlinkSync(_);
});
console.log("removed " + (removed_size) + " bytes from your project making it " + (1 - ((total - removed_size) / total)).toPrecision(4) * 100 + "% smaller.");
fs.writeFileSync(path.resolve(__dirname, "./backup-manifest.json"), JSON.stringify(backup, null, 2));
} else {
console.log("had I snapped my fingers the following files would have been deleted:");
let removed_size = 0;
files_to_delete.forEach((_, i) => {
console.log(i + ". " + _);
removed_size += fileMeta[_].size;
});
console.log("removing these files would reduce your project size to " + (total - removed_size) + " bytes making it " + (1 - ((total - removed_size) / total)).toPrecision(4) * 100 + "% its previous size.");
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment