Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@JamieMagee
Created March 7, 2021 22:12
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 JamieMagee/ecd5ca417a7dcfaa2afd73f88d52adc3 to your computer and use it in GitHub Desktop.
Save JamieMagee/ecd5ca417a7dcfaa2afd73f88d52adc3 to your computer and use it in GitHub Desktop.
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const child_process_1 = require("child_process");
function getProjectUrl(project) {
if (typeof project === 'string') {
return project;
}
if ((project === null || project === void 0 ? void 0 : project.url) !== undefined) {
return project.url;
}
return 'UNKNOWN';
}
function parseDependencies(dependency) {
var _a;
let output = [];
output.push({
name: dependency.name,
version: dependency.version,
license: (_a = dependency.license) !== null && _a !== void 0 ? _a : 'UNKNOWN',
source: getProjectUrl(dependency.repository),
});
if (dependency.dependencies) {
Object.entries(dependency.dependencies).forEach(([key, value]) => {
output.push(...parseDependencies(value));
});
}
return output;
}
child_process_1.exec('npm list --global --depth=1 --long --json', (error, stdout, stderr) => {
const npmList = JSON.parse(stdout);
console.log(JSON.stringify([
...Object.keys(npmList.dependencies)
.map((key) => {
return parseDependencies(npmList.dependencies[key]);
})
.reduce((a, b) => a.concat(b), []),
]));
});
//# sourceMappingURL=index.js.map
import { exec } from 'child_process';
type Repository = { type: string; url: string } | string;
type Dependency = {
name: string;
version: string;
license?: string;
repository: Repository;
dependencies?: { [key: string]: Dependency };
};
type NpmList = {
name: 'npm';
dependencies: { [key: string]: Dependency };
};
type TernOutput = {
name: string;
version: string;
license: string;
source: string;
};
function getProjectUrl(project: Repository): string {
if (typeof project === 'string') {
return project;
}
if (project?.url !== undefined) {
return project.url;
}
return 'UNKNOWN';
}
function parseDependencies(dependency: Dependency): TernOutput[] {
let output: TernOutput[] = [];
output.push({
name: dependency.name,
version: dependency.version,
license: dependency.license ?? 'UNKNOWN',
source: getProjectUrl(dependency.repository),
});
if (dependency.dependencies) {
Object.entries(dependency.dependencies).forEach(([key, value]) => {
output.push(...parseDependencies(value));
});
}
return output;
}
exec('npm list --global --depth=1 --long --json', (error, stdout, stderr) => {
const npmList: NpmList = JSON.parse(stdout);
console.log(
JSON.stringify([
...Object.keys(npmList.dependencies)
.map((key) => {
return parseDependencies(npmList.dependencies[key]);
})
.reduce((a, b) => a.concat(b), []),
])
);
});
[
{
"name": "npm",
"version": "7.6.0",
"license": "Artistic-2.0",
"source": "https://github.com/npm/cli"
},
{
"name": "@npmcli/arborist",
"version": "2.2.5",
"license": "ISC",
"source": "https://github.com/npm/arborist"
},
{
"name": "@npmcli/ci-detect",
"version": "1.3.0",
"license": "ISC",
"source": "git+https://github.com/npm/ci-detect.git"
},
{
"name": "@npmcli/config",
"version": "1.2.9",
"license": "ISC",
"source": "git+https://github.com/npm/config"
},
{
"name": "@npmcli/run-script",
"version": "1.8.3",
"license": "ISC",
"source": "git+https://github.com/npm/run-script.git"
},
{
"name": "abbrev",
"version": "1.1.1",
"license": "ISC",
"source": "http://github.com/isaacs/abbrev-js"
},
{
"name": "ansicolors",
"version": "0.3.2",
"license": "MIT",
"source": "git://github.com/thlorenz/ansicolors.git"
},
{
"name": "ansistyles",
"version": "0.1.3",
"license": "MIT",
"source": "git://github.com/thlorenz/ansistyles.git"
},
{
"name": "archy",
"version": "1.0.0",
"license": "MIT",
"source": "http://github.com/substack/node-archy.git"
},
{
"name": "byte-size",
"version": "7.0.0",
"license": "MIT",
"source": "https://github.com/75lb/byte-size"
},
{
"name": "cacache",
"version": "15.0.5",
"license": "ISC",
"source": "https://github.com/npm/cacache"
},
{
"name": "chalk",
"version": "4.1.0",
"license": "MIT",
"source": "chalk/chalk"
},
{
"name": "chownr",
"version": "2.0.0",
"license": "ISC",
"source": "git://github.com/isaacs/chownr.git"
},
{
"name": "cli-columns",
"version": "3.1.2",
"license": "MIT",
"source": "shannonmoeller/cli-columns"
},
{
"name": "cli-table3",
"version": "0.6.0",
"license": "MIT",
"source": "https://github.com/cli-table/cli-table3.git"
},
{
"name": "columnify",
"version": "1.5.4",
"license": "MIT",
"source": "git://github.com/timoxley/columnify.git"
},
{
"name": "glob",
"version": "7.1.6",
"license": "ISC",
"source": "git://github.com/isaacs/node-glob.git"
},
{
"name": "graceful-fs",
"version": "4.2.6",
"license": "ISC",
"source": "https://github.com/isaacs/node-graceful-fs"
},
{
"name": "hosted-git-info",
"version": "3.0.8",
"license": "ISC",
"source": "git+https://github.com/npm/hosted-git-info.git"
},
{
"name": "ini",
"version": "2.0.0",
"license": "ISC",
"source": "git://github.com/isaacs/ini.git"
},
{
"name": "init-package-json",
"version": "2.0.2",
"license": "ISC",
"source": "https://github.com/npm/init-package-json.git"
},
{
"name": "is-cidr",
"version": "4.0.2",
"license": "BSD-2-Clause",
"source": "silverwind/is-cidr"
},
{
"name": "json-parse-even-better-errors",
"version": "2.3.1",
"license": "MIT",
"source": "https://github.com/npm/json-parse-even-better-errors"
},
{
"name": "leven",
"version": "3.1.0",
"license": "MIT",
"source": "sindresorhus/leven"
},
{
"name": "libnpmaccess",
"version": "4.0.1",
"license": "ISC",
"source": "https://github.com/npm/libnpmaccess.git"
},
{
"name": "libnpmdiff",
"version": "2.0.3",
"license": "ISC",
"source": "https://github.com/npm/libnpmdiff"
},
{
"name": "libnpmfund",
"version": "1.0.2",
"license": "ISC",
"source": "https://github.com/npm/libnpmfund"
},
{
"name": "libnpmhook",
"version": "6.0.1",
"license": "ISC",
"source": "https://github.com/npm/libnpmhook"
},
{
"name": "libnpmorg",
"version": "2.0.1",
"license": "ISC",
"source": "https://github.com/npm/libnpmorg.git"
},
{
"name": "libnpmpack",
"version": "2.0.1",
"license": "ISC",
"source": "https://github.com/npm/libnpmpack.git"
},
{
"name": "libnpmpublish",
"version": "4.0.0",
"license": "ISC",
"source": "https://github.com/npm/libnpmpublish.git"
},
{
"name": "libnpmsearch",
"version": "3.1.0",
"license": "ISC",
"source": "https://github.com/npm/libnpmsearch.git"
},
{
"name": "libnpmteam",
"version": "2.0.2",
"license": "ISC",
"source": "https://github.com/npm/libnpmteam.git"
},
{
"name": "libnpmversion",
"version": "1.0.11",
"license": "ISC",
"source": "git+https://github.com/npm/libnpmversion"
},
{
"name": "make-fetch-happen",
"version": "8.0.14",
"license": "ISC",
"source": "https://github.com/npm/make-fetch-happen"
},
{
"name": "minipass-pipeline",
"version": "1.2.4",
"license": "ISC",
"source": "UNKNOWN"
},
{
"name": "minipass",
"version": "3.1.3",
"license": "ISC",
"source": "git+https://github.com/isaacs/minipass.git"
},
{
"name": "mkdirp-infer-owner",
"version": "2.0.0",
"license": "ISC",
"source": "git+https://github.com/isaacs/mkdirp-infer-owner"
},
{
"name": "mkdirp",
"version": "1.0.4",
"license": "MIT",
"source": "https://github.com/isaacs/node-mkdirp.git"
},
{ "name": "ms", "version": "2.1.3", "license": "MIT", "source": "vercel/ms" },
{
"name": "node-gyp",
"version": "7.1.2",
"license": "MIT",
"source": "git://github.com/nodejs/node-gyp.git"
},
{
"name": "nopt",
"version": "5.0.0",
"license": "ISC",
"source": "https://github.com/npm/nopt.git"
},
{
"name": "npm-audit-report",
"version": "2.1.4",
"license": "ISC",
"source": "git+https://github.com/npm/npm-audit-report.git"
},
{
"name": "npm-package-arg",
"version": "8.1.1",
"license": "ISC",
"source": "https://github.com/npm/npm-package-arg"
},
{
"name": "npm-pick-manifest",
"version": "6.1.0",
"license": "ISC",
"source": "https://github.com/npm/npm-pick-manifest"
},
{
"name": "npm-profile",
"version": "5.0.2",
"license": "ISC",
"source": "git+https://github.com/npm/npm-profile.git"
},
{
"name": "npm-registry-fetch",
"version": "9.0.0",
"license": "ISC",
"source": "https://github.com/npm/npm-registry-fetch"
},
{
"name": "npm-user-validate",
"version": "1.0.1",
"license": "BSD-2-Clause",
"source": "git://github.com/npm/npm-user-validate.git"
},
{
"name": "npmlog",
"version": "4.1.2",
"license": "ISC",
"source": "https://github.com/npm/npmlog.git"
},
{
"name": "opener",
"version": "1.5.2",
"license": "(WTFPL OR MIT)",
"source": "domenic/opener"
},
{
"name": "pacote",
"version": "11.2.7",
"license": "ISC",
"source": "git@github.com:npm/pacote"
},
{
"name": "parse-conflict-json",
"version": "1.1.1",
"license": "ISC",
"source": "git+https://github.com/npm/parse-conflict-json.git"
},
{
"name": "qrcode-terminal",
"version": "0.12.0",
"license": "UNKNOWN",
"source": "https://github.com/gtanner/qrcode-terminal"
},
{
"name": "read-package-json-fast",
"version": "2.0.2",
"license": "ISC",
"source": "git+https://github.com/npm/read-package-json-fast.git"
},
{
"name": "read-package-json",
"version": "3.0.1",
"license": "ISC",
"source": "https://github.com/npm/read-package-json.git"
},
{
"name": "read",
"version": "1.0.7",
"license": "ISC",
"source": "git://github.com/isaacs/read.git"
},
{
"name": "readdir-scoped-modules",
"version": "1.1.0",
"license": "ISC",
"source": "https://github.com/npm/readdir-scoped-modules"
},
{
"name": "rimraf",
"version": "3.0.2",
"license": "ISC",
"source": "git://github.com/isaacs/rimraf.git"
},
{
"name": "semver",
"version": "7.3.4",
"license": "ISC",
"source": "https://github.com/npm/node-semver"
},
{
"name": "ssri",
"version": "8.0.1",
"license": "ISC",
"source": "https://github.com/npm/ssri"
},
{
"name": "tar",
"version": "6.1.0",
"license": "ISC",
"source": "https://github.com/npm/node-tar.git"
},
{
"name": "text-table",
"version": "0.2.0",
"license": "MIT",
"source": "git://github.com/substack/text-table.git"
},
{
"name": "tiny-relative-date",
"version": "1.3.0",
"license": "MIT",
"source": "https://github.com/wildlyinaccurate/relative-date.git"
},
{
"name": "treeverse",
"version": "1.0.4",
"license": "ISC",
"source": "git+https://github.com/npm/treeverse.git"
},
{
"name": "validate-npm-package-name",
"version": "3.0.0",
"license": "ISC",
"source": "https://github.com/npm/validate-npm-package-name"
},
{
"name": "which",
"version": "2.0.2",
"license": "ISC",
"source": "git://github.com/isaacs/node-which.git"
},
{
"name": "write-file-atomic",
"version": "3.0.3",
"license": "ISC",
"source": "git://github.com/npm/write-file-atomic.git"
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment