ant-design/scripts/post-script.js

96 lines
3.0 KiB
JavaScript
Raw Normal View History

2022-01-24 11:26:26 +08:00
/* eslint-disable no-console */
2022-01-19 15:27:05 +08:00
const fetch = require('isomorphic-fetch');
const semver = require('semver');
const dayjs = require('dayjs');
2022-01-19 15:27:05 +08:00
const inquirer = require('inquirer');
const chalk = require('chalk');
const { spawnSync } = require('child_process');
2022-07-08 10:48:29 +08:00
const packageJson = require('../package.json');
2022-01-19 15:27:05 +08:00
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(chalk.cyan('🤖 Post Publish Scripting...\n'));
2022-07-08 10:48:29 +08:00
if (packageJson.version.startsWith('5.0')) {
console.log(chalk.green('🤖 Next version, skipped.'));
return;
}
2022-01-19 15:27:05 +08:00
const { time, 'dist-tags': distTags } = await fetch('http://registry.npmjs.org/antd').then(res =>
res.json(),
);
console.log('🐚 Latest Conch Version:', chalk.green(distTags.conch || 'null'), '\n');
// Sort and get the latest versions
const versionList = Object.keys(time)
.filter(version => semver.valid(version) && !semver.prerelease(version))
.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.slice(0, 20).map(version => ({
2022-01-19 15:27:05 +08:00
publishTime: time[version],
timeDiff: dayjs().diff(dayjs(time[version])),
2022-01-19 15:27:05 +08:00
value: version,
}));
const startDefaultVersionIndex = latestVersions.findIndex(
({ timeDiff }) => timeDiff >= SAFE_DAYS_START,
);
const defaultVersionList = latestVersions.slice(0, startDefaultVersionIndex + 1).reverse();
// Find safe version
let defaultVersionObj;
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];
2022-01-19 15:27:05 +08:00
const defaultVersion = defaultVersionObj ? defaultVersionObj.value : null;
// Selection
const { conchVersion } = await inquirer.prompt([
{
type: 'list',
name: 'conchVersion',
default: defaultVersion,
message: 'Please select Conch Version:',
choices: latestVersions.map(info => {
const { value, publishTime } = info;
const desc = dayjs(publishTime).fromNow();
2022-01-19 15:27:05 +08:00
return {
...info,
name: `${value} (${desc}) ${value === defaultVersion ? '(default)' : ''}`,
};
}),
},
]);
// Check if need to update
if (distTags.conch === conchVersion) {
console.log(`🎃 Conch Version not change. Safe to ${chalk.green('ignore')}.`);
} else {
console.log('💾 Tagging Conch Version:', chalk.green(conchVersion));
spawnSync('npm', ['dist-tag', 'add', `antd@${conchVersion}`, 'conch'], {
stdio: 'inherit',
stdin: 'inherit',
});
}
})();