Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@knit/find-dependencies

Package Overview
Dependencies
Maintainers
2
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@knit/find-dependencies - npm Package Compare versions

Comparing version 0.0.0-5bd66a9 to 0.0.0-812e2aa

223

__tests__/unit.test.js

@@ -1,11 +0,18 @@

/* flow */
"use strict";
import path from "path";
var _path = _interopRequireDefault(require("path"));
import pathJoin from "@knit/path-join";
var _pathJoin = _interopRequireDefault(require("@knit/path-join"));
import * as knit from "..";
var knit = _interopRequireWildcard(require(".."));
const mockPath = path;
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/* flow */
const mockPath = _path.default;
require("read-pkg").__setMockPackages({

@@ -17,28 +24,30 @@ [mockPath.join("ws", "@scope", "package")]: {},

}
},
[mockPath.join("ws", "@yarn", "workspaces")]: {
dependencies: {
extDep: 1
}
}
});
jest.mock("@knit/depcheck", () =>
jest.fn(
module =>
new Promise(resolve =>
resolve({
using: {
[mockPath.join("ws", "@scope", "package")]: { modA: [], modB: [] },
[mockPath.join("ws", "packageB")]: { modC: [], modD: [] },
[mockPath.join("ws", "modD")]: {}
}[module]
})
)
)
);
jest.mock("@knit/depcheck", () => jest.fn(module => new Promise(resolve => resolve({
using: {
[mockPath.join("ws", "@scope", "package")]: {
modA: [],
modB: []
},
[mockPath.join("ws", "packageB")]: {
modC: [],
modD: []
},
[mockPath.join("ws", "modD")]: {}
}[module]
}))));
describe("findDependencies", () => {
const fd = knit.findDependencies;
it("returns list of modules", async () => {
const ms = await fd({
workspace: "ws",
dir: pathJoin("@scope/package"),
path: pathJoin("ws/@scope/package")
dir: (0, _pathJoin.default)("@scope/package"),
path: (0, _pathJoin.default)("ws/@scope/package")
});

@@ -51,3 +60,3 @@ expect(ms).toEqual(["modA", "modB"]);

dir: "packageB",
path: pathJoin("ws/packageB")
path: (0, _pathJoin.default)("ws/packageB")
});

@@ -57,110 +66,142 @@ expect(ms).toEqual(["modC"]);

});
describe("findAllDependencies", () => {
const fad = knit.findAllDependencies;
it("returns list of dependencies used by modules", async () => {
const ms = await fad([
{
workspace: "ws",
dir: pathJoin("@scope/package"),
path: pathJoin("ws/@scope/package")
},
{
workspace: "ws",
dir: "packageB",
path: pathJoin("ws/packageB")
}
]);
const ms = await fad([{
workspace: "ws",
dir: (0, _pathJoin.default)("@scope/package"),
path: (0, _pathJoin.default)("ws/@scope/package")
}, {
workspace: "ws",
dir: "packageB",
path: (0, _pathJoin.default)("ws/packageB")
}]);
expect(ms).toEqual(["modA", "modB", "modC"]);
});
});
describe("findMissingDependencies", () => {
const md = knit.findMissingDependencies;
it("returns list of missing modules", () => {
const ms = md(["modA"], [], {});
const ms = md(["modA"], {}, {
dependencies: {}
});
expect(ms).toEqual(["modA"]);
});
it("returns empty list when nothing missing", () => {
const ms = md(["modA"], [], { modA: "1" });
const ms = md(["modA"], {}, {
dependencies: {
modA: "1"
}
});
expect(ms).toEqual([]);
});
it("does not consider internal modules as missing", () => {
const ms = md(["modA", "modB"], ["modB"], { modA: "1" });
const ms = md(["modA", "modB"], {
modB: {}
}, {
dependencies: {
modA: "1"
}
});
expect(ms).toEqual([]);
});
it("does consider internal modules as missing when in a yarn workspace", () => {
const ms = md(["modA", "@yarn/workspaces"], {
"@yarn/workspaces": {
workspace: "ws",
dir: (0, _pathJoin.default)("@yarn/workspaces"),
path: (0, _pathJoin.default)("ws/@yarn/workspaces")
}
}, {
dependencies: {
modA: "1"
},
workspaces: {}
});
expect(ms).toEqual(["@yarn/workspaces"]);
});
it("looks for dependencies on the package when in a yarn workspace", () => {
const ms = md(["extDep"], {
"@yarn/workspaces": {
workspace: "ws",
dir: (0, _pathJoin.default)("@yarn/workspaces"),
path: (0, _pathJoin.default)("ws/@yarn/workspaces")
}
}, {
dependencies: {},
workspaces: {}
});
expect(ms).toEqual([]);
});
});
describe("findAllMissingDependencies", () => {
const famd = knit.findAllMissingDependencies;
it("returns list of dependencies used by modules", async () => {
const ms = await famd(
[
{
workspace: "ws",
dir: pathJoin("@scope/package"),
path: pathJoin("ws/@scope/package")
},
{
workspace: "ws",
dir: "packageB",
path: pathJoin("ws/packageB")
}
],
{
dependencies: { modA: "1", modC: "1" }
const ms = await famd([{
workspace: "ws",
dir: (0, _pathJoin.default)("@scope/package"),
path: (0, _pathJoin.default)("ws/@scope/package")
}, {
workspace: "ws",
dir: "packageB",
path: (0, _pathJoin.default)("ws/packageB")
}], {
dependencies: {
modA: "1",
modC: "1"
}
);
});
expect(ms).toEqual(["modB"]);
});
});
describe("findUnusedDependencies", () => {
const ud = knit.findUnusedDependencies;
it("returns list of unused modules", () => {
const ms = ud([], [], { modA: 1 });
const ms = ud([], {}, {
dependencies: {
modA: 1
}
});
expect(ms).toEqual(["modA"]);
});
it("returns empty list when nothing unused", () => {
const ms = ud(["modA"], [], { modA: "1" });
const ms = ud(["modA"], {}, {
dependencies: {
modA: "1"
}
});
expect(ms).toEqual([]);
});
it("does not consider internal modules as unused", () => {
const ms = ud(["modA"], ["modB"], { modA: "1" });
const ms = ud(["modA"], ["modB"], {
dependencies: {
modA: "1"
}
});
expect(ms).toEqual([]);
});
});
describe("findAllUnusedDependencies", () => {
const faud = knit.findAllUnusedDependencies;
it("returns list of unused dependencies", async () => {
const ms = await faud(
[
{
workspace: "ws",
dir: pathJoin("@scope/package"),
path: pathJoin("ws/@scope/package")
},
{
workspace: "ws",
dir: "packageB",
path: pathJoin("ws/packageB")
}
],
{
dependencies: { modA: "1", modC: "1", modE: "1" }
const ms = await faud([{
workspace: "ws",
dir: (0, _pathJoin.default)("@scope/package"),
path: (0, _pathJoin.default)("ws/@scope/package")
}, {
workspace: "ws",
dir: "packageB",
path: (0, _pathJoin.default)("ws/packageB")
}], {
dependencies: {
modA: "1",
modC: "1",
modE: "1"
}
);
});
expect(ms).toEqual(["modE"]);
});
});
describe("findInternalDependencies", () => {
const id = knit.findInternalDependencies;
it("returns list of used internal modules", () => {

@@ -171,6 +212,4 @@ const ms = id(["modA", "modB"], ["modA"]);

});
describe("makeDependencyMap", () => {
const mdm = knit.makeDependencyMap;
it("returns a map of modules and their dependencies", async () => {

@@ -180,4 +219,4 @@ const m = await mdm({

workspace: "ws",
dir: pathJoin("@scope/package"),
path: pathJoin("ws/@scope/package")
dir: (0, _pathJoin.default)("@scope/package"),
path: (0, _pathJoin.default)("ws/@scope/package")
},

@@ -187,3 +226,3 @@ packageB: {

dir: "packageB",
path: pathJoin("ws/packageB")
path: (0, _pathJoin.default)("ws/packageB")
}

@@ -196,2 +235,2 @@ });

});
});
});

@@ -1,118 +0,72 @@

/* @flow */
"use strict";
import type { TPkgJson, TPkgJsonDeps } from "@knit/needle";
import type { TPackageNames } from "@knit/knit-core";
import type { TPackages, TPackagePaths } from "@knit/find-packages";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.makeDependencyMap = exports.findAllUnusedDependencies = exports.findAllMissingDependencies = exports.findAllDependencies = exports.findInternalDependencies = exports.findUnusedDependencies = exports.findMissingDependencies = exports.findDependencies = void 0;
import depcheck from "@knit/depcheck";
import readPkg from "@knit/read-pkg";
var _depcheck = _interopRequireDefault(require("@knit/depcheck"));
type TFindDependencies = (paths: TPackagePaths) => Promise<TPackageNames>;
export const findDependencies: TFindDependencies = paths =>
depcheck(paths.path)
.then(res => Object.keys(res.using))
.then(using => {
const pkg = readPkg(paths);
const deps = Object.keys((pkg || {}).dependencies || {});
const peers = Object.keys((pkg || {}).peerDependencies || {});
var _readPkg = _interopRequireDefault(require("@knit/read-pkg"));
return using.concat(deps).filter(d => !peers.includes(d));
})
.then(using =>
using.reduce((acc, d) => (acc.includes(d) ? acc : acc.concat(d)), [])
)
.then(using => using.sort())
.catch(e => {
throw e;
});
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
type TFindMissingDependencies = (
u: TPackageNames,
m: TPackageNames,
d: TPkgJsonDeps
) => TPackageNames;
export const findMissingDependencies: TFindMissingDependencies = (
using,
modules,
deps
) => {
const installed = Object.keys(deps);
const findDependencies = paths => (0, _depcheck.default)(paths.path).then(res => Object.keys(res.using)).then(using => {
const pkg = (0, _readPkg.default)(paths);
const deps = Object.keys((pkg || {}).dependencies || {});
const peers = Object.keys((pkg || {}).peerDependencies || {});
return using.concat(deps).filter(d => !(peers.indexOf(d) !== -1));
}).then(using => using.reduce((acc, d) => acc.indexOf(d) !== -1 ? acc : acc.concat(d), [])).then(using => using.sort()).catch(e => {
throw e;
});
return using
.filter(m => !installed.includes(m) && !modules.includes(m))
.reduce((acc, d) => (acc.includes(d) ? acc : acc.concat(d)), []);
exports.findDependencies = findDependencies;
const findMissingDependencies = (using, modules, rootDeps) => {
let installed = Object.keys(rootDeps.dependencies);
if (rootDeps.workspaces) {
installed = installed.concat(Object.keys(modules).reduce((acc, k) => acc.concat(Object.keys((0, _readPkg.default)(modules[k]).dependencies || {})), []));
}
return using.filter(m => !(installed.indexOf(m) !== -1) && (rootDeps.workspaces || !(Object.keys(modules).indexOf(m) !== -1))).reduce((acc, d) => acc.indexOf(d) !== -1 ? acc : acc.concat(d), []);
};
type TFindUnusedDependencies = (
u: TPackageNames,
m: TPackageNames,
d: TPkgJsonDeps
) => TPackageNames;
export const findUnusedDependencies: TFindUnusedDependencies = (
using,
modules,
deps
) => {
const installed = Object.keys(deps);
exports.findMissingDependencies = findMissingDependencies;
return installed
.filter(m => !using.includes(m) && !modules.includes(m))
.reduce((acc, d) => (acc.includes(d) ? acc : acc.concat(d)), []);
const findUnusedDependencies = (using, modules, rootDeps) => {
let installed = Object.keys(rootDeps.dependencies);
if (rootDeps.workspaces) {
installed = installed.concat(Object.keys(modules).reduce((acc, k) => acc.concat(Object.keys((0, _readPkg.default)(modules[k]).dependencies || {})), []));
}
return installed.filter(m => !(using.indexOf(m) !== -1) && (rootDeps.workspaces || !(Object.keys(modules).indexOf(m) !== -1))).reduce((acc, d) => acc.indexOf(d) !== -1 ? acc : acc.concat(d), []);
};
type TFindInternalDependencies = (
u: TPackageNames,
m: TPackageNames
) => TPackageNames;
export const findInternalDependencies: TFindInternalDependencies = (
using,
modules
) =>
using
.filter(m => modules.includes(m))
.reduce((acc, d) => (acc.includes(d) ? acc : acc.concat(d)), []);
exports.findUnusedDependencies = findUnusedDependencies;
type TFindAllDependencies = (m: TPackages) => Promise<TPackageNames>;
export const findAllDependencies: TFindAllDependencies = async modules =>
(await Promise.all(
Object.keys(modules).reduce((acc, mod) => {
const deps = findDependencies(modules[mod]);
return acc.concat(deps);
}, [])
)).reduce((a, m) => (a.includes(m) ? a : a.concat(m)), []);
const findInternalDependencies = (using, modules) => using.filter(m => modules.indexOf(m) !== -1).reduce((acc, d) => acc.indexOf(d) !== -1 ? acc : acc.concat(d), []);
type TFindAllMissingDependencies = (
m: TPackages,
rootPkg: TPkgJson
) => Promise<TPackageNames>;
export const findAllMissingDependencies: TFindAllMissingDependencies = (
modules,
rootPkg
) =>
findAllDependencies(modules).then(using =>
findMissingDependencies(using, Object.keys(modules), rootPkg.dependencies)
);
exports.findInternalDependencies = findInternalDependencies;
type TFindAllUnusedDependencies = (
m: TPackages,
rootPkg: TPkgJson
) => Promise<TPackageNames>;
export const findAllUnusedDependencies: TFindAllUnusedDependencies = (
modules,
rootPkg
) =>
findAllDependencies(modules).then(using =>
findUnusedDependencies(using, Object.keys(modules), rootPkg.dependencies)
);
const findAllDependencies = async modules => (await Promise.all(Object.keys(modules).reduce((acc, mod) => {
const deps = findDependencies(modules[mod]);
return acc.concat(deps);
}, []))).reduce((a, m) => a.indexOf(m) !== -1 ? a : a.concat(m), []);
type TMakeDependencyMap = (
m: TPackages
) => Promise<{ [k: string]: TPackageNames }>;
export const makeDependencyMap: TMakeDependencyMap = async modules =>
Object.keys(modules).reduce(
async (acc, mod) => ({
...(await acc),
[mod]: await findDependencies(modules[mod])
}),
{}
);
exports.findAllDependencies = findAllDependencies;
const findAllMissingDependencies = (modules, rootPkg) => findAllDependencies(modules).then(using => findMissingDependencies(using, modules, rootPkg));
exports.findAllMissingDependencies = findAllMissingDependencies;
const findAllUnusedDependencies = (modules, rootPkg) => findAllDependencies(modules).then(using => findUnusedDependencies(using, modules, rootPkg));
exports.findAllUnusedDependencies = findAllUnusedDependencies;
const makeDependencyMap = async modules => Object.keys(modules).reduce(async (acc, mod) => ({ ...(await acc),
[mod]: await findDependencies(modules[mod])
}), {});
exports.makeDependencyMap = makeDependencyMap;
{
"homepage": "https://github.com/knitjs/knit#readme",
"license": "MIT",
"author": {
"name": "Shane Wilson"
},
"bugs": {
"url": "https://github.com/knitjs/knit/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/knitjs/knit.git"
},
"engines": {
"node": ">=10",
"npm": ">=5"
},
"private": false,
"name": "@knit/find-dependencies",
"version": "0.0.0-5bd66a9"
"description": "Find dependenceis of monorepo packages",
"version": "0.0.0-812e2aa",
"dependencies": {
"@knit/depcheck": "0.0.0-812e2aa",
"@knit/find-packages": "0.0.0-812e2aa",
"@knit/knit-core": "0.0.0-812e2aa",
"@knit/needle": "0.0.0-812e2aa",
"@knit/read-pkg": "0.0.0-812e2aa"
}
}
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