Skip to content

Instantly share code, notes, and snippets.

@WuglyakBolgoink
Created April 3, 2019 16:58
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 WuglyakBolgoink/41e1fceebd459ff6efca39bd70bd7533 to your computer and use it in GitHub Desktop.
Save WuglyakBolgoink/41e1fceebd459ff6efca39bd70bd7533 to your computer and use it in GitHub Desktop.
Clean node_modules to minimize CI Caching/Artifacts
/**
* TODO:
* 1) remove all ".idea" folders!
* 2) remove all "coverage" folders!
* 3) remove all empty folders!
* 4) remove all "examples" folders!
* 5) remove all "example" folders!
* 6) remove all "test" folders!
* 7) remove all "tests" folders!
* 8) remove all "*.md" files extly with names!
*/
const fs = require('fs');
const path = require('path');
const del = require('del');
const extensions = [
// /package\.json$/i,
// /\.json$/i, // <<<< TODO: can not be deleted with wildcard "*"!!!
// /\.d\.ts$/i, // TODO: ERROR in error TS6053: File '/blabox/node_modules/typescript/lib/lib.dom.d.ts' not found.
// /\.md$/i, // TODO: File /blabox/node_modules/@angular/cli/commands/add.md was not found while constructing the subcommand add.
/\.map$/i,
/LICENSE\.APACHE2$/i,
/LICENSE\.MIT$/i,
/LICENSE\.BSD$/i,
/LICENSE\-MIT$/i,
/LICENSE\.wrapped$/i,
/LICENSE$/i,
/LICENCE$/i,
/README$/i,
/README\.md$/i,
/NOTICE$/i,
/AUTHORS$/i,
/COPYING$/i,
/CREDITS$/i,
/INSTALL$/i,
/CHANGELOG$/i,
/Makefile$/i,
/\.conf$/i,
/\.bak$/i,
/\.ac$/i,
/\.spec\./i,
/\.cpp$/i,
/\.h$/i,
/\.c$/i,
/\.apk$/i,
/\.ipa$/i,
/\.css$/i,
/\.less$/i,
/\.sass$/i,
/\.scss$/i,
/\.png$/i,
/\.pngs$/i,
/\.svg$/i,
/\.jpg$/i,
/\.jpeg$/i,
/\.bmp$/i,
/\.gif$/i,
/\.ico$/i,
/\.yml$/i,
/\.yaml$/i,
/\.pl$/i,
/\.py$/i,
/\.rb$/i,
/\.coffee$/i,
/\.markdown$/i,
/\.html$/i,
/\.gitignore$/i,
/\.gitkeep$/i,
/\.gitattributes$/i,
/\.npmignore$/i,
/\.lock$/i,
/\.editorconfig$/i,
/\.eslintrc\.json/i,
/\.eslintrc\.js/i,
/\.eslintrc$/i,
/\.eslintignore$/i,
/\.jscsrc$/i,
/\.jshintrc$/i,
/\.jshintignore$/i,
/\.jscs\.json$/i,
/\.log$/i,
/\.hpp$/i,
/\.cc$/i,
/\.txt$/i,
/\.doc$/i,
/\.docx$/i,
/\.pdf$/i,
/\.xls$/i,
/\.xlsx$/i,
/\.tgz$/i,
/\.tar$/i,
/\.zip$/i,
/\.7z$/i,
/\.rar$/i,
/\.bat$/i,
/\.exe$/i,
/\.hbs$/i, // completion.sh.hbs
/autotest\.watchr$/i,
/gulpfile\.js$/i,
/bower\.json$/i,
/Gruntfile\.js$/i,
/\.pub$/i,
/\.priv$/i,
/\.pem$/i,
/\.crt$/i,
/\.key$/i,
/tsconfig\.json$/i,
/tslint\.json$/i,
/ts_spec_config\.json$/i
];
const nodeDir = path.resolve(__dirname, 'node_modules');
const list = walkDir(nodeDir);
let remFiles = [];
extensions.forEach((rex, index) => {
let fList = list.filter((item) => {
return rex.test(item.path);
});
console.log(`[${ index + 1 }][${ rex }]:`, fList.length, 'files');
remFiles = [ ...remFiles, ...fList ];
});
console.table([
{
'"node_modules" size': `${ getSize(list).toFixed(2) } mb`,
'Should be deleted': `${ getSize(remFiles).toFixed(2) } mb`,
'list length': list.length,
'remFiles length': remFiles.length
}
]);
const delFiles = remFiles.map(el => el.path);
(async () => {
const deletedPaths = await del(delFiles);
if (deletedPaths.length > 0) {
console.log('Deleted files and folders:', deletedPaths.length);
}
})();
function getSize(files) {
let sum = 0;
files.forEach((file) => {
sum = sum + file.size;
});
return sum;
}
function walkDir(dir) {
const result = [];
let p;
const files = [ dir ];
do {
const filepath = files.pop();
const stat = fs.lstatSync(filepath);
if (stat.isDirectory()) {
fs
.readdirSync(filepath)
.forEach(f => files.push(path.join(filepath, f)));
} else {
if (stat.isFile()) {
result.push(createFileMeta(path.resolve(dir, filepath)));
}
}
} while (files.length !== 0);
return result;
}
function createFileMeta(filename) {
return {
path: filename,
size: getFilesizeInBytes(filename)
};
}
function getFilesizeInBytes(filename) {
const stats = fs.statSync(filename);
const fileSizeInBytes = stats.size;
//Convert the file size to megabytes (optional)
const fileSizeInMegabytes = fileSizeInBytes / (1024 * 1024);
return fileSizeInMegabytes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment