diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 7a3749d..ef429d6 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 18523f1..681439c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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: diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..319e41e --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +strict-peer-dependencies=false diff --git a/README.md b/README.md index bf17c11..f6d65ac 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index abb7427..8f2844f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/utils.ts b/src/utils.ts index de11639..8e9234e 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -40,6 +40,7 @@ type GiteeRepo = { access_token: string name: string description: string + private: boolean } /** * 创建 Gitee 项目 @@ -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', @@ -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 各自独立寻找,不为空即为找到 @@ -97,7 +129,6 @@ async function loadToken(type: TokenType): Promise { console.error(error) } } - console.error(colors.red(`未找到 ${type} token !`)) return '' } @@ -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)) { @@ -303,14 +334,35 @@ 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 仓库初始化成功!') @@ -318,14 +370,14 @@ async function initRemoteGitRepo(projectPath: string, answers: InitAnswers) { } 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)