Skip to content

Instantly share code, notes, and snippets.

@jonchurch
Last active February 24, 2024 20:45
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 jonchurch/29fd6f13e99ae0ea238b674b3d9e3e26 to your computer and use it in GitHub Desktop.
Save jonchurch/29fd6f13e99ae0ea238b674b3d9e3e26 to your computer and use it in GitHub Desktop.
Unique github orgs in express package.json deps. Keys are github orgs, array values are (npm) package names
{
"jshttp": [
"accepts",
"content-disposition",
"content-type",
"cookie",
"etag",
"fresh",
"http-errors",
"methods",
"on-finished",
"proxy-addr",
"range-parser",
"statuses",
"type-is",
"vary"
],
"blakeembrey": [
"array-flatten"
],
"expressjs": [
"body-parser",
"serve-static"
],
"visionmedia": [
"cookie-signature"
],
"debug-js": [
"debug"
],
"dougwilson": [
"depd"
],
"pillarjs": [
"encodeurl",
"finalhandler",
"parseurl",
"path-to-regexp",
"send"
],
"component": [
"escape-html"
],
"sindresorhus": [
"merge-descriptors"
],
"ljharb": [
"qs"
],
"feross": [
"safe-buffer"
],
"wesleytodd": [
"setprototypeof"
],
"jaredhanson": [
"utils-merge"
]
}
#!/bin/bash
# This script reads the dependencies object from package.json and uses npm info to fetch repo info
# It creates a new line delimited JSON file with the org and package name for each dep
# Exit when any command fails, treat unset variables as an error, and fail on any error within pipes.
set -euo pipefail
# set -x # Used for debugging: prints each command before it's executed.
# Ensure results.json exists and is empty.
truncate -s 0 results.json
# Extract the list of dependency package names from package.json and iterate over them.
jq -r '.dependencies | keys | .[]' package.json | while read -r package; do
# Fetch the npm package information in JSON format. The output includes various metadata, including the repository URL.
npm_info_output=$(npm info "$package" --json)
# Extract the repository URL from the npm package information. If not present, use "no repository" as a fallback.
repository_url=$(echo "$npm_info_output" | jq -r '.repository.url // "no repository"')
# Check if a repository URL was found. If so, construct a JSON object with the package name and its repository URL.
# If no repository URL is present, construct a JSON object with the package name and "no repository" as the URL.
# The `-n` option is used with `jq` to operate without requiring input from stdin.
if [[ $repository_url != "no repository" ]]; then
jq -n -c --arg pkg "$package" --arg url "$repository_url" '{package: $pkg, repository_url: $url}'
else
jq -n -c --arg pkg "$package" '{package: $pkg, repository_url: "no repository"}'
fi
# Redirect the output of the loop (JSON objects for each package) into results.json, appending to the existing content.
done >> results.ndjson
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment