@boses/github-clone 1.0.9 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +80 -40
- package/dist/gitClone.esm.js +2 -0
- package/dist/gitClone.esm.js.map +1 -0
- package/dist/gitClone.js +2 -0
- package/dist/gitClone.js.map +1 -0
- package/dist/main.js +3 -0
- package/dist/main.js.map +1 -0
- package/dist/src/config.d.ts +11 -0
- package/dist/src/gitClone.d.ts +8 -0
- package/dist/src/main.d.ts +2 -0
- package/dist/src/utils.d.ts +17 -0
- package/package.json +64 -32
- package/dist/package.json +0 -57
- package/dist/src/config.js +0 -22
- package/dist/src/fs.js +0 -59
- package/dist/src/main.js +0 -55
- package/dist/src/utils.js +0 -80
package/README.md
CHANGED
|
@@ -1,86 +1,126 @@
|
|
|
1
|
-
#
|
|
1
|
+
# GitHub-clone
|
|
2
2
|
|
|
3
|
-
  
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
为了解决国内 GitHub clone 速度慢较慢的工具
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
> 注意以下文档书写形式遵循下述规范:
|
|
8
|
+
>
|
|
9
|
+
> `[]`代表这个字段必填,`<>`则为选填,而`name?:string`,表示这个参数为非必填为`string`类型。
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
## 执行流程
|
|
10
12
|
|
|
11
|
-
|
|
13
|
+
- 首先将输入的 `github.com`替换成设置的镜像网站地址,默认为(github.com.cnpmjs.org)
|
|
14
|
+
- 执行 `git clone [url]` 的操作
|
|
15
|
+
- 拉取镜像完成之后,重写 git 的远程仓库推送地址,将镜像推送地址重置为 github 的镜像地址
|
|
16
|
+
|
|
17
|
+
## CLI 使用方式
|
|
18
|
+
|
|
19
|
+
### 安装
|
|
12
20
|
|
|
13
21
|
```sh
|
|
14
|
-
npm i -g @boses/github-clone
|
|
15
|
-
# or
|
|
16
22
|
yarn global add @boses/github-clone
|
|
17
23
|
```
|
|
18
24
|
|
|
19
|
-
|
|
25
|
+
之后通过`g clone <path>`形式来使用,更多`API`和`clone`的使用方法可以调用`g ---help`查看。
|
|
20
26
|
|
|
21
|
-
|
|
27
|
+
### API
|
|
22
28
|
|
|
23
|
-
|
|
29
|
+
#### Clone
|
|
24
30
|
|
|
25
|
-
|
|
31
|
+
**clone <path> [dir] <--branch [branchName]>**
|
|
26
32
|
|
|
27
|
-
-
|
|
28
|
-
|
|
33
|
+
- `<path>`
|
|
34
|
+
|
|
35
|
+
- type:`stirng`
|
|
36
|
+
- require:`true`
|
|
37
|
+
|
|
38
|
+
拉取 GitHub 仓库 对应的地址,可以拉取以下三种类型地址
|
|
39
|
+
|
|
40
|
+
| 类型 | 说明 |
|
|
41
|
+
| ------------------------------------------------ | ----------------------------- |
|
|
42
|
+
| https://github.com/bosens-China/github-clone | 默认浏览器导航栏的地址 |
|
|
43
|
+
| https://github.com/bosens-China/github-clone.git | Github 右侧 Code HTTPS 的地址 |
|
|
44
|
+
| git@github.com:bosens-China/breeze-clone.git | Github 右侧 Code SSH 的地址 |
|
|
45
|
+
|
|
46
|
+
- `[dir]`
|
|
47
|
+
|
|
48
|
+
- type:`stirng`
|
|
49
|
+
- require:`false`
|
|
29
50
|
|
|
30
|
-
|
|
51
|
+
clone 到本地的目录名称
|
|
31
52
|
|
|
32
|
-
|
|
53
|
+
- `--branch [branchName]`
|
|
33
54
|
|
|
34
|
-
|
|
55
|
+
- type:`stirng`
|
|
56
|
+
- require:`false`
|
|
35
57
|
|
|
36
|
-
|
|
58
|
+
指定拉取的分支名称,可以以`--branch`长形式使用也可以以`-b`的短形式使用,例如:
|
|
37
59
|
|
|
38
|
-
|
|
60
|
+
```sh
|
|
61
|
+
g clone https://github.com/bosens-China/github-clone.git -b dev
|
|
62
|
+
```
|
|
39
63
|
|
|
40
|
-
|
|
64
|
+
#### set [url]
|
|
41
65
|
|
|
42
|
-
|
|
66
|
+
- `[url]`
|
|
43
67
|
|
|
44
|
-
|
|
68
|
+
- type:`stirng`
|
|
69
|
+
- require:`true`
|
|
45
70
|
|
|
46
|
-
|
|
47
|
-
- require: `false`
|
|
48
|
-
- default: `undefined`
|
|
71
|
+
用于配置镜像网站
|
|
49
72
|
|
|
50
|
-
|
|
73
|
+
#### get
|
|
51
74
|
|
|
52
|
-
|
|
75
|
+
返回用户配置的`set [url]`地址,默认为`github.com.cnpmjs.org`
|
|
53
76
|
|
|
54
|
-
|
|
55
|
-
- require: `false`
|
|
56
|
-
- default: `undefined`
|
|
77
|
+
## Node
|
|
57
78
|
|
|
58
|
-
|
|
79
|
+
### 安装
|
|
59
80
|
|
|
60
81
|
```sh
|
|
61
|
-
|
|
82
|
+
yarn add @boses/github-clone
|
|
62
83
|
```
|
|
63
84
|
|
|
64
|
-
|
|
85
|
+
```js
|
|
86
|
+
const clone = require('@boses/github-clone');
|
|
87
|
+
// 也可以通过es模块引用
|
|
88
|
+
// import clone from '@boses/github-clone/gitClone.esm'
|
|
89
|
+
clone('https://github.com/SunshowerC/blog');
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
> clone 会以同步的形式运行,记得使用 `try` 包裹住可能的错误
|
|
93
|
+
|
|
94
|
+
### API
|
|
95
|
+
|
|
96
|
+
`clone: (url: string, options: Partial<Options>) => void`
|
|
65
97
|
|
|
66
98
|
#### url
|
|
67
99
|
|
|
68
|
-
- type:`
|
|
69
|
-
- require: `
|
|
70
|
-
- default: `github.com.cnpmjs.org`
|
|
100
|
+
- type:`string`
|
|
101
|
+
- require: `true`
|
|
71
102
|
|
|
72
|
-
|
|
103
|
+
拉取的 GitHub 仓库地址
|
|
73
104
|
|
|
74
|
-
|
|
105
|
+
#### options
|
|
75
106
|
|
|
76
|
-
|
|
107
|
+
| 名称 | 类型 | 是否必填 | 描述 |
|
|
108
|
+
| ------------- | --------- | -------- | ------------------------------------------ |
|
|
109
|
+
| dir | `string` | `false` | 拉取的目录名称 |
|
|
110
|
+
| branch | `string` | `false` | 拉取的分支名称 |
|
|
111
|
+
| mirrorAddress | `string` | `false` | 镜像网站,如果你需要使用镜像可以填写此网站 |
|
|
112
|
+
| silence | `boolean` | `false` | 是否静默模式执行 clone |
|
|
77
113
|
|
|
78
114
|
## 其他
|
|
79
115
|
|
|
116
|
+
目前版本更新导致对`1.0.9`之前的`g get`不支持,请重新执行`g set [url]`的操作
|
|
117
|
+
|
|
80
118
|
如果发现错误或者需要有更好的建议欢迎在 [issues](https://github.com/bosens-China/github-clone) 中提出
|
|
81
119
|
|
|
82
120
|
## 参考
|
|
83
|
-
|
|
121
|
+
|
|
122
|
+
- [知乎 git clone 一个 github 上的仓库,太慢,经常连接失败...](https://www.zhihu.com/question/27159393/answer/1117219745)
|
|
123
|
+
|
|
84
124
|
## 协议
|
|
85
125
|
|
|
86
126
|
[MIT License](/License)
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{execSync as t,spawnSync as r}from"child_process";import i from"process";import"path";const o=(o,e)=>{if(!(()=>{try{return t("git --version"),!0}catch(t){return!1}})())throw new Error("Git does not exist!");if(!(t=>!!/^(https:\/\/github\.com\/|git@github\.com:)[\s\S]+\/[\s\S]+(\.git)?$/.exec(t))(o))throw new Error(`The current URL ${o} is not in a valid GitHub clone format!`);const{dir:s,branch:c,mirrorAddress:h,silence:n}=e||{};((t,i)=>{const o=t.split(" "),e=o.shift()||"",s=r(e,o,i);if(s.error)throw s.error})(`git clone ${h?((t,r)=>{let i=t;if(i.startsWith("git@")){const[,t,r]=i.match(/^git@github\.com:([\s\S]+)\/([\s\S]+)\.git$/)||[];i=`https://github.com/${t}/${r}.git`}return i.endsWith(".git")||(i+=".git"),i=i.replace("github.com",r),i})(o,h):o}${s?` ${s}`:""}${c?` --branch ${c}`:""}`,{cwd:i.cwd(),stdio:n?"pipe":"inherit"})};export{o as default};
|
|
2
|
+
//# sourceMappingURL=gitClone.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gitClone.esm.js","sources":["../src/utils.ts","../src/gitClone.ts"],"sourcesContent":["import path from 'path';\nimport { execSync } from 'child_process';\n\n/**\n * 替换git的远程推送源地址,这里暂定origin名称为固定的\n */\nexport const setGitSource = (dir: string, url: string) => {\n // 仓库所在文件地址\n const dirPath = path.join(process.cwd(), dir);\n // 先删除在执行\n const shellStr = `git remote set-url origin ${url}`;\n execSync(shellStr, { cwd: dirPath });\n};\n\n/**\n * 根据github的url返回对应的文件夹名称\n * https://github.com.cnpmjs.org/bosens-China/github-clone会返回github-clone\n *\n */\nexport const getDir = (url: string) => {\n const str = url;\n const dir = str.split('/').pop() || '';\n return dir.includes('.git') ? dir.slice(0, dir.indexOf('.git')) : dir;\n};\n\n/* 是否为支持的git clone拉取格式\n * https://github.com/bosens-China/breeze-cli\n * https://github.com/bosens-China/breeze-cli.git\n * git@github.com:bosens-China/breeze-cli.git\n */\nexport const isGithubLink = (url: string) => {\n const reg = /^(https:\\/\\/github\\.com\\/|git@github\\.com:)[\\s\\S]+\\/[\\s\\S]+(\\.git)?$/;\n return !!reg.exec(url);\n};\n\n/**\n * 将当前的网站替换成镜像网站,例如\n * https://github.com/bosens-China/github-clone会被替换成\n * https://github.com.cnpmjs.org/bosens-China/github-clone\n */\n\nexport const replaceMirror = (currentWebsite: string, replaceWebsite: string) => {\n let s = currentWebsite;\n // 这里处理一下以git@开头的情况,如果git@开头,把用户名和仓库提取出来,用https的形式拉取\n // https://github.com/bosens-China/breeze-cli.git\n // git@github.com:bosens-China/breeze-cli.git\n if (s.startsWith('git@')) {\n const [, name, warehouse] = s.match(/^git@github\\.com:([\\s\\S]+)\\/([\\s\\S]+)\\.git$/) || [];\n s = `https://github.com/${name}/${warehouse}.git`;\n }\n if (!s.endsWith('.git')) {\n s += '.git';\n }\n s = s.replace('github.com', replaceWebsite);\n return s;\n};\n","import { execSync, spawnSync, SpawnSyncOptionsWithBufferEncoding } from 'child_process';\nimport process from 'process';\nimport { isGithubLink, replaceMirror } from './utils';\n\n/**\n * 判断git是否存在\n */\nconst gitExist = () => {\n try {\n execSync(`git --version`);\n return true;\n } catch {\n return false;\n }\n};\n\nconst pull = (shellStr: string, option?: SpawnSyncOptionsWithBufferEncoding) => {\n const args = shellStr.split(' ');\n const name = args.shift() || '';\n const info = spawnSync(name, args, option);\n if (info.error) {\n throw info.error;\n }\n return info;\n};\ninterface Options {\n dir: string;\n branch: string;\n mirrorAddress: string;\n silence: boolean;\n}\n\nconst clone = (url: string, options: Partial<Options>) => {\n if (!gitExist()) {\n throw new Error(`Git does not exist!`);\n }\n if (!isGithubLink(url)) {\n throw new Error(`The current URL ${url} is not in a valid GitHub clone format!`);\n }\n const { dir, branch, mirrorAddress, silence } = options || {};\n // 如果存在镜像网站就替换一下格式\n const template = mirrorAddress ? replaceMirror(url, mirrorAddress) : url;\n const shell = `git clone ${template}${dir ? ` ${dir}` : ''}${branch ? ` --branch ${branch}` : ''}`;\n pull(shell, { cwd: process.cwd(), stdio: silence ? 'pipe' : 'inherit' });\n};\n\nexport default clone;\n"],"names":["clone","url","options","execSync","gitExist","Error","exec","isGithubLink","dir","branch","mirrorAddress","silence","shellStr","option","args","split","name","shift","info","spawnSync","error","pull","currentWebsite","replaceWebsite","s","startsWith","warehouse","match","endsWith","replace","replaceMirror","cwd","process","stdio"],"mappings":"4FA8BO,MCEDA,EAAQ,CAACC,EAAaC,KAC1B,IA1Be,MACf,IAEE,OADAC,EAAS,kBACF,EACP,SACA,OAAO,IAqBJC,GACH,MAAM,IAAIC,MAAM,uBAElB,IDN0B,CAACJ,KACf,uEACCK,KAAKL,GCIbM,CAAaN,GAChB,MAAM,IAAII,MAAM,mBAAmBJ,4CAErC,MAAMO,IAAEA,EAAGC,OAAEA,EAAMC,cAAEA,EAAaC,QAAEA,GAAYT,GAAW,GAvBhD,EAACU,EAAkBC,KAC9B,MAAMC,EAAOF,EAASG,MAAM,KACtBC,EAAOF,EAAKG,SAAW,GACvBC,EAAOC,EAAUH,EAAMF,EAAMD,GACnC,GAAIK,EAAKE,MACP,MAAMF,EAAKE,OAsBbC,CADc,aADGX,EDAU,EAACY,EAAwBC,KACpD,IAAIC,EAAIF,EAIR,GAAIE,EAAEC,WAAW,QAAS,CACxB,OAAST,EAAMU,GAAaF,EAAEG,MAAM,gDAAkD,GACtFH,EAAI,sBAAsBR,KAAQU,QAMpC,OAJKF,EAAEI,SAAS,UACdJ,GAAK,QAEPA,EAAIA,EAAEK,QAAQ,aAAcN,GACrBC,GCb0BM,CAAc7B,EAAKS,GAAiBT,IAC/BO,EAAM,IAAIA,IAAQ,KAAKC,EAAS,aAAaA,IAAW,KAClF,CAAEsB,IAAKC,EAAQD,MAAOE,MAAOtB,EAAU,OAAS"}
|
package/dist/gitClone.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var t=require("child_process"),r=require("process");function e(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}require("path");var i=e(r);module.exports=(r,e)=>{if(!(()=>{try{return t.execSync("git --version"),!0}catch(t){return!1}})())throw new Error("Git does not exist!");if(!(t=>!!/^(https:\/\/github\.com\/|git@github\.com:)[\s\S]+\/[\s\S]+(\.git)?$/.exec(t))(r))throw new Error(`The current URL ${r} is not in a valid GitHub clone format!`);const{dir:o,branch:c,mirrorAddress:s,silence:n}=e||{};((r,e)=>{const i=r.split(" "),o=i.shift()||"",c=t.spawnSync(o,i,e);if(c.error)throw c.error})(`git clone ${s?((t,r)=>{let e=t;if(e.startsWith("git@")){const[,t,r]=e.match(/^git@github\.com:([\s\S]+)\/([\s\S]+)\.git$/)||[];e=`https://github.com/${t}/${r}.git`}return e.endsWith(".git")||(e+=".git"),e=e.replace("github.com",r),e})(r,s):r}${o?` ${o}`:""}${c?` --branch ${c}`:""}`,{cwd:i.default.cwd(),stdio:n?"pipe":"inherit"})};
|
|
2
|
+
//# sourceMappingURL=gitClone.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gitClone.js","sources":["../src/gitClone.ts","../src/utils.ts"],"sourcesContent":["import { execSync, spawnSync, SpawnSyncOptionsWithBufferEncoding } from 'child_process';\nimport process from 'process';\nimport { isGithubLink, replaceMirror } from './utils';\n\n/**\n * 判断git是否存在\n */\nconst gitExist = () => {\n try {\n execSync(`git --version`);\n return true;\n } catch {\n return false;\n }\n};\n\nconst pull = (shellStr: string, option?: SpawnSyncOptionsWithBufferEncoding) => {\n const args = shellStr.split(' ');\n const name = args.shift() || '';\n const info = spawnSync(name, args, option);\n if (info.error) {\n throw info.error;\n }\n return info;\n};\ninterface Options {\n dir: string;\n branch: string;\n mirrorAddress: string;\n silence: boolean;\n}\n\nconst clone = (url: string, options: Partial<Options>) => {\n if (!gitExist()) {\n throw new Error(`Git does not exist!`);\n }\n if (!isGithubLink(url)) {\n throw new Error(`The current URL ${url} is not in a valid GitHub clone format!`);\n }\n const { dir, branch, mirrorAddress, silence } = options || {};\n // 如果存在镜像网站就替换一下格式\n const template = mirrorAddress ? replaceMirror(url, mirrorAddress) : url;\n const shell = `git clone ${template}${dir ? ` ${dir}` : ''}${branch ? ` --branch ${branch}` : ''}`;\n pull(shell, { cwd: process.cwd(), stdio: silence ? 'pipe' : 'inherit' });\n};\n\nexport default clone;\n","import path from 'path';\nimport { execSync } from 'child_process';\n\n/**\n * 替换git的远程推送源地址,这里暂定origin名称为固定的\n */\nexport const setGitSource = (dir: string, url: string) => {\n // 仓库所在文件地址\n const dirPath = path.join(process.cwd(), dir);\n // 先删除在执行\n const shellStr = `git remote set-url origin ${url}`;\n execSync(shellStr, { cwd: dirPath });\n};\n\n/**\n * 根据github的url返回对应的文件夹名称\n * https://github.com.cnpmjs.org/bosens-China/github-clone会返回github-clone\n *\n */\nexport const getDir = (url: string) => {\n const str = url;\n const dir = str.split('/').pop() || '';\n return dir.includes('.git') ? dir.slice(0, dir.indexOf('.git')) : dir;\n};\n\n/* 是否为支持的git clone拉取格式\n * https://github.com/bosens-China/breeze-cli\n * https://github.com/bosens-China/breeze-cli.git\n * git@github.com:bosens-China/breeze-cli.git\n */\nexport const isGithubLink = (url: string) => {\n const reg = /^(https:\\/\\/github\\.com\\/|git@github\\.com:)[\\s\\S]+\\/[\\s\\S]+(\\.git)?$/;\n return !!reg.exec(url);\n};\n\n/**\n * 将当前的网站替换成镜像网站,例如\n * https://github.com/bosens-China/github-clone会被替换成\n * https://github.com.cnpmjs.org/bosens-China/github-clone\n */\n\nexport const replaceMirror = (currentWebsite: string, replaceWebsite: string) => {\n let s = currentWebsite;\n // 这里处理一下以git@开头的情况,如果git@开头,把用户名和仓库提取出来,用https的形式拉取\n // https://github.com/bosens-China/breeze-cli.git\n // git@github.com:bosens-China/breeze-cli.git\n if (s.startsWith('git@')) {\n const [, name, warehouse] = s.match(/^git@github\\.com:([\\s\\S]+)\\/([\\s\\S]+)\\.git$/) || [];\n s = `https://github.com/${name}/${warehouse}.git`;\n }\n if (!s.endsWith('.git')) {\n s += '.git';\n }\n s = s.replace('github.com', replaceWebsite);\n return s;\n};\n"],"names":["url","options","execSync","gitExist","Error","exec","isGithubLink","dir","branch","mirrorAddress","silence","shellStr","option","args","split","name","shift","info","spawnSync","error","pull","currentWebsite","replaceWebsite","s","startsWith","warehouse","match","endsWith","replace","replaceMirror","cwd","process","stdio"],"mappings":"mLAgCc,CAACA,EAAaC,KAC1B,IA1Be,MACf,IAEE,OADAC,WAAS,kBACF,EACP,SACA,OAAO,IAqBJC,GACH,MAAM,IAAIC,MAAM,uBAElB,ICN0B,CAACJ,KACf,uEACCK,KAAKL,GDIbM,CAAaN,GAChB,MAAM,IAAII,MAAM,mBAAmBJ,4CAErC,MAAMO,IAAEA,EAAGC,OAAEA,EAAMC,cAAEA,EAAaC,QAAEA,GAAYT,GAAW,GAvBhD,EAACU,EAAkBC,KAC9B,MAAMC,EAAOF,EAASG,MAAM,KACtBC,EAAOF,EAAKG,SAAW,GACvBC,EAAOC,YAAUH,EAAMF,EAAMD,GACnC,GAAIK,EAAKE,MACP,MAAMF,EAAKE,OAsBbC,CADc,aADGX,ECAU,EAACY,EAAwBC,KACpD,IAAIC,EAAIF,EAIR,GAAIE,EAAEC,WAAW,QAAS,CACxB,OAAST,EAAMU,GAAaF,EAAEG,MAAM,gDAAkD,GACtFH,EAAI,sBAAsBR,KAAQU,QAMpC,OAJKF,EAAEI,SAAS,UACdJ,GAAK,QAEPA,EAAIA,EAAEK,QAAQ,aAAcN,GACrBC,GDb0BM,CAAc7B,EAAKS,GAAiBT,IAC/BO,EAAM,IAAIA,IAAQ,KAAKC,EAAS,aAAaA,IAAW,KAClF,CAAEsB,IAAKC,UAAQD,MAAOE,MAAOtB,EAAU,OAAS"}
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";var e=require("commander"),t=require("os"),r=require("path"),o=require("fs"),i=require("./gitClone"),c=require("child_process");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var s=n(t),a=n(r),u=n(o),l=n(i);const d=s.default.homedir(),g=a.default.join(d,".g.config"),h="github.com.cnpmjs.org",m=()=>u.default.existsSync(g)&&u.default.readFileSync(g,"utf-8")||h,p=new e.Command;p.version("1.1.2","-v, --version","输出当前版本号"),p.command("clone <url> [warehouse]").description("根据url拉取指定仓库").option("-b, --branch <value>","拉取指定分支").action(((e,t,{branch:r}={})=>{try{l.default(e,{dir:t,branch:r,mirrorAddress:m()});const o=t||(e=>{const t=e.split("/").pop()||"";return t.includes(".git")?t.slice(0,t.indexOf(".git")):t})(m()?((e,t)=>{let r=e;if(r.startsWith("git@")){const[,e,t]=r.match(/^git@github\.com:([\s\S]+)\/([\s\S]+)\.git$/)||[];r=`https://github.com/${e}/${t}.git`}return r.endsWith(".git")||(r+=".git"),r=r.replace("github.com",t),r})(e,m()):e);((e,t)=>{const r=a.default.join(process.cwd(),e),o=`git remote set-url origin ${t}`;c.execSync(o,{cwd:r})})(o,e)}catch(e){console.error(`${e instanceof Error?e.message:e}`)}})),p.command("set [path]").description(`修改github的镜像仓库地址,<path>可选,如果省略,默认为: ${h}`).action(((e=h)=>{try{t=e,u.default.writeFileSync(g,t),console.log(`set ${e}成功`)}catch(e){console.error("set path error")}var t})),p.command("get [path]").description("读取当前的默认镜像地址").action((()=>{const e=m();console.log(`当前镜像地址为:${e}`)})),p.parse(process.argv);
|
|
3
|
+
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.js","sources":["../src/config.ts","../src/main.ts","../src/utils.ts"],"sourcesContent":["import os from 'os';\nimport path from 'path';\nimport fs from 'fs';\n\nconst dir = os.homedir();\nconst configPath = path.join(dir, '.g.config');\n\n// 默认github镜像网站\nexport const DEFAULTPATH = 'github.com.cnpmjs.org';\n\n/**\n * 读取配置\n *\n */\nexport const getAddress = () => {\n if (!fs.existsSync(configPath)) {\n return DEFAULTPATH;\n }\n return fs.readFileSync(configPath, 'utf-8') || DEFAULTPATH;\n};\n\n/**\n * 设置配置\n *\n */\nexport const setAddress = (address: string) => {\n fs.writeFileSync(configPath, address);\n};\n","#!/usr/bin/env node\n\nimport { Command } from 'commander';\nimport { version } from '../package.json';\nimport { getAddress, setAddress, DEFAULTPATH } from './config';\nimport GitClone from './gitClone';\nimport { setGitSource, getDir, replaceMirror } from './utils';\n\nconst program = new Command();\nprogram.version(version, '-v, --version', '输出当前版本号');\n// git clone 支持多个命令,但是这里只让他支持-b也就是拉取指定分支,非必填\nprogram\n .command('clone <url> [warehouse]')\n .description('根据url拉取指定仓库')\n .option('-b, --branch <value>', '拉取指定分支')\n .action((url, dir, { branch } = {}) => {\n try {\n // 开始拉取\n GitClone(url, { dir, branch, mirrorAddress: getAddress() });\n // 拉取成功之后,进入拉取目录修改推送源地址\n const directory = dir || getDir(getAddress() ? replaceMirror(url, getAddress()) : url);\n setGitSource(directory, url);\n } catch (e) {\n console.error(`${e instanceof Error ? e.message : e}`);\n }\n });\n\nprogram\n .command('set [path]')\n .description(`修改github的镜像仓库地址,<path>可选,如果省略,默认为: ${DEFAULTPATH}`)\n .action((pathUrl = DEFAULTPATH) => {\n try {\n setAddress(pathUrl);\n console.log(`set ${pathUrl}成功`);\n } catch {\n console.error(`set path error`);\n }\n });\nprogram\n .command('get [path]')\n .description(`读取当前的默认镜像地址`)\n .action(() => {\n const path = getAddress();\n console.log(`当前镜像地址为:${path}`);\n });\nprogram.parse(process.argv);\n","import path from 'path';\nimport { execSync } from 'child_process';\n\n/**\n * 替换git的远程推送源地址,这里暂定origin名称为固定的\n */\nexport const setGitSource = (dir: string, url: string) => {\n // 仓库所在文件地址\n const dirPath = path.join(process.cwd(), dir);\n // 先删除在执行\n const shellStr = `git remote set-url origin ${url}`;\n execSync(shellStr, { cwd: dirPath });\n};\n\n/**\n * 根据github的url返回对应的文件夹名称\n * https://github.com.cnpmjs.org/bosens-China/github-clone会返回github-clone\n *\n */\nexport const getDir = (url: string) => {\n const str = url;\n const dir = str.split('/').pop() || '';\n return dir.includes('.git') ? dir.slice(0, dir.indexOf('.git')) : dir;\n};\n\n/* 是否为支持的git clone拉取格式\n * https://github.com/bosens-China/breeze-cli\n * https://github.com/bosens-China/breeze-cli.git\n * git@github.com:bosens-China/breeze-cli.git\n */\nexport const isGithubLink = (url: string) => {\n const reg = /^(https:\\/\\/github\\.com\\/|git@github\\.com:)[\\s\\S]+\\/[\\s\\S]+(\\.git)?$/;\n return !!reg.exec(url);\n};\n\n/**\n * 将当前的网站替换成镜像网站,例如\n * https://github.com/bosens-China/github-clone会被替换成\n * https://github.com.cnpmjs.org/bosens-China/github-clone\n */\n\nexport const replaceMirror = (currentWebsite: string, replaceWebsite: string) => {\n let s = currentWebsite;\n // 这里处理一下以git@开头的情况,如果git@开头,把用户名和仓库提取出来,用https的形式拉取\n // https://github.com/bosens-China/breeze-cli.git\n // git@github.com:bosens-China/breeze-cli.git\n if (s.startsWith('git@')) {\n const [, name, warehouse] = s.match(/^git@github\\.com:([\\s\\S]+)\\/([\\s\\S]+)\\.git$/) || [];\n s = `https://github.com/${name}/${warehouse}.git`;\n }\n if (!s.endsWith('.git')) {\n s += '.git';\n }\n s = s.replace('github.com', replaceWebsite);\n return s;\n};\n"],"names":["dir","os","homedir","configPath","path","join","DEFAULTPATH","getAddress","fs","existsSync","readFileSync","program","Command","version","command","description","option","action","url","branch","GitClone","mirrorAddress","directory","split","pop","includes","slice","indexOf","getDir","currentWebsite","replaceWebsite","s","startsWith","name","warehouse","match","endsWith","replace","replaceMirror","dirPath","process","cwd","shellStr","execSync","setGitSource","e","console","error","Error","message","pathUrl","address","writeFileSync","log","parse","argv"],"mappings":";qPAIA,MAAMA,EAAMC,UAAGC,UACTC,EAAaC,UAAKC,KAAKL,EAAK,aAGrBM,EAAc,wBAMdC,EAAa,IACnBC,UAAGC,WAAWN,IAGZK,UAAGE,aAAaP,EAAY,UAF1BG,ECRLK,EAAU,IAAIC,UACpBD,EAAQE,gBAAiB,gBAAiB,WAE1CF,EACGG,QAAQ,2BACRC,YAAY,eACZC,OAAO,uBAAwB,UAC/BC,QAAO,CAACC,EAAKlB,GAAOmB,OAAAA,GAAW,MAC9B,IAEEC,UAASF,EAAK,CAAElB,IAAAA,EAAKmB,OAAAA,EAAQE,cAAed,MAE5C,MAAMe,EAAYtB,GCDF,CAACkB,IACrB,MACMlB,EADMkB,EACIK,MAAM,KAAKC,OAAS,GACpC,OAAOxB,EAAIyB,SAAS,QAAUzB,EAAI0B,MAAM,EAAG1B,EAAI2B,QAAQ,SAAW3B,GDFrC4B,CAAOrB,ICqBT,EAACsB,EAAwBC,KACpD,IAAIC,EAAIF,EAIR,GAAIE,EAAEC,WAAW,QAAS,CACxB,OAASC,EAAMC,GAAaH,EAAEI,MAAM,gDAAkD,GACtFJ,EAAI,sBAAsBE,KAAQC,QAMpC,OAJKH,EAAEK,SAAS,UACdL,GAAK,QAEPA,EAAIA,EAAEM,QAAQ,aAAcP,GACrBC,GDlC4CO,CAAcpB,EAAKX,KAAgBW,GCd5D,EAAClB,EAAakB,KAExC,MAAMqB,EAAUnC,UAAKC,KAAKmC,QAAQC,MAAOzC,GAEnC0C,EAAW,6BAA6BxB,IAC9CyB,WAASD,EAAU,CAAED,IAAKF,KDUtBK,CAAatB,EAAWJ,GACxB,MAAO2B,GACPC,QAAQC,MAAM,GAAGF,aAAaG,MAAQH,EAAEI,QAAUJ,SAIxDlC,EACGG,QAAQ,cACRC,YAAY,sCAAsCT,KAClDW,QAAO,CAACiC,EAAU5C,KACjB,IDNuB6C,ECOVD,EDNf1C,UAAG4C,cAAcjD,EAAYgD,GCOzBL,QAAQO,IAAI,OAAOH,OACnB,SACAJ,QAAQC,MAAM,kBDVM,IAACI,KCa3BxC,EACGG,QAAQ,cACRC,YAAY,eACZE,QAAO,KACN,MAAMb,EAAOG,IACbuC,QAAQO,IAAI,WAAWjD,QAE3BO,EAAQ2C,MAAMd,QAAQe"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 替换git的远程推送源地址,这里暂定origin名称为固定的
|
|
3
|
+
*/
|
|
4
|
+
export declare const setGitSource: (dir: string, url: string) => void;
|
|
5
|
+
/**
|
|
6
|
+
* 根据github的url返回对应的文件夹名称
|
|
7
|
+
* https://github.com.cnpmjs.org/bosens-China/github-clone会返回github-clone
|
|
8
|
+
*
|
|
9
|
+
*/
|
|
10
|
+
export declare const getDir: (url: string) => string;
|
|
11
|
+
export declare const isGithubLink: (url: string) => boolean;
|
|
12
|
+
/**
|
|
13
|
+
* 将当前的网站替换成镜像网站,例如
|
|
14
|
+
* https://github.com/bosens-China/github-clone会被替换成
|
|
15
|
+
* https://github.com.cnpmjs.org/bosens-China/github-clone
|
|
16
|
+
*/
|
|
17
|
+
export declare const replaceMirror: (currentWebsite: string, replaceWebsite: string) => string;
|
package/package.json
CHANGED
|
@@ -1,49 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@boses/github-clone",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"main": "dist/
|
|
3
|
+
"version": "1.1.3",
|
|
4
|
+
"main": "./dist/gitClone.js",
|
|
5
|
+
"module": "./dist/gitClone.esm.js",
|
|
5
6
|
"license": "MIT",
|
|
6
|
-
"devDependencies": {
|
|
7
|
-
"@babel/core": "^7.12.10",
|
|
8
|
-
"@babel/preset-env": "^7.12.11",
|
|
9
|
-
"@babel/preset-typescript": "^7.12.7",
|
|
10
|
-
"@types/jest": "^26.0.20",
|
|
11
|
-
"@types/node": "^14.14.22",
|
|
12
|
-
"@typescript-eslint/eslint-plugin": "^4.14.0",
|
|
13
|
-
"@typescript-eslint/parser": "^4.14.0",
|
|
14
|
-
"babel-jest": "^26.6.3",
|
|
15
|
-
"eslint": "^7.18.0",
|
|
16
|
-
"eslint-config-alloy": "^3.10.0",
|
|
17
|
-
"eslint-plugin-prettier": "^3.3.1",
|
|
18
|
-
"jest": "^26.6.3",
|
|
19
|
-
"prettier": "^2.2.1",
|
|
20
|
-
"ts-node": "^9.1.1",
|
|
21
|
-
"typescript": "^4.1.3"
|
|
22
|
-
},
|
|
23
|
-
"scripts": {
|
|
24
|
-
"build": "tsc",
|
|
25
|
-
"build-watch": "tsc && tsc -w",
|
|
26
|
-
"test": "jest"
|
|
27
|
-
},
|
|
28
|
-
"dependencies": {
|
|
29
|
-
"commander": "^7.0.0"
|
|
30
|
-
},
|
|
31
7
|
"bin": {
|
|
32
|
-
"g": "dist/
|
|
8
|
+
"g": "./dist/main.js"
|
|
33
9
|
},
|
|
10
|
+
"types": "./dist/src/gitClone.d.ts",
|
|
34
11
|
"engines": {
|
|
35
12
|
"node": ">=10.6.0"
|
|
36
13
|
},
|
|
37
14
|
"files": [
|
|
38
|
-
"/dist"
|
|
39
|
-
"src/*.js",
|
|
40
|
-
"*.md"
|
|
15
|
+
"/dist"
|
|
41
16
|
],
|
|
42
17
|
"author": {
|
|
43
18
|
"name": "boses",
|
|
44
19
|
"email": "yangboses@gmail.com"
|
|
45
20
|
},
|
|
46
|
-
"description": "
|
|
21
|
+
"description": "解决国内 Github clone 速度慢的工具",
|
|
47
22
|
"keywords": [
|
|
48
23
|
"git",
|
|
49
24
|
"github",
|
|
@@ -53,5 +28,62 @@
|
|
|
53
28
|
"repository": {
|
|
54
29
|
"type": "git",
|
|
55
30
|
"url": "https://github.com/bosens-China/github-clone"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@babel/core": "^7.15.8",
|
|
34
|
+
"@babel/plugin-transform-runtime": "^7.16.0",
|
|
35
|
+
"@babel/preset-env": "^7.15.8",
|
|
36
|
+
"@babel/preset-typescript": "^7.15.0",
|
|
37
|
+
"@rollup/plugin-babel": "^5.3.0",
|
|
38
|
+
"@rollup/plugin-commonjs": "^21.0.1",
|
|
39
|
+
"@rollup/plugin-json": "^4.1.0",
|
|
40
|
+
"@rollup/plugin-node-resolve": "^13.0.6",
|
|
41
|
+
"@types/babel__core": "^7.1.16",
|
|
42
|
+
"@types/fs-extra": "^9.0.13",
|
|
43
|
+
"@types/jest": "^27.0.2",
|
|
44
|
+
"@types/node": "^16.11.6",
|
|
45
|
+
"@typescript-eslint/eslint-plugin": "^5.2.0",
|
|
46
|
+
"@typescript-eslint/parser": "^5.2.0",
|
|
47
|
+
"babel-jest": "^27.3.1",
|
|
48
|
+
"commitizen": "^4.2.4",
|
|
49
|
+
"cz-conventional-changelog": "3.3.0",
|
|
50
|
+
"eslint": "^8.1.0",
|
|
51
|
+
"eslint-config-prettier": "^8.3.0",
|
|
52
|
+
"eslint-config-standard": "^16.0.3",
|
|
53
|
+
"eslint-plugin-import": "^2.25.2",
|
|
54
|
+
"eslint-plugin-node": "^11.1.0",
|
|
55
|
+
"eslint-plugin-prettier": "^4.0.0",
|
|
56
|
+
"eslint-plugin-promise": "^5.1.1",
|
|
57
|
+
"fs-extra": "^10.0.0",
|
|
58
|
+
"jest": "^27.3.1",
|
|
59
|
+
"prettier": "^2.4.1",
|
|
60
|
+
"rollup": "^2.58.3",
|
|
61
|
+
"rollup-plugin-copy": "^3.4.0",
|
|
62
|
+
"rollup-plugin-preserve-shebangs": "^0.2.0",
|
|
63
|
+
"rollup-plugin-terser": "^7.0.2",
|
|
64
|
+
"rollup-plugin-typescript2": "^0.30.0",
|
|
65
|
+
"ts-node": "^10.4.0",
|
|
66
|
+
"ts-node-dev": "^1.1.8",
|
|
67
|
+
"tsconfig-paths": "^3.11.0",
|
|
68
|
+
"tslib": "^2.3.1",
|
|
69
|
+
"typescript": "^4.4.4"
|
|
70
|
+
},
|
|
71
|
+
"dependencies": {
|
|
72
|
+
"commander": "^7.0.0"
|
|
73
|
+
},
|
|
74
|
+
"scripts": {
|
|
75
|
+
"lint": "eslint --ext .js,.ts src",
|
|
76
|
+
"fix": "eslint --fix --ext .js,.ts src",
|
|
77
|
+
"format": "prettier --write src",
|
|
78
|
+
"commit": "cz",
|
|
79
|
+
"dev": "tsnd --respawn main.ts",
|
|
80
|
+
"test": "jest",
|
|
81
|
+
"build": "rollup --config ./script/rollup.config.js",
|
|
82
|
+
"build-watch": "rollup --config ./script/rollup.config.js --watch"
|
|
83
|
+
},
|
|
84
|
+
"config": {
|
|
85
|
+
"commitizen": {
|
|
86
|
+
"path": "./node_modules/cz-conventional-changelog"
|
|
87
|
+
}
|
|
56
88
|
}
|
|
57
89
|
}
|
package/dist/package.json
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@boses/github-clone",
|
|
3
|
-
"version": "1.0.9",
|
|
4
|
-
"main": "dist/src/main.js",
|
|
5
|
-
"license": "MIT",
|
|
6
|
-
"devDependencies": {
|
|
7
|
-
"@babel/core": "^7.12.10",
|
|
8
|
-
"@babel/preset-env": "^7.12.11",
|
|
9
|
-
"@babel/preset-typescript": "^7.12.7",
|
|
10
|
-
"@types/jest": "^26.0.20",
|
|
11
|
-
"@types/node": "^14.14.22",
|
|
12
|
-
"@typescript-eslint/eslint-plugin": "^4.14.0",
|
|
13
|
-
"@typescript-eslint/parser": "^4.14.0",
|
|
14
|
-
"babel-jest": "^26.6.3",
|
|
15
|
-
"eslint": "^7.18.0",
|
|
16
|
-
"eslint-config-alloy": "^3.10.0",
|
|
17
|
-
"eslint-plugin-prettier": "^3.3.1",
|
|
18
|
-
"jest": "^26.6.3",
|
|
19
|
-
"prettier": "^2.2.1",
|
|
20
|
-
"ts-node": "^9.1.1",
|
|
21
|
-
"typescript": "^4.1.3"
|
|
22
|
-
},
|
|
23
|
-
"scripts": {
|
|
24
|
-
"build": "tsc",
|
|
25
|
-
"build-watch": "tsc && tsc -w",
|
|
26
|
-
"test": "jest"
|
|
27
|
-
},
|
|
28
|
-
"dependencies": {
|
|
29
|
-
"commander": "^7.0.0"
|
|
30
|
-
},
|
|
31
|
-
"bin": {
|
|
32
|
-
"g": "dist/src/main.js"
|
|
33
|
-
},
|
|
34
|
-
"engines": {
|
|
35
|
-
"node": ">=10.6.0"
|
|
36
|
-
},
|
|
37
|
-
"files": [
|
|
38
|
-
"/dist",
|
|
39
|
-
"src/*.js",
|
|
40
|
-
"*.md"
|
|
41
|
-
],
|
|
42
|
-
"author": {
|
|
43
|
-
"name": "boses",
|
|
44
|
-
"email": "yangboses@gmail.com"
|
|
45
|
-
},
|
|
46
|
-
"description": "工程化解决 Github clone 速度慢的工具",
|
|
47
|
-
"keywords": [
|
|
48
|
-
"git",
|
|
49
|
-
"github",
|
|
50
|
-
"clone",
|
|
51
|
-
"typescript"
|
|
52
|
-
],
|
|
53
|
-
"repository": {
|
|
54
|
-
"type": "git",
|
|
55
|
-
"url": "https://github.com/bosens-China/github-clone"
|
|
56
|
-
}
|
|
57
|
-
}
|
package/dist/src/config.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.getConfig = exports.setConfig = exports.DEFAULTPATH = void 0;
|
|
7
|
-
const os_1 = __importDefault(require("os"));
|
|
8
|
-
const path_1 = __importDefault(require("path"));
|
|
9
|
-
const fs_1 = require("../src/fs");
|
|
10
|
-
exports.DEFAULTPATH = 'github.com.cnpmjs.org';
|
|
11
|
-
const dir = os_1.default.homedir();
|
|
12
|
-
const configPath = path_1.default.join(dir, '.g.config.json');
|
|
13
|
-
async function setConfig(config) {
|
|
14
|
-
return fs_1.setJsonFile(configPath, config);
|
|
15
|
-
}
|
|
16
|
-
exports.setConfig = setConfig;
|
|
17
|
-
async function getConfig() {
|
|
18
|
-
const config = await fs_1.getJsonFile(configPath);
|
|
19
|
-
return (config || { path: exports.DEFAULTPATH });
|
|
20
|
-
}
|
|
21
|
-
exports.getConfig = getConfig;
|
|
22
|
-
//# sourceMappingURL=config.js.map
|
package/dist/src/fs.js
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.getJsonFile = exports.setJsonFile = exports.writeFile = exports.readFile = exports.access = void 0;
|
|
7
|
-
const fs_1 = __importDefault(require("fs"));
|
|
8
|
-
function access(file) {
|
|
9
|
-
return new Promise((resolve) => {
|
|
10
|
-
fs_1.default.access(file, fs_1.default.constants.R_OK | fs_1.default.constants.W_OK, (err) => {
|
|
11
|
-
if (err) {
|
|
12
|
-
return resolve(false);
|
|
13
|
-
}
|
|
14
|
-
return resolve(true);
|
|
15
|
-
});
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
exports.access = access;
|
|
19
|
-
function readFile(file) {
|
|
20
|
-
return new Promise((resolve, reject) => {
|
|
21
|
-
fs_1.default.readFile(file, 'utf-8', (err, data) => {
|
|
22
|
-
if (err) {
|
|
23
|
-
return reject(err);
|
|
24
|
-
}
|
|
25
|
-
return resolve(data);
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
exports.readFile = readFile;
|
|
30
|
-
function writeFile(file, content) {
|
|
31
|
-
return new Promise((resolve, reject) => {
|
|
32
|
-
fs_1.default.writeFile(file, content, 'utf-8', (err) => {
|
|
33
|
-
if (err) {
|
|
34
|
-
return reject(err);
|
|
35
|
-
}
|
|
36
|
-
return resolve(content);
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
exports.writeFile = writeFile;
|
|
41
|
-
function setJsonFile(file, data, indent = 2) {
|
|
42
|
-
const obj = JSON.stringify(data, null, indent);
|
|
43
|
-
return writeFile(file, obj);
|
|
44
|
-
}
|
|
45
|
-
exports.setJsonFile = setJsonFile;
|
|
46
|
-
async function getJsonFile(file) {
|
|
47
|
-
if (!(await access(file))) {
|
|
48
|
-
return undefined;
|
|
49
|
-
}
|
|
50
|
-
const obj = await readFile(file);
|
|
51
|
-
try {
|
|
52
|
-
return JSON.parse(obj);
|
|
53
|
-
}
|
|
54
|
-
catch (e) {
|
|
55
|
-
return undefined;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
exports.getJsonFile = getJsonFile;
|
|
59
|
-
//# sourceMappingURL=fs.js.map
|
package/dist/src/main.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
"use strict";
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
const commander_1 = require("commander");
|
|
5
|
-
const package_json_1 = require("../package.json");
|
|
6
|
-
const utils_1 = require("./utils");
|
|
7
|
-
const config_1 = require("./config");
|
|
8
|
-
const program = new commander_1.Command();
|
|
9
|
-
program.version(package_json_1.version, '-v, --version', '输出当前版本号');
|
|
10
|
-
program
|
|
11
|
-
.command('clone <url> [warehouse]')
|
|
12
|
-
.description('根据url拉取指定仓库')
|
|
13
|
-
.option('-b, --branch <value>', '拉取指定分支')
|
|
14
|
-
.action(async (url, warehouse, { branch } = {}) => {
|
|
15
|
-
if (!utils_1.isGithubLink(url)) {
|
|
16
|
-
return utils_1.error(`${url} 不是一个有效的链接,目前只支持github的shh和https仓库链接!`);
|
|
17
|
-
}
|
|
18
|
-
if (!(await utils_1.existenceGit())) {
|
|
19
|
-
return utils_1.error(`git不存在,请安装git之后在进行操作: https://git-scm.com/`);
|
|
20
|
-
}
|
|
21
|
-
const config = await config_1.getConfig();
|
|
22
|
-
const replaceStr = utils_1.githubReplace(url, config.path);
|
|
23
|
-
const str = `git clone ${replaceStr}${warehouse ? ` ${warehouse}` : ''}${branch ? ` --branch ${branch}` : ''}`;
|
|
24
|
-
try {
|
|
25
|
-
await utils_1.cloneGie(str);
|
|
26
|
-
}
|
|
27
|
-
catch (e) {
|
|
28
|
-
return utils_1.error();
|
|
29
|
-
}
|
|
30
|
-
const dir = warehouse || utils_1.gitDir(replaceStr);
|
|
31
|
-
await utils_1.setGitSource(dir, url);
|
|
32
|
-
process.exit(0);
|
|
33
|
-
});
|
|
34
|
-
program
|
|
35
|
-
.command('set [path]')
|
|
36
|
-
.description(`修改github的镜像仓库地址,<path>可选,如果省略,默认为: ${config_1.DEFAULTPATH}`)
|
|
37
|
-
.action(async (pathUrl = config_1.DEFAULTPATH) => {
|
|
38
|
-
try {
|
|
39
|
-
await config_1.setConfig({ path: pathUrl });
|
|
40
|
-
process.exit(0);
|
|
41
|
-
}
|
|
42
|
-
catch (e) {
|
|
43
|
-
utils_1.error('set path失败');
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
program
|
|
47
|
-
.command('get [path]')
|
|
48
|
-
.description(`读取当前的默认镜像地址`)
|
|
49
|
-
.action(async () => {
|
|
50
|
-
const config = await config_1.getConfig();
|
|
51
|
-
console.log(`当前镜像地址为:${config.path}`);
|
|
52
|
-
process.exit(0);
|
|
53
|
-
});
|
|
54
|
-
program.parse(process.argv);
|
|
55
|
-
//# sourceMappingURL=main.js.map
|
package/dist/src/utils.js
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.setGitSource = exports.gitDir = exports.githubReplace = exports.cloneGie = exports.existenceGit = exports.shell = exports.error = exports.isGithubLink = void 0;
|
|
7
|
-
const child_process_1 = require("child_process");
|
|
8
|
-
const path_1 = __importDefault(require("path"));
|
|
9
|
-
function isGithubLink(url) {
|
|
10
|
-
const reg = /^(https:\/\/github\.com\/|git@github\.com:)[\s\S]+\/[\s\S]+(\.git)?$/;
|
|
11
|
-
return !!reg.exec(url);
|
|
12
|
-
}
|
|
13
|
-
exports.isGithubLink = isGithubLink;
|
|
14
|
-
function error(name) {
|
|
15
|
-
if (name) {
|
|
16
|
-
console.error(name);
|
|
17
|
-
}
|
|
18
|
-
process.exit(1);
|
|
19
|
-
}
|
|
20
|
-
exports.error = error;
|
|
21
|
-
function shell(shellStr, option) {
|
|
22
|
-
return new Promise((resolve, reject) => {
|
|
23
|
-
child_process_1.exec(shellStr, option, (err, data) => {
|
|
24
|
-
if (err) {
|
|
25
|
-
return reject(err);
|
|
26
|
-
}
|
|
27
|
-
return resolve(data);
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
exports.shell = shell;
|
|
32
|
-
function existenceGit() {
|
|
33
|
-
return shell('git --version')
|
|
34
|
-
.then(() => true)
|
|
35
|
-
.catch(() => false);
|
|
36
|
-
}
|
|
37
|
-
exports.existenceGit = existenceGit;
|
|
38
|
-
function cloneGie(shellStr) {
|
|
39
|
-
return new Promise((resolve, reject) => {
|
|
40
|
-
const args = shellStr.split(' ');
|
|
41
|
-
const name = args.shift() || '';
|
|
42
|
-
const git = child_process_1.spawn(name, args, { stdio: 'inherit' });
|
|
43
|
-
git.on('close', (code) => {
|
|
44
|
-
if (code !== 0) {
|
|
45
|
-
return reject(new Error(`code状态码为${code}`));
|
|
46
|
-
}
|
|
47
|
-
return resolve(code);
|
|
48
|
-
});
|
|
49
|
-
git.on('error', (err) => {
|
|
50
|
-
return reject(err);
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
exports.cloneGie = cloneGie;
|
|
55
|
-
function githubReplace(url, replaceStr) {
|
|
56
|
-
let s = url;
|
|
57
|
-
if (s.startsWith('git@')) {
|
|
58
|
-
const [, name, warehouse] = s.match(/^git@github\.com:([\s\S]+)\/([\s\S]+)\.git$/) || [];
|
|
59
|
-
s = `https://github.com/${name}/${warehouse}.git`;
|
|
60
|
-
}
|
|
61
|
-
if (!s.endsWith('.git')) {
|
|
62
|
-
s += '.git';
|
|
63
|
-
}
|
|
64
|
-
s = s.replace('github.com', replaceStr);
|
|
65
|
-
return s;
|
|
66
|
-
}
|
|
67
|
-
exports.githubReplace = githubReplace;
|
|
68
|
-
function gitDir(str) {
|
|
69
|
-
const dir = str.split('/').pop() || '';
|
|
70
|
-
return dir.includes('.git') ? dir.slice(0, dir.indexOf('.git')) : dir;
|
|
71
|
-
}
|
|
72
|
-
exports.gitDir = gitDir;
|
|
73
|
-
function setGitSource(dir, url) {
|
|
74
|
-
const originUrl = url.endsWith('.git') ? url : `${url}.git`;
|
|
75
|
-
const dirPath = path_1.default.join(process.cwd(), dir);
|
|
76
|
-
const shellStr = `git remote set-url origin ${originUrl}`;
|
|
77
|
-
return shell(shellStr, { cwd: dirPath });
|
|
78
|
-
}
|
|
79
|
-
exports.setGitSource = setGitSource;
|
|
80
|
-
//# sourceMappingURL=utils.js.map
|