Skip to content

Instantly share code, notes, and snippets.

@dgieselaar
Created December 6, 2018 20:02
Show Gist options
  • Save dgieselaar/dd6de31ca967af29267b8ea4ef3a18ad to your computer and use it in GitHub Desktop.
Save dgieselaar/dd6de31ca967af29267b8ea4ef3a18ad to your computer and use it in GitHub Desktop.
const fs = require('fs');
const path = require('path');
const { uniq, groupBy, pickBy } = require('lodash');
const beautifiedJsFile = fs.readFileSync(path.join(__dirname, './build.js'), 'utf-8');
const nodeModulesRegex = /"(.*?\/node_modules\/.*?)"/;
const findNodeModules = ( str ) => {
const lines = str.split('\n');
const modules = [];
lines.forEach(line => {
if (line.includes(`/node_modules/`)) {
const match = line.match(nodeModulesRegex);
modules.push(match[1]);
}
})
return uniq(modules);
};
const getRealModuleName = ( str ) => {
return str.substr(str.lastIndexOf('/node_modules/'));
};
const findDupes = ( str ) => {
const modules = findNodeModules(str);
return pickBy(
groupBy(modules, getRealModuleName),
( val, key ) => val.length > 1
);
};
console.log(findDupes(beautifiedJsFile));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment