Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(deps-script): only walk deps for ours #5635

Merged
merged 1 commit into from Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 23 additions & 5 deletions DEPENDENCIES.md
Expand Up @@ -32,6 +32,7 @@ graph LR;
libnpmdiff-->npmcli-template-oss["@npmcli/template-oss"];
libnpmdiff-->pacote;
libnpmexec-->bin-links;
libnpmexec-->minify-registry-metadata;
libnpmexec-->npm-package-arg;
libnpmexec-->npmcli-arborist["@npmcli/arborist"];
libnpmexec-->npmcli-ci-detect["@npmcli/ci-detect"];
Expand Down Expand Up @@ -74,6 +75,7 @@ graph LR;
libnpmteam-->npm-registry-fetch;
libnpmteam-->npmcli-eslint-config["@npmcli/eslint-config"];
libnpmteam-->npmcli-template-oss["@npmcli/template-oss"];
libnpmversion-->json-parse-even-better-errors;
libnpmversion-->npmcli-eslint-config["@npmcli/eslint-config"];
libnpmversion-->npmcli-git["@npmcli/git"];
libnpmversion-->npmcli-run-script["@npmcli/run-script"];
Expand All @@ -92,6 +94,7 @@ graph LR;
npm-->hosted-git-info;
npm-->ini;
npm-->init-package-json;
npm-->json-parse-even-better-errors;
npm-->libnpmaccess;
npm-->libnpmdiff;
npm-->libnpmexec;
Expand All @@ -108,6 +111,7 @@ graph LR;
npm-->npm-audit-report;
npm-->npm-install-checks;
npm-->npm-package-arg;
npm-->npm-pick-manifest;
npm-->npm-profile;
npm-->npm-registry-fetch;
npm-->npm-user-validate;
Expand Down Expand Up @@ -141,6 +145,10 @@ graph LR;
npm-package-arg-->semver;
npm-package-arg-->validate-npm-package-name;
npm-packlist-->ignore-walk;
npm-pick-manifest-->npm-install-checks;
npm-pick-manifest-->npm-normalize-package-bin;
npm-pick-manifest-->npm-package-arg;
npm-pick-manifest-->semver;
npm-profile-->npm-registry-fetch;
npm-profile-->proc-log;
npm-registry-fetch-->make-fetch-happen;
Expand All @@ -149,9 +157,12 @@ graph LR;
npm-registry-fetch-->proc-log;
npmcli-arborist-->bin-links;
npmcli-arborist-->cacache;
npmcli-arborist-->json-parse-even-better-errors;
npmcli-arborist-->minify-registry-metadata;
npmcli-arborist-->nopt;
npmcli-arborist-->npm-install-checks;
npmcli-arborist-->npm-package-arg;
npmcli-arborist-->npm-pick-manifest;
npmcli-arborist-->npm-registry-fetch;
npmcli-arborist-->npmcli-eslint-config["@npmcli/eslint-config"];
npmcli-arborist-->npmcli-installed-package-contents["@npmcli/installed-package-contents"];
Expand Down Expand Up @@ -180,6 +191,7 @@ graph LR;
npmcli-config-->read-package-json-fast;
npmcli-config-->semver;
npmcli-fs-->semver;
npmcli-git-->npm-pick-manifest;
npmcli-git-->npmcli-promise-spawn["@npmcli/promise-spawn"];
npmcli-git-->proc-log;
npmcli-git-->semver;
Expand All @@ -188,8 +200,10 @@ graph LR;
npmcli-map-workspaces-->npmcli-name-from-folder["@npmcli/name-from-folder"];
npmcli-map-workspaces-->read-package-json-fast;
npmcli-metavuln-calculator-->cacache;
npmcli-metavuln-calculator-->json-parse-even-better-errors;
npmcli-metavuln-calculator-->pacote;
npmcli-metavuln-calculator-->semver;
npmcli-package-json-->json-parse-even-better-errors;
npmcli-promise-spawn-->infer-owner;
npmcli-query-->npm-package-arg;
npmcli-query-->semver;
Expand All @@ -203,6 +217,7 @@ graph LR;
pacote-->infer-owner;
pacote-->npm-package-arg;
pacote-->npm-packlist;
pacote-->npm-pick-manifest;
pacote-->npm-registry-fetch;
pacote-->npmcli-git["@npmcli/git"];
pacote-->npmcli-installed-package-contents["@npmcli/installed-package-contents"];
Expand All @@ -212,10 +227,13 @@ graph LR;
pacote-->read-package-json-fast;
pacote-->read-package-json;
pacote-->ssri;
parse-conflict-json-->json-parse-even-better-errors;
promzard-->read;
read-->mute-stream;
read-package-json-->json-parse-even-better-errors;
read-package-json-->normalize-package-data;
read-package-json-->npm-normalize-package-bin;
read-package-json-fast-->json-parse-even-better-errors;
read-package-json-fast-->npm-normalize-package-bin;
readdir-scoped-modules-->dezalgo;
unique-filename-->unique-slug;
Expand Down Expand Up @@ -760,8 +778,8 @@ packages higher up the chain.
- @npmcli/arborist
- @npmcli/metavuln-calculator
- pacote, libnpmaccess, libnpmhook, libnpmorg, libnpmsearch, libnpmteam, npm-profile
- npm-registry-fetch
- make-fetch-happen, libnpmversion, @npmcli/config, init-package-json
- @npmcli/installed-package-contents, @npmcli/map-workspaces, cacache, @npmcli/git, @npmcli/run-script, read-package-json, @npmcli/query, readdir-scoped-modules, promzard
- npm-bundled, read-package-json-fast, @npmcli/fs, unique-filename, @npmcli/promise-spawn, npm-package-arg, npm-packlist, normalize-package-data, bin-links, nopt, npm-install-checks, npmlog, dezalgo, read
- npm-normalize-package-bin, @npmcli/name-from-folder, semver, @npmcli/move-file, fs-minipass, infer-owner, ssri, unique-slug, proc-log, @npmcli/node-gyp, hosted-git-info, validate-npm-package-name, ignore-walk, minipass-fetch, @npmcli/package-json, cmd-shim, read-cmd-shim, write-file-atomic, abbrev, are-we-there-yet, gauge, parse-conflict-json, wrappy, treeverse, @npmcli/eslint-config, @npmcli/template-oss, @npmcli/disparity-colors, @npmcli/ci-detect, mute-stream, ini, npm-audit-report, npm-user-validate
- npm-registry-fetch, libnpmversion
- @npmcli/git, make-fetch-happen, @npmcli/config, init-package-json
- @npmcli/installed-package-contents, @npmcli/map-workspaces, cacache, npm-pick-manifest, @npmcli/run-script, read-package-json, @npmcli/query, readdir-scoped-modules, promzard
- npm-bundled, read-package-json-fast, @npmcli/fs, unique-filename, @npmcli/promise-spawn, npm-install-checks, npm-package-arg, npm-packlist, normalize-package-data, @npmcli/package-json, bin-links, nopt, npmlog, parse-conflict-json, dezalgo, read
- npm-normalize-package-bin, @npmcli/name-from-folder, json-parse-even-better-errors, semver, @npmcli/move-file, fs-minipass, infer-owner, ssri, unique-slug, hosted-git-info, proc-log, validate-npm-package-name, @npmcli/node-gyp, ignore-walk, minipass-fetch, cmd-shim, read-cmd-shim, write-file-atomic, abbrev, are-we-there-yet, gauge, wrappy, treeverse, @npmcli/eslint-config, @npmcli/template-oss, minify-registry-metadata, @npmcli/disparity-colors, @npmcli/ci-detect, mute-stream, ini, npm-audit-report, npm-user-validate
86 changes: 58 additions & 28 deletions scripts/dependency-graph.js
@@ -1,9 +1,17 @@
#!/usr/bin/env node

'use strict'

// Generates our dependency graph documents in DEPENDENCIES.md.

const Arborist = require('@npmcli/arborist')
const mapWorkspaces = require('@npmcli/map-workspaces')
const fs = require('fs')
const log = require('proc-log')

if (process.argv.includes('--debug')) {
process.on('log', console.error)
}

// To re-create npm-cli-repos.txt run:
/* eslint-disable-next-line max-len */
Expand Down Expand Up @@ -66,6 +74,7 @@ function escapeName (name) {
}
return name
}

function stripName (name) {
if (name.startsWith('@')) {
const parts = name.slice(1).split('/')
Expand All @@ -75,21 +84,23 @@ function stripName (name) {
}

const main = async function () {
// add all of the cli's public workspaces as package names
const workspaces = await mapWorkspaces({ pkg: require('../package.json') })
for (const [key, value] of workspaces.entries()) {
if (!require(value + '/package.json').private) {
repos.push(key)
}
}

const arborist = new Arborist({
prefix: process.cwd(),
path: process.cwd(),
})
const tree = await arborist.loadVirtual({ path: process.cwd(), name: 'npm' })
tree.name = 'npm'

const {
heirarchy: heirarchyOurs,
annotations: annotationsOurs,
} = walk(tree, true)

const {
annotations: annotationsAll,
} = walk(tree, false)
const [annotationsOurs, heirarchyOurs] = walk(tree, true)
const [annotationsAll] = walk(tree, false)

const out = [
'# npm dependencies',
Expand Down Expand Up @@ -121,31 +132,52 @@ const main = async function () {
const walk = function (tree, onlyOurs) {
const annotations = [] // mermaid dependency annotations
const dependedBy = {}

iterate(tree, dependedBy, annotations, onlyOurs)

const allDeps = new Set(Object.keys(dependedBy))
const foundDeps = new Set()
const heirarchy = []
while (allDeps.size) {
const level = []
for (const dep of allDeps) {
if (!dependedBy[dep].size) {
level.push(dep)
foundDeps.add(dep)

if (onlyOurs) {
while (allDeps.size) {
log.silly('SIZE', allDeps.size)
const level = []

for (const dep of allDeps) {
log.silly(dep, '::', [...dependedBy[dep]].join(', '))
log.silly('-'.repeat(80))

if (!dependedBy[dep].size) {
level.push(dep)
foundDeps.add(dep)
}
}
}
for (const dep of allDeps) {
for (const found of foundDeps) {
allDeps.delete(found)
dependedBy[dep].delete(found)

log.silly('LEVEL', level.length)
log.silly('FOUND', foundDeps.size)

for (const dep of allDeps) {
for (const found of foundDeps) {
allDeps.delete(found)
dependedBy[dep].delete(found)
}
}

log.silly('SIZE', allDeps.size)

if (!level.length) {
const remaining = `Remaining deps: ${[...allDeps.keys()]}`
throw new Error(`Would do an infinite loop here, need to debug. ${remaining}`)
}

heirarchy.push(level.join(', '))
log.silly('HIEARARCHY', heirarchy.length)
log.silly('='.repeat(80))
}
if (!level.length) {
throw new Error('Would do an infinite loop here, need to debug')
}
heirarchy.push(level.join(', '))
}

return { heirarchy, annotations }
return [annotations, heirarchy]
}
const iterate = function (node, dependedBy, annotations, onlyOurs) {
if (!dependedBy[node.packageName]) {
Expand All @@ -167,10 +199,8 @@ const iterate = function (node, dependedBy, annotations, onlyOurs) {
}

main().then(() => {
process.exit(0)
return 0
return process.exit(0)
}).catch(err => {
console.error(err)
process.exit(1)
return 1
return process.exit(1)
})
19 changes: 5 additions & 14 deletions scripts/npm-cli-repos.txt
@@ -1,6 +1,5 @@
abbrev-js
agent
arborist
wraithgar marked this conversation as resolved.
Show resolved Hide resolved
are-we-there-yet
benchmarks
bin-links
Expand All @@ -20,26 +19,19 @@ fs
fs-minipass
gauge
git
graphec
hosted-git-info
ignore-walk
infer-owner
inflight
ini
init-package-json
installed-package-contents
libnpmaccess
libnpmfund
libnpmhook
libnpmorg
libnpmpack
libnpmpublish
libnpmsearch
libnpmteam
libnpmversion
lint
json-parse-even-better-errors
make-fetch-happen
map-workspaces
metavuln-calculator
minify-registry-metadata
minipass-fetch
move-file
mute-stream
Expand All @@ -52,13 +44,14 @@ node-which
nopt
normalize-package-data
npm-audit-report
npm-birthday
npm-bundled
npm-cli-release-please
npm-install-checks
npm-install-script
npm-normalize-package-bin
npm-package-arg
npm-packlist
npm-pick-manifest
npm-profile
npm-registry-fetch
npm-registry-mock
Expand All @@ -71,7 +64,6 @@ proc-log
proggy
promise-spawn
promzard
pull
query
read
read-cmd-shim
Expand All @@ -83,7 +75,6 @@ run-script
ssri
stafftools
statusboard
stringify-package
tap-nock
template-oss
treeverse
Expand Down