-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathsync.js
60 lines (48 loc) · 1.92 KB
/
sync.js
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
import { Command } from 'commander';
import SimpleGit from 'simple-git';
import open from 'open';
const program = new Command();
async function getLatestSyncHash () {
const git = SimpleGit();
const history = await git.log(['origin/main']);
const latestSync = history.all.find(v => /^sync #\w{7}\w?\s?$/i.test(v.body))?.body?.trim();
// console.log(history.all[0])
if (!latestSync) {
throw new Error('No hash found. Are there any commit with `sync #hash` message in git history?');
}
// console.log(latestSync)
return latestSync.match(/#(\w{7}\w?)/)[1];
}
async function getLatestUpstreamHash () {
const git = SimpleGit();
const latestUpstreamHash = await git.show(['-s', '--format="%h"', 'origin/upstream']);
return latestUpstreamHash.trim().replace(/"/g, '');
}
program
.name('sync')
.description('The sync helper CLI');
program.command('compare')
.description('Open the github website to compare diffs to sync.')
.action(async () => {
open(`https://github.com/vuejs/docs/compare/${await getLatestSyncHash()}...${await getLatestUpstreamHash()}`);
});
program.command('pr')
.description('Generate sync PR\'s title and content.')
.option('-o, --open', 'directly open link to create pr (sync branch->main branch)')
.action(async (options) => {
const latestUpstreamHash = await getLatestUpstreamHash();
const latestSyncHash = await getLatestSyncHash();
const title = `Sync #${latestUpstreamHash}`;
const body = `## Description of Problem
https://github.com/vuejs/docs/compare/${latestSyncHash}...${latestUpstreamHash}
`;
console.log(`
--- PR Title ---:
${title}
--- PR Content ---:
${body}`);
if (options.open) {
open(`https://github.com/vuejs-translations/docs-zh-cn/compare/main...sync?quick_pull=1&title=${encodeURIComponent(title)}&body=${encodeURIComponent(body)}&labels=${encodeURIComponent('从英文版同步,请使用 merge commit 合并')}`);
}
});
program.parse();