Skip to content

Instantly share code, notes, and snippets.

@brunojppb
Last active August 23, 2022 10:49
Show Gist options
  • Save brunojppb/c6340bd5b84a316a093894d6f4d9393d to your computer and use it in GitHub Desktop.
Save brunojppb/c6340bd5b84a316a093894d6f4d9393d to your computer and use it in GitHub Desktop.
Count how many direct dependencies a monorepo has.
const { readdir } = require('fs/promises');
const { join } = require('path');
const fs = require('fs');
const { set } = require('date-fns');
const readdirRecursive = async dir => {
const files = await readdir(dir, { withFileTypes: true });
const paths = files.map(async file => {
const path = join(dir, file.name);
if (file.isDirectory()) return await readdirRecursive(path);
return path;
});
return (await Promise.all(paths)).flat(Infinity);
}
(async () => {
const files = await readdirRecursive('packages')
const packageFiles = files.filter(f =>
f.includes('package.json') &&
!f.includes('node_modules') &&
!f.includes('.next')
)
const devDeps = new Set()
const deps = new Set()
['./package.json', ...packageFiles].forEach(path => {
const json = JSON.parse(fs.readFileSync(path))
function pushToSet(dependencies, set) {
Object.entries(dependencies ?? {})
.forEach(([key, value]) => {
const v = `${key}: ${value}`
set.add(v)
})
}
pushToSet(json.dependencies, deps)
pushToSet(json.devDependencies, devDeps)
})
console.log('Dev Dependencies', devDeps)
console.log('Dependencies', deps)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment