File size: 3,367 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import chalk from 'chalk';
import fetch from 'isomorphic-fetch';
import ora from 'ora';
import simpleGit from 'simple-git';
import type { StatusResult } from 'simple-git';

import { version } from '../package.json';

const cwd = process.cwd();
const git = simpleGit(cwd);
const spinner = ora('Loading unicorns').start('开始检查仓库状态');

function exitProcess(code = 1) {
  console.log(''); // Keep an empty line here to make looks good~
  process.exit(code);
}

async function checkVersion() {
  spinner.start('正在检查当前版本是否已经存在');
  const checkUrls = ['https://registry.npmmirror.com/antd', 'http://registry.npmjs.org/antd'];
  const promises = checkUrls.map(
    (url) =>
      new Promise<Record<string, any>>((resolve) => {
        fetch(url)
          .then((res: Response) => res.json())
          .then(({ versions }) => {
            resolve(versions);
          })
          .catch(() => {
            // Do nothing.
          });
      }),
  );

  // Any one of the promises resolved, we can continue.
  const versions = Promise.race(promises);

  if (version in versions) {
    spinner.fail(chalk.yellow('😈 Current version already exists. Forget update package.json?'));
    spinner.info(`${chalk.cyan(' => Current:')}: version`);
    exitProcess();
  }
  spinner.succeed('版本检查通过');
}

async function checkBranch({ current }: StatusResult) {
  spinner.start('正在检查当前分支是否合法');
  if (
    version.includes('-alpha.') ||
    version.includes('-beta.') ||
    version.includes('-rc.') ||
    version.includes('-experimental.')
  ) {
    spinner.info(chalk.cyan('😃 Alpha version. Skip branch check.'));
  } else if (current !== 'master') {
    spinner.fail(chalk.red('🤔 You are not in the master branch!'));
    exitProcess();
  }
  spinner.succeed('分支检查通过');
}

async function checkCommit({ files }: StatusResult) {
  spinner.start('正在检查当前 git 状态');
  if (files.length) {
    spinner.fail(chalk.red('🙄 You forgot something to commit.'));
    files.forEach(({ path: filePath, working_dir: mark }) => {
      console.log(' -', chalk.red(mark), filePath);
    });
    exitProcess();
  }
  spinner.succeed('git 状态检查通过');
}

async function checkRemote() {
  spinner.start('正在检查远程分支');
  const { remote } = await git.fetch('origin', 'master');
  if (!remote?.includes('ant-design/ant-design')) {
    const { value } = await git.getConfig('remote.origin.url');
    if (!value?.includes('ant-design/ant-design')) {
      spinner.fail(
        chalk.red('🧐 Your remote origin is not ant-design/ant-design, did you fork it?'),
      );
      exitProcess();
    }
  }
  spinner.succeed('远程分支检查通过');
}

async function checkToken() {
  if (!process.env.GITHUB_ACCESS_TOKEN) {
    console.log(
      spinner.fail(
        chalk.red(
          '🚨 请先设置 GITHUB_ACCESS_TOKEN 环境变量到本地,请不要泄露给任何在线页面: https://octokit.github.io/rest.js/v20#authentication',
        ),
      ),
    );
    exitProcess();
  }
  spinner.succeed('GITHUB_ACCESS_TOKEN 检查通过');
}

export default async function checkRepo() {
  const status = await git.status();
  await checkVersion();
  await checkBranch(status);
  await checkCommit(status);
  await checkRemote();
  await checkToken();
}