-
-
Save sebastianhomeier/68e237cae51a14dc95d24861285280f2 to your computer and use it in GitHub Desktop.
NVM list global packages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const { promises: fs } = require('fs'); | |
const path = require('path'); | |
(async () => { | |
const nvmPath = process.env.NVM_DIR; | |
if (!nvmPath) { | |
console.error('Unable to detect nvm path. Exiting.'); | |
process.exit(1); | |
} | |
const onlyNpm = {}; | |
const nothing = {}; | |
const apps = await fs.readdir(path.resolve(nvmPath, 'versions')); | |
for await (const app of apps) { | |
const versions = await fs.readdir(path.resolve(nvmPath, 'versions', app)); | |
for (const version of versions) { | |
const packagesPath = [nvmPath, 'versions', app, version, 'lib', 'node_modules']; | |
const packages = await fs.readdir(path.resolve(...packagesPath)); | |
const hasOnlyNpm = packages.length === 1 && packages[0] === 'npm'; | |
const hasNothing = !packages.length; | |
if (hasOnlyNpm) { | |
if (!onlyNpm[app]) { | |
onlyNpm[app] = []; | |
} | |
onlyNpm[app].push(version); | |
continue; | |
} else if (hasNothing) { | |
if (!nothing[app]) { | |
nothing[app] = []; | |
} | |
nothing[app].push(version); | |
continue; | |
} | |
console.log(version); | |
console.log(Array(version.length + 1).join('=')) | |
for (const p of packages) { | |
console.log(p); | |
if ('@' === p[0]) { | |
const subpackages = await fs.readdir(path.resolve(...packagesPath, p)); | |
for (const s of subpackages) { | |
console.log(` ${s}`); | |
} | |
} | |
} | |
console.log(''); | |
} | |
} | |
if (Object.keys(onlyNpm).length) { | |
console.log(`Only npm\n========`); | |
for (const app in onlyNpm) { | |
console.log(`${app}: ${onlyNpm[app].join(', ')}`); | |
} | |
console.log(''); | |
} | |
if (Object.keys(nothing).length) { | |
console.log(`No package\n==========`); | |
for (const app in nothing) { | |
console.log(`${app}: ${nothing[app].join(', ')}`); | |
} | |
console.log(''); | |
} | |
process.exit(0); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment