Skip to content

Commit

Permalink
feat: 新增 github 镜像加速
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Dec 13, 2021
1 parent 9165155 commit 2b0824f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"release": "semantic-release",
"commit": "git add . && git cz",
"create": "ct create",
"build:dev": "cross-env NODE_ENV=development rollup -c && rimraf temp && cross-env NODE_ENV=development ct create",
"build:dev": "rimraf dist && cross-env NODE_ENV=development rollup -c && rimraf temp && cross-env NODE_ENV=development ct create",
"build:prod": "npm run build && rimraf temp && cross-env NODE_ENV=production ct create"
},
"devDependencies": {
Expand All @@ -42,6 +42,7 @@
"@types/fs-extra": "^9.0.4",
"@types/lodash": "^4.14.165",
"@types/node": "^16.9.6",
"@types/promise.any": "^2.0.0",
"@typescript-eslint/eslint-plugin": "^4.9.0",
"@typescript-eslint/parser": "^4.9.0",
"commitizen": "^4.2.2",
Expand All @@ -65,14 +66,16 @@
"validate-commit-msg": "^2.14.0"
},
"dependencies": {
"axios": "^0.24.0",
"colors": "^1.4.0",
"commander": "^8.2.0",
"dayjs": "^1.9.6",
"download-git-repo": "^3.0.2",
"fs-extra": "^10.0.0",
"minimist": "^1.2.5",
"ora": "^5.4.1",
"plop": "^2.7.6"
"plop": "^2.7.6",
"promise.any": "^2.0.2"
},
"config": {
"commitizen": {
Expand Down
2 changes: 1 addition & 1 deletion src/plopfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = function (plop: NodePlopAPI) {
const author = answers.author as string
const template = answers.template as string
const projectPath = path.join(process.cwd(), name)
await downloadGitRepo(`github:CaoMeiYouRen/${template}`, projectPath)
await downloadGitRepo(`CaoMeiYouRen/${template}`, projectPath)
await init(projectPath, {
name,
author,
Expand Down
43 changes: 42 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@ import fs from 'fs-extra'
import path from 'path'
import ora from 'ora'
import download from 'download-git-repo'
import axios from 'axios'
import { exec, ExecOptions } from 'child_process'
import { PACKAGE_MANAGER, __DEV__ } from './env'
import any from 'promise.any'

if (!Promise.any) {
Promise.any = any
}

const REMOTES = [
'https://github.com',
'https://hub.fastgit.org',
'https://gitclone.com',
'https://gh.neting.cc.cnpmjs.org',
]

interface Package {
name: string
version: string
Expand Down Expand Up @@ -35,10 +49,11 @@ Bitbucket - bitbucket:owner/name
* @returns 成功返回 true
*/
export async function downloadGitRepo(repository: string, destination: string, options: any = {}) {
const fastRepo = await getFastGitRepo(repository)
const loading = ora(`download - ${repository}`)
loading.start()
return new Promise((resolve, reject) => {
download(repository, destination, options, (err: any) => {
download(fastRepo, destination, options, (err: any) => {
loading.stop()
if (err) {
return reject(err)
Expand Down Expand Up @@ -172,3 +187,29 @@ export function sortKey(obj: Record<string, unknown>) {
})
return obj2
}

/**
* 从 github 和镜像中选择最快的源
* @param repository 源 owner/name, 例如 CaoMeiYouRen/rollup-template
*/
export async function getFastGitRepo(repository: string) {
const loading = ora(`正在选择镜像源 - ${repository}`)
loading.start()
try {
const fast = await Promise.any(REMOTES.map((remote) => {
// const url = `${remote}/${repository}/.git`
const url = `${remote}/${repository}/archive/refs/heads/master.zip`
return axios({
url,
method: 'HEAD',
timeout: 15 * 1000,
})
}))
return `direct:${fast.config.url}`
} catch (error) {
console.error(error)
process.exit(1)
} finally {
loading.stop()
}
}

0 comments on commit 2b0824f

Please sign in to comment.