Skip to content

Instantly share code, notes, and snippets.

@mabhub
Created February 19, 2019 14:13
Show Gist options
  • Save mabhub/5b9a32da340d89770eccbcfc3b702569 to your computer and use it in GitHub Desktop.
Save mabhub/5b9a32da340d89770eccbcfc3b702569 to your computer and use it in GitHub Desktop.
NVM list global packages
#!/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([`Node ${version}`, ...packages].join(`\n - `), '\n');
}
}
if (Object.keys(onlyNpm).length) {
console.log(`# Only npm:`);
for (const app in onlyNpm) {
console.log(` - ${app}: ${onlyNpm[app].join(', ')}`);
}
console.log('');
}
if (Object.keys(nothing).length) {
console.log(`# No package:`);
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