从Github仓库子文件夹安装软件包的npm命令

82
5个回答

39
将 GitHub 子文件夹的链接复制到 Gitpkg 中。然后便可使用 yarn 或 npm 与其一同安装 github 子文件夹中的软件包。 https://gitpkg.now.sh/

3
我的感兴趣的仓库一直出现502错误。例如公共仓库:https://gitpkg.now.sh/vendure-ecommerce/vendure/packages/core?master - s3m3n
@s3m3n,我认为你的核心包或其某些依赖项中的一些postinstall脚本失败了,我尝试了一下但无法弄清楚:(另外,我得到了一个“500内部服务器错误”,而不是502。 - farukg
由于目前 GitPkg 不支持 yarn2 (berry),一个临时的解决方案可以是:(1) 下载包代码,(2) 用 npm 构建和打包它,(3) 将结果 tarball 复制到我的项目中,最后 (4) 通过 yarn add <file> 将其作为依赖项添加。 - Evgeniya Manolova
1
服务好像已经停止运行了? - mmomtchev
gitpkg是否支持私有GitHub仓库?在我看来似乎不支持,或者也许有熟悉它的人可以指导我吗? - Wayne Mao

38

请添加到package.json文件中:

...
"scripts": {
  "postinstall": "mkdir BotBuilder; cd BotBuilder; git init; git remote add -f origin https://github.com/Microsoft/BotBuilder.git; git config core.sparseCheckout true; echo \"Node/core\" >> .git/info/sparse-checkout; git pull --depth=1 origin master; cd ..; npm i ./BotBuilder/Node/core/"
  ...
},
...

postinstall脚本在安装完包后执行。

然后按照以下步骤进行:

  1. 创建文件夹以克隆存储库:mkdir BotBuilder
  2. 进入文件夹:cd BotBuilder
  3. 初始化git仓库:git init
  4. 将git origin设置为Microsoft/BotBuilder repo:git remote add -f origin https://github.com/Microsoft/BotBuilder.git
  5. 启用sparse checkoutgit config core.sparseCheckout true
  6. Node/core添加到checkout列表中:echo "Node/core" >> .git/info/sparse-checkout
  7. 拉取部分存储库:git pull --depth=1 origin master
  8. 进入您的应用程序文件夹:cd ..
  9. 安装BotBuilder: npm i ./BotBuilder/Node/core/

1
请参阅 https://dev59.com/2l4c5IYBdhLWcg3wv8hl#30584951 以获取有关该存储库的特定拉取请求。 - William Entriken
git submodule update -i -r - Jeton

29
如果软件包源托管在GitHub上,你可以像这样使用GitPkg
# using npm:
npm install https://gitpkg.now.sh/<user>/<project>/<subdir>?<commit-ish>
# using yarn:
yarn add https://gitpkg.now.sh/<user>/<project>/<subdir>?<commit-ish>

针对您的特定情况,URL 将是这个:

https://gitpkg.now.sh/Microsoft/BotBuilder/Node/core?master

他们的网站上有一个很好的向导式表单,可以帮助构建URL并安装命令。


我遇到了 TAR_BAD_ARCHIVE: 未识别的归档格式。 - Daniel
@Daniel 请提供更多上下文。你做了什么? - Shayan Toqraee

2
@Tomasz Jakub Rup的回答启发,我更新了他的示例,并指向了基于3.0分支的示例,并使用了新的git功能sparse-checkout。这个功能将节省时间/带宽,因为它不需要提前克隆整个存储库,只会获取你指定的内容。然而,许多服务器不支持--filter选项,这是减少空间的主要方式,但在许多情况下,--depth 1仍将减少带宽。

我使用了一个.tmp_npm文件夹来最小化覆盖,并且由于是隐藏文件,可能被.gitignored忽略。

"scripts": {
  "postinstall": "mkdir .tmp_npm; cd .tmp_npm; git init; git clone --filter=blob:none --no-checkout --depth 1 --sparse -b 3.0 https://github.com/Microsoft/BotBuilder.git; cd BotBuilder/; git sparse-checkout init --cone; git sparse-checkout add Node/core; git checkout; cd ../..; npm i .tmp_npm/BotBuilder/Node/core/"
  ...
},

1
可能有些离题,但仍与问题相关。

https://git-scm.com/book/en/v2/Git-Tools-Submodules

Git子模块是可以在其他仓库中使用的git存储库(以下简称超级模块)。每个子模块都具有通常的分支功能和标签,好处在于每个超级模块都是可版本控制的、可插拔的组件,可以单独工作或与超级模块一起开发。
一些有用的命令:
要添加一个子模块,您需要在超级模块内运行以下命令:
git submodule add <url-to-submodule-repo>

子模块仍需从仓库初始化和获取:
git submodule init git submodule update 可以通过运行以下命令克隆具有子模块的超级模块并获取所有子模块:
git clone --recursive 您可以在子模块目录内运行以下命令将上游更改拉到子模块的分支中:
git fetch
然后运行以下命令以更新本地代码:
git merge 以下命令将获取和合并超级模块中的所有子模块:
git submodule update --remote 如果您想跟踪子模块的特定分支,可以使用以下命令:
git config -f .gitmodules submodule..branch fantastic_new_implementation
如果您已经在超级模块和子模块上工作,并且推送了超级模块,则对子模块所做的更改仅存在于本地,并且您正在与之合作的人不会知道这些更改。 在尝试推送超级模块之前,以下命令将检查是否已推送子模块:
git push --recurse-submodules=check
最后,这里有一个有用的ForEach命令,允许我们为每个子模块运行一个命令。 git submodule foreach 'git checkout -b featureA'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接