【问题标题】:Where can I get NPM package information from a Github Repository?我在哪里可以从 Github 存储库获取 NPM 包信息?
【发布时间】:2022-01-25 03:14:43
【问题描述】:

我希望能够在 Github 上基于 Javascript 的存储库中获取有关 NPM 包的一些信息。

我想相信我可以从 package.json 文件的存储库中看到它们。

请对此提供任何帮助或指导?

我想使用 powershell 从文件中提取所需的信息。

【问题讨论】:

  • 我不太确定我是否理解您的问题?您需要解析package.json 文件吗?还是需要查询 NPM 数据库?
  • 我更喜欢解析 package.json 文件以获取创建的 npm 包

标签: powershell github npm


【解决方案1】:
  • 确定感兴趣的package.json 文件的原始内容的URL:

  • 然后使用 Invoke-RestMethod 和原始 URL 来检索包的 package.json 文件的内容并将其解析为您可以访问其属性的对象(下面的 for-display 输出显示左列中的属性,以及它们的属性)右侧的值 - 注意 dependencies / devDependencies 属性,表示手头的包在运行时/设计时依赖于哪些其他包):

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 <aseem.kishore@gmail.com>
contributors    : {Max Nanasy <max.nanasy@gmail.com>, Andrew Eisenberg <andrew@eisenberg.as>, Jordan Tucker <jordanbtucker@gmail.com>}
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}

以编程方式导出网址

# 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

【讨论】:

  • 所以如果我检查原始文件的输出,包在哪里?所有输出都一样吗?
  • @CalebAdepoju,我不完全理解你的问题,但看看我的更新是否有帮助。
猜你喜欢
  • 2021-04-18
  • 2014-09-29
  • 2019-02-05
  • 2015-03-05
  • 2013-10-04
  • 2020-09-28
  • 1970-01-01
  • 2019-12-11
  • 1970-01-01
相关资源
最近更新 更多