Where can I get NPM package information from a Github Repository?
-
Determine the URL for the raw content of the
package.json
file of interest:- E.g., for the
json5
package, click on theRepository
link to go to the associated GitHub repository, navigate to thepackage.json
file and click on theRaw
button to get the raw URL.
- E.g., for the
-
Then use
Invoke-RestMethod
with the raw URL to retrieve and parse the content of the package'spackage.json
file into an object whose properties you can access (the for-display output below shows the properties in the left column, and their values to the right - note thedependencies
/devDependencies
properties denoting what other packages the package at hand depends on at runtime / design time):
PS> Invoke-RestMethod https://raw.githubusercontent.com/json5/json5/master/package.json
name : json5
version : 2.2.0
description : JSON for humans.
main : lib/index.js
module : dist/index.mjs
bin : lib/cli.js
browser : dist/index.js
types : lib/index.d.ts
files : {lib/, dist/}
engines : @{node=>=6}
scripts : @{build=rollup -c; build-package=node build/package.js; build-unicode=node build/unicode.js; coverage=tap --coverage-report html test; lint=eslint --fix .; prepublishOnly=npm run production; preversion=npm run production; production=npm run lint && npm test && npm run build; test=tap -Rspec --100 test; version=npm run build-package && git add package.json5}
repository : @{type=git; url=git+https://github.com/json5/json5.git}
keywords : {json, json5, es5, es2015…}
author : Aseem Kishore <[email protected]>
contributors : {Max Nanasy <[email protected]>, Andrew Eisenberg <[email protected]>, Jordan Tucker <[email protected]>}
license : MIT
bugs : @{url=https://github.com/json5/json5/issues}
homepage : http://json5.org/
dependencies : @{minimist=^1.2.5}
devDependencies : @{core-js=^2.6.5; eslint=^5.15.3; eslint-config-standard=^12.0.0; eslint-plugin-import=^2.16.0; eslint-plugin-node=^8.0.1; eslint-plugin-promise=^4.0.1; eslint-plugin-standard=^4.0.0; regenerate=^1.4.0; rollup=^0.64.1; rollup-plugin-buble=^0.19.6; rollup-plugin-commonjs=^9.2.1; rollup-plugin-node-resolve=^3.4.0; rollup-plugin-terser=^1.0.1; sinon=^6.3.5; tap=^12.6.0; unicode-10.0.0=^0.7.5}
Deriving the URLs programmatically:
# Package name.
$npmPackageName = 'json5'
# Derive the package's npm registry URL
$npmUrl = "https://www.npmjs.com/package/$npmPackageName"
# Derive the associated API URL
$npmApiUrl = $npmUrl -replace '(?<=/)www(?=.)', 'replicate' -replace '/package'
# Derive the source-code repository URL
$repoUrl = Invoke-RestMethod $npmApiUrl |
ForEach-Object { $_.repository.url.Substring($_.repository.type.Length+1) }
# Assuming the source-code repository is a GitHub URL,
# derive the *raw* URL from it, from which files can be downloaded
# as-is.
$rawGitHubUrl= 'https://raw.githubusercontent.com/' + ($repoUrl -replace '\.git$' -replace '^https://github\.com/')
# Derive the raw URL for the package.json file.
$rawPackageJsonUrl = "$rawGitHubUrl/master/package.json"
# Parse the package.json file's JSON content into an object
$objectFromPackageJson = Invoke-RestMethod $rawPackageJsonUrl
# Output an example property.
$objectFromPackageJson.dependencies