npm install package from github repo subfolder

2020-01-29 04:22发布

Is it possible to install npm package from github when the package located inside subfolder?

For example, we have Microsoft BotBuilder repository: https://github.com/Microsoft/BotBuilder

But I need to install package inside subfolder "Node/core/": https://github.com/Microsoft/BotBuilder/tree/master/Node/core/

So how can I install it with npm?

标签: git github npm
2条回答
一纸荒年 Trace。
2楼-- · 2020-01-29 04:42

Might be slightly off topic, just still relevant to the question

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

Git Submodules are git repos that you can use in other repos (henceforth, referred to as Supermodules). With each submodule having the usual assortment of branches features and tags, the benefit comes from each supermodule being a version controlled, pluggable components, that can be worked on separately or developed alongside the supermodule.

A Few Useful Commands

To add a submodule, you run the following inside your supermodule:

git submodule add <url-to-submodule-repo>

The submodule(s) still have to be initialized and fetched from the repo:

git submodule init git submodule update

A supermodule with submodules can be cloned and all submodules fetched by running:

git clone --recursive <url-to-supermodule>

You can pull upstream changes to a submodule's branch by running the following inside the submodule directory:

git fetch

Then run the following to update local code:

git merge

The following will fetch and merge for all submodules in your supermodule:

git submodule update --remote

If you want to track a specific branch of a submodule you can use the following:

git config -f .gitmodules submodule.<my-submodule>.branch fantastic_new_implementation

If you have worked on your supermodules and submodules and you push your supermodule, changes made to submodules will only exist locally and those you are collaborating with will not know of these changes. The following command will check if your submodules have been pushed BEFORE attempting to push your supermodule

git push --recurse-submodules=check

Finally, here is a useful ForEach command, that allows us to run a command for each submodule

git submodule foreach 'git checkout -b featureA

查看更多
我命由我不由天
3楼-- · 2020-01-29 04:54

Add to 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 script is running after the package is installed.

And step by step:

  1. Make folder to clone repo: mkdir BotBuilder
  2. enter to the folder: cd BotBuilder
  3. init git repo: git init
  4. set git origin to Microsoft/BotBuilder repo: git remote add -f origin https://github.com/Microsoft/BotBuilder.git
  5. enable sparse checkout: git config core.sparseCheckout true
  6. add Node/core to checkout list: echo "Node/core" >> .git/info/sparse-checkout
  7. pull part of repo: git pull --depth=1 origin master
  8. enter to Your app folder: cd ..
  9. install BotBuilder: npm i ./BotBuilder/Node/core/
查看更多
登录 后发表回答