ant-design/scripts/post-publish.ts

162 lines
4.9 KiB
TypeScript
Raw Normal View History

import { execSync, spawnSync } from 'child_process';
import { confirm, select } from '@inquirer/prompts';
2023-09-11 09:47:36 +08:00
import chalk from 'chalk';
import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
import fetch from 'isomorphic-fetch';
import ora from 'ora';
import semver from 'semver';
import deprecatedVersions from '../BUG_VERSIONS.json';
import { version as packageVersion } from '../package.json';
2022-01-19 15:27:05 +08:00
2022-12-13 23:21:20 +08:00
dayjs.extend(relativeTime);
2023-04-28 16:57:25 +08:00
2022-11-16 23:36:34 +08:00
const CONCH_TAG = 'conch-v5';
2023-09-11 09:47:36 +08:00
function matchDeprecated(v: string) {
const match = Object.keys(deprecatedVersions).find((depreciated) =>
2023-09-11 09:47:36 +08:00
semver.satisfies(v, depreciated),
);
const reason = deprecatedVersions[match as keyof typeof deprecatedVersions] || [];
return {
match,
reason: Array.isArray(reason) ? reason : [reason],
};
}
const SAFE_DAYS_START = 1000 * 60 * 60 * 24 * 15; // 15 days
const SAFE_DAYS_DIFF = 1000 * 60 * 60 * 24 * 3; // 3 days not update seems to be stable
2022-01-19 15:27:05 +08:00
(async function process() {
console.log('🤖 Post Publish Scripting...\n');
2022-07-08 10:48:29 +08:00
// git tag
const spinner = ora(chalk.cyan(`Tagging ${packageVersion}`)).start();
execSync(`git tag ${packageVersion}`);
execSync(`git push origin ${packageVersion}:${packageVersion}`);
spinner.succeed(
chalk.cyan(
`Tagged ${packageVersion} 📦: https://github.com/ant-design/ant-design/releases/tag/${packageVersion}`,
),
);
console.log();
2022-07-08 10:48:29 +08:00
2022-11-16 23:36:34 +08:00
const { time, 'dist-tags': distTags } = await fetch('http://registry.npmjs.org/antd').then(
2023-09-11 09:47:36 +08:00
(res: Response) => res.json(),
2022-01-19 15:27:05 +08:00
);
2022-11-16 23:36:34 +08:00
console.log('🐚 Latest Conch Version:', chalk.green(distTags[CONCH_TAG] || 'null'), '\n');
2022-01-19 15:27:05 +08:00
// Sort and get the latest versions
const versionList = Object.keys(time)
2022-11-16 23:36:34 +08:00
.filter((version) => semver.valid(version) && !semver.prerelease(version))
2022-01-19 15:27:05 +08:00
.sort((v1, v2) => {
const time1 = dayjs(time[v1]).valueOf();
const time2 = dayjs(time[v2]).valueOf();
2022-01-19 15:27:05 +08:00
return time2 - time1;
});
// Slice for choosing the latest versions
const latestVersions = versionList
// Cut off
.slice(0, 30)
// Formatter
2022-11-16 23:36:34 +08:00
.map((version) => ({
publishTime: time[version],
2022-07-29 15:56:11 +08:00
timeDiff: dayjs().diff(dayjs(time[version])),
value: version,
depreciated: matchDeprecated(version).match,
}));
const filteredLatestVersions = latestVersions
// Filter deprecated versions
.filter(({ depreciated }) => !depreciated);
2022-01-19 15:27:05 +08:00
const startDefaultVersionIndex = filteredLatestVersions.findIndex(
({ timeDiff }) => timeDiff >= SAFE_DAYS_START,
);
const defaultVersionList = filteredLatestVersions
.slice(0, startDefaultVersionIndex + 1)
.reverse();
// Find safe version
let defaultVersionObj = null;
for (let i = 0; i < defaultVersionList.length - 1; i += 1) {
defaultVersionObj = defaultVersionList[i];
const nextVersionObj = defaultVersionList[i + 1];
if (defaultVersionObj.timeDiff - nextVersionObj.timeDiff > SAFE_DAYS_DIFF) {
break;
}
defaultVersionObj = null;
}
// Not find to use the latest version instead
defaultVersionObj = defaultVersionObj || defaultVersionList[defaultVersionList.length - 1];
let defaultVersion = defaultVersionObj ? defaultVersionObj.value : null;
// If default version is less than current, use current
2023-09-11 09:47:36 +08:00
if (semver.compare(defaultVersion!, distTags[CONCH_TAG]) < 0) {
2022-11-16 23:36:34 +08:00
defaultVersion = distTags[CONCH_TAG];
}
2022-01-19 15:27:05 +08:00
let conchVersion = await select({
default: defaultVersion,
message: 'Please select Conch Version:',
choices: latestVersions.map((info) => {
const { value, publishTime, depreciated } = info;
const desc = dayjs(publishTime).fromNow();
return {
value,
name: [
// Warning
depreciated ? '🚨' : '✅',
// Version
value,
// Date Diff
`(${desc})`,
// Default Mark
value === defaultVersion ? '(default)' : '',
// Current Mark
value === distTags[CONCH_TAG] ? chalk.gray('- current') : '',
]
.filter((str) => Boolean(String(str).trim()))
.join(' '),
};
}),
});
2022-01-19 15:27:05 +08:00
// Make sure it's not deprecated version
const deprecatedObj = matchDeprecated(conchVersion);
if (deprecatedObj.match) {
console.log('\n');
console.log(chalk.red('Deprecated For:'));
2022-11-16 23:36:34 +08:00
deprecatedObj.reason.forEach((reason) => {
console.log(chalk.yellow(` * ${reason}`));
});
console.log('\n');
const conchConfirm = await confirm({
default: false,
message: 'SURE to continue ?!!',
});
if (!conchConfirm) {
conchVersion = '';
}
}
2022-01-19 15:27:05 +08:00
// Check if need to update
2022-11-16 23:36:34 +08:00
if (!conchVersion || distTags[CONCH_TAG] === conchVersion) {
2022-01-19 15:27:05 +08:00
console.log(`🎃 Conch Version not change. Safe to ${chalk.green('ignore')}.`);
} else {
console.log('💾 Tagging Conch Version:', chalk.green(conchVersion));
2023-09-11 09:47:36 +08:00
spawnSync('npm', ['dist-tag', 'add', `antd@${conchVersion}`, CONCH_TAG], { stdio: 'inherit' });
2022-01-19 15:27:05 +08:00
}
})();