Skip to content

Commit

Permalink
feat: 新增 Github 自动创建远程仓库功能;更新文档;更新依赖
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Sep 14, 2022
1 parent 6e72182 commit 41419ba
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- name: Setup Node.js environment
uses: actions/setup-node@v2
with:
node-version: "14"
node-version: "lts/*"
- name: Cache multiple paths
uses: actions/cache@v2
with:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Node.js@14 environment
- name: Setup Node.js environment
uses: actions/setup-node@v2
with:
node-version: "14"
node-version: "lts/*"
- name: Cache multiple paths
uses: actions/cache@v2
with:
Expand Down
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
strict-peer-dependencies=false
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ ct
ct create
```

## 配置

在当前目录下或 `HOME` 路径下创建 `.ctrc` 文件即可,格式为 `json`

```json
{
"GITHUB_TOKEN": "",
"GITEE_TOKEN": ""
}
```

GITHUB_TOKEN 请参考: [创建用于命令行的个人访问令牌](https://help.github.com/cn/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line)

GITEE_TOKEN 请参考:https://gitee.com/profile/personal_access_tokens

**如果不使用自动初始化远程仓库功能,可以跳过该配置**

## 开发

```sh
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"husky": "^8.0.1",
"lint-staged": "^13.0.0",
"rimraf": "^3.0.2",
"rollup": "^2.75.7",
"rollup": "^2.79.0",
"rollup-plugin-terser": "^7.0.2",
"semantic-release": "^19.0.2",
"ts-node": "^10.2.1",
Expand Down
76 changes: 64 additions & 12 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type GiteeRepo = {
access_token: string
name: string
description: string
private: boolean
}
/**
* 创建 Gitee 项目
Expand All @@ -52,7 +53,7 @@ async function createGiteeRepo(data: GiteeRepo) {
try {
const formData = new URLSearchParams()
Object.entries(data).forEach(([key, value]) => {
formData.append(key, value)
formData.append(key, String(value))
})
return await axios({
url: '/user/repos',
Expand All @@ -66,6 +67,37 @@ async function createGiteeRepo(data: GiteeRepo) {
}
}

type GithubRepo = {
name: string
description: string
private: boolean
}

/**
* 创建 Github 项目
*
* @author CaoMeiYouRen
* @date 2022-09-15
* @param data
*/
async function createGithubRepo(authToken: string, data: GithubRepo) {
try {
return await axios({
url: '/user/repos',
baseURL: GITHUB_API_URL,
method: 'POST',
headers: {
Authorization: `Bearer ${authToken}`,
Accept: 'application/vnd.github+json',
},
data,
})
} catch (error) {
console.error(error)
return null
}
}

/**
* 载入 token,优先寻找当前目录下的 .ctrc 文件,其次寻找 HOME 路径下的 .ctrc。
* github 和 gitee 的 token 各自独立寻找,不为空即为找到
Expand Down Expand Up @@ -97,7 +129,6 @@ async function loadToken(type: TokenType): Promise<string> {
console.error(error)
}
}
console.error(colors.red(`未找到 ${type} token !`))
return ''
}

Expand Down Expand Up @@ -287,12 +318,12 @@ export async function sleep(time: number) {
async function initRemoteGitRepo(projectPath: string, answers: InitAnswers) {
const loading = ora('正在初始化远程 Git 仓库……').start()
try {
const { name, description, gitRemoteUrl } = answers
if (gitRemoteUrl) {
await asyncExec(`git remote add origin ${gitRemoteUrl}`, {
cwd: projectPath,
})
}
const { name, description, gitRemoteUrl, isOpenSource } = answers

await asyncExec(`git remote add origin ${gitRemoteUrl}`, {
cwd: projectPath,
})

// 判断 remote 类型
let type = ''
if (/github\.com/.test(gitRemoteUrl)) {
Expand All @@ -303,29 +334,50 @@ async function initRemoteGitRepo(projectPath: string, answers: InitAnswers) {

switch (type) {
case 'github': {
break
const authToken = await loadToken(type)
if (!authToken) {
console.error(colors.red(`未找到 ${type} token !跳过初始化!`))
break
}
const resp = await createGithubRepo(authToken, {
name,
description,
private: isOpenSource,
})
if (resp?.status >= 200) {
loading.succeed('远程 Git 仓库初始化成功!')
console.info(colors.green(`远程 Git 仓库地址 ${resp.data?.html_url}`))
} else {
loading.fail('远程 Git 仓库初始化失败!')
}
return
}
case 'gitee': {
const access_token = await loadToken(type)
if (!access_token) {
console.error(colors.red(`未找到 ${type} token !跳过初始化!`))
break
}
const resp = await createGiteeRepo({
access_token,
name,
description,
private: true,
})
if (resp?.status >= 200) {
loading.succeed('远程 Git 仓库初始化成功!')
console.info(colors.green(`远程 Git 仓库地址 ${resp.data?.html_url}`))
} else {
loading.fail('远程 Git 仓库初始化失败!')
}
break
return
}
default: {
loading.stop()
console.info(colors.green(`请在远程 Git 仓库初始化 ${gitRemoteUrl}`))
break
}
}
loading.stop()
console.info(colors.green(`请在远程 Git 仓库初始化 ${gitRemoteUrl}`))
} catch (error) {
loading.fail('远程 Git 仓库初始化失败!')
console.error(error)
Expand Down

0 comments on commit 41419ba

Please sign in to comment.