universo-platform-2d
170 строк · 4.2 Кб
1import { execSync } from 'node:child_process';2import { join } from 'node:path';3import { fileURLToPath } from 'node:url';4
5import { Clipboard } from '@napi-rs/clipboard';6import {7FetchOptions,8ProxyOptions,9RemoteCallbacks,10Repository,11Sort,12} from '@napi-rs/simple-git';13import chalk from 'chalk';14import { ProxyAgent, setGlobalDispatcher } from 'undici';15
16import corePackage from '../../packages/frontend/core/package.json' assert { type: 'json' };17
18const clipboard = new Clipboard();19
20const oldHash = corePackage.dependencies['@blocksuite/affine'].split('-').pop();21
22const info = await fetch('https://registry.npmjs.org/@blocksuite/affine').then(23res => res.json()24);25
26const latestVersion = info['dist-tags'].latest;27const latestHash = latestVersion.split('-').pop();28
29if (oldHash === latestHash) {30console.info(chalk.greenBright('Already updated'));31process.exit(0);32}
33
34if (process.env.http_proxy) {35setGlobalDispatcher(new ProxyAgent(process.env.http_proxy));36}
37
38console.info(`Upgrade blocksuite from ${oldHash} -> ${latestHash}`);39
40const blockSuiteDeps = execSync(`yarn info -A --name-only --json`, {41encoding: 'utf8',42});43
44const blocksuiteDepsList = blockSuiteDeps45.split('\n')46.map(s => s.trim())47.filter(Boolean)48.map(s => s.substring(1, s.length - 1))49.filter(50s => s.startsWith('@blocksuite') && !s.startsWith('@blocksuite/icons')51)52.map(s => s.split('@npm').at(0));53
54for (const pkg of blocksuiteDepsList) {55const command = `yarn up ${pkg}@${latestVersion}`;56console.info(chalk.bgCyan(`Executing ${command}`));57execSync(command, {58stdio: 'inherit',59});60}
61
62console.info(`Upgrade complete`);63
64const repo = new Repository(65join(fileURLToPath(import.meta.url), '..', '..', '..', '..', 'BlockSuite')66);67
68const remote = repo.remoteAnonymous(69'https://github.com/toeverything/BlockSuite.git'70);71
72remote.fetch(73['master'],74new FetchOptions().proxyOptions(new ProxyOptions().auto()).remoteCallback(75new RemoteCallbacks().transferProgress(progress => {76if (progress.totalDeltas && progress.totalObjects) {77console.log(78`${(79(progress.receivedObjects / progress.totalObjects) * 50 +80(progress.indexedDeltas / progress.totalDeltas) * 5081).toFixed(2)}%`82);83}84})85)86);87
88const latest = repo.findCommit(latestHash);89
90const commits = {91Features: [],92Bugfix: [],93Refactor: [],94Misc: [],95};96
97for (const oid of repo98.revWalk()99.push(latest.id())100.setSorting(Sort.Time & Sort.Topological)) {101if (oid.startsWith(oldHash)) {102break;103}104const commit = repo.findCommit(oid);105const summary = commit.summary();106if (summary.startsWith('feat')) {107commits.Features.push(commit);108} else if (summary.startsWith('fix')) {109commits.Bugfix.push(commit);110} else if (summary.startsWith('refactor')) {111commits.Refactor.push(commit);112} else {113commits.Misc.push(commit);114}115}
116
117clipboard.setText(await formatCommits(commits));118
119console.info(`Changelog copied to clipboard`);120
121async function formatCommits(commits) {122return `## Features123${await Promise.all(commits.Features.map(format)).then(commits =>124commits.join('\n')125)}126
127## Bugfix
128${await Promise.all(commits.Bugfix.map(format)).then(commits =>129commits.join('\n')130)}131
132## Refactor
133${await Promise.all(commits.Refactor.map(format)).then(commits =>134commits.join('\n')135)}136
137## Misc
138${await Promise.all(commits.Misc.map(format)).then(commits =>139commits.join('\n')140)}141`;142/**143* @param {import('./index').Commit} commit
144* @returns string
145*/
146async function format(commit) {147const summary = commit.summary();148const match = summary.match(/\(#(\d+)\)/);149if (match) {150const [_, pull] = match;151const pullInfo = await fetch(152`https://api.github.com/repos/toeverything/BlockSuite/pulls/${pull}`,153{154headers: {155Accept: 'application/vnd.github+json',156Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,157'X-GitHub-Api-Version': '2022-11-28',158},159}160)161.then(res => res.json())162.catch(() => ({ user: {} }));163const {164user: { login },165} = pullInfo;166return `- https://github.com/toeverything/BlockSuite/pull/${pull} @${login}`;167}168return `- ${summary}`;169}170}
171