New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

johnny-dependency

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

johnny-dependency - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

lib/cli.js

93

lib/index.js
const pacote = require('pacote');
const getAllDependencies = require('./get-dependencies');
const untildify = require('untildify');
const fs = require('fs');
const npa = require('npm-package-arg');
const getLatestDependencies = require('./get-latest-dependencies');
const { resolveDependencies, formatGraphWithoutCircularDeps } = require('./format-dependencies');
const _ = require('lodash');
const npmrcPath = untildify('~/.npmrc');
const npmrc = fs.readFileSync(npmrcPath, 'utf8');
const tokenMatch = npmrc.match(/.*authToken=(.*)/);
const token = tokenMatch && tokenMatch[1];
const examplePackage = {
name: '@domain-group/fe-co-button',
version: '3.0.0'
};
function getPackage(npmPackage) {
const spec = `${npmPackage.name}@${npmPackage.version}`;
return pacote.manifest(spec, {
auth: {
token
}
});
function buildGraph(rootPackage, pacoteOptions) {
return getAllDependencies(rootPackage, pacoteOptions).then(result => getLatestDependencies(_.uniq(result.dependencies.map(npmPackage => npmPackage.name))).then(latestVersions => formatGraphWithoutCircularDeps(rootPackage, resolveDependencies(result), latestVersions)));
}
function flattenDependencies(deps, found) {
return Object.keys(deps).reduce((acc, name) => {
const version = deps[name];
if (_.get(found, [name, 'versions', version]) === undefined) {
// eslint-disable-next-line no-use-before-define
acc.push({ name, version });
}
return acc;
}, []);
}
function validateDeps(packages) {
return packages.filter(npmPackage => {
const type = npa(`${npmPackage.name}@${npmPackage.version}`).type;
return type === 'tag' || type === 'version' || type === 'range';
});
}
function addPackageToGraph(npmPackage, found) {
return getPackage(npmPackage).then(manifest => {
if (_.get(found, [npmPackage.name, 'versions', manifest.version])) {
return Promise.resolve();
}
if (npmPackage.version === 'latest') {
_.setWith(found, [npmPackage.name, 'latestVersion'], manifest.version, Object);
}
const deps = {
deps: manifest.dependencies,
devDeps: manifest.devDependencies,
peerDeps: manifest.peerDependencies
};
_.setWith(found, [npmPackage.name, 'versions', manifest.version], deps, Object);
const flatDeps = flattenDependencies(deps.deps, found)
// devDeps seems to cause errors for many packages
// .concat(flattenDependencies(deps.devDeps, found));
.concat(flattenDependencies(deps.peerDeps, found));
const validDeps = validateDeps(flatDeps);
return Promise.all(validDeps.map(missingPackage =>
// eslint-disable-next-line no-use-before-define
buildGraph(missingPackage, found))).then(() => found);
}).catch(error => {
console.error(error);
Promise.resolve();
});
}
function buildGraph(npmPackage, found = {}) {
return new Promise(res => {
if (found[npmPackage.name] === undefined) {
addPackageToGraph({ name: npmPackage.name, version: 'latest' }, found).then(newFound => {
res(newFound);
});
}
res(found);
}).then(newFound => addPackageToGraph(npmPackage, newFound));
}
buildGraph(examplePackage).then(res => console.log('res', JSON.stringify(res, null, 2)));
module.exports = buildGraph;
{
"name": "johnny-dependency",
"version": "0.1.0",
"version": "0.2.0",
"description": "Get a graph of npm dependencies",

@@ -10,3 +10,3 @@ "main": "lib/index.js",

"build": "babel src/ -d lib/",
"start": "yarn run build && node lib/index.js",
"start": "yarn run build && node lib/cli.js",
"prepublish": "yarn run build",

@@ -33,4 +33,5 @@ "lint": "./node_modules/.bin/eslint src/",

"pacote": "^2.7.36",
"rxjs": "^5.4.1",
"untildify": "^3.0.2"
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc