本文根据download-git-repo官方文档和个人阅读理解进行翻译。

 

用node下载并提取一个 git repository (GitHub, GitLab, Bitbucket) 

安装

$ npm install download-git-repo

API

download(repository, destination, options, callback)

下载一个 git repository 到 destination 文件夹,配置参数 options, 和 callback回调.

repository

一、可以采用下面简写方式

  • GitHub - github:owner/name 或者 owner/name
  • GitLab - gitlab:owner/name
  • Bitbucket - bitbucket:owner/name

1、默认是 master 分枝, 但你可以指定分枝和tag ,如 owner/name#my-branch

2、你还可以指定自定义来源,如 gitlab:custom.com:owner/name. 自定义来源默认为 https 或 git@ , 你也可以自己之定义协议. 

 

二、Direct - direct:url方式

这种方式会跳过上面简写的方式,直接传递 url.

1、如果使用 direct,并且没有 clone配置项, 你必须传入完整的zip文件地址, 包括分枝(如果需要的话). 

2、如果使用 direct 并带有 clone配置项, 你必须传入完整的 git repo url , 你可以通过 direct:url#my-branch指定分枝.

 

destination

下载仓库的文件路径

 

options(可选)配置对象:

callback

回调函数,会传入err.

例子

简写形式

使用http 下载 Github repository master 分枝.

download('flippidippi/download-git-repo-fixture', 'test/tmp', function (err) {

  console.log(err ? 'Error' : 'Success')

})

使用 git clone 下载 Bitbucket repository  my-branch 分枝.

download('bitbucket:flippidippi/download-git-repo-fixture#my-branch', 'test/tmp', { clone: true }, function (err) {

  console.log(err ? 'Error' : 'Success')

})

使用 http 下载 GitLab 自定义来源仓库,并附带 token.

download('gitlab:mygitlab.com:flippidippi/download-git-repo-fixture#my-branch', 'test/tmp', { headers: { 'PRIVATE-TOKEN': '1234' } } function (err) {

  console.log(err ? 'Error' : 'Success')

})

使用git clone 下载自定义来源和协议 GitLab 仓库. 如果clone 一个自定义来源的仓库,tpye (githubgitlab 等.) 不是必须的.

download('https://mygitlab.com:flippidippi/download-git-repo-fixture#my-branch', 'test/tmp', { clone: true }, function (err) {

  console.log(err ? 'Error' : 'Success')

})

Direct 方式

direct url http下载方式.

download('direct:https://gitlab.com/flippidippi/download-git-repo-fixture/repository/archive.zip', 'test/tmp', function (err) {

  console.log(err ? 'Error' : 'Success')

})

使用direct url  git clone  方式下载master分枝.

download('direct:https://gitlab.com/flippidippi/download-git-repo-fixture.git', 'test/tmp', { clone: true }, function (err) {

  console.log(err ? 'Error' : 'Success')

})

使用direct url  git clone  方式下载 my-branch分枝.

download('direct:https://gitlab.com/flippidippi/download-git-repo-fixture.git#my-branch', 'test/tmp', { clone: true }, function (err) {

  console.log(err ? 'Error' : 'Success')

})

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐