/
index.js
156 lines (132 loc) · 5.05 KB
/
index.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const path = require('path')
const fs = require('fs-extra')
const exec = require('./util/exec')
const logger = require('./util/logger')
const runConfigs = require('./run')
const addComment = require('./add-comment')
const actionInfo = require('./prepare/action-info')()
const { mainRepoDir, diffRepoDir } = require('./constants')
const loadStatsConfig = require('./prepare/load-stats-config')
const { cloneRepo, mergeBranch, getCommitId, linkPackages, getLastStable } =
require('./prepare/repo-setup')(actionInfo)
const allowedActions = new Set(['synchronize', 'opened'])
if (!allowedActions.has(actionInfo.actionName) && !actionInfo.isRelease) {
logger(
`Not running for ${actionInfo.actionName} event action on repo: ${actionInfo.prRepo} and ref ${actionInfo.prRef}`
)
process.exit(0)
}
;(async () => {
try {
if (await fs.pathExists(path.join(__dirname, '../SKIP_NEXT_STATS.txt'))) {
console.log(
'SKIP_NEXT_STATS.txt file present, exiting stats generation..'
)
process.exit(0)
}
const { stdout: gitName } = await exec(
'git config user.name && git config user.email'
)
console.log('git author result:', gitName)
// clone PR/newer repository/ref first to get settings
if (!actionInfo.skipClone) {
await cloneRepo(actionInfo.prRepo, diffRepoDir, actionInfo.prRef)
}
if (actionInfo.isRelease) {
process.env.STATS_IS_RELEASE = 'true'
}
// load stats config from allowed locations
const { statsConfig, relativeStatsAppDir } = loadStatsConfig()
if (actionInfo.isLocal && actionInfo.prRef === statsConfig.mainBranch) {
throw new Error(
`'GITHUB_REF' can not be the same as mainBranch in 'stats-config.js'.\n` +
`This will result in comparing against the same branch`
)
}
if (actionInfo.isLocal) {
// make sure to use local repo location instead of the
// one provided in statsConfig
statsConfig.mainRepo = actionInfo.prRepo
}
/* eslint-disable-next-line */
actionInfo.commitId = await getCommitId(diffRepoDir)
let mainNextSwcVersion
if (!actionInfo.skipClone) {
let mainRef = statsConfig.mainBranch
if (actionInfo.isRelease) {
logger(`Release detected, using last stable tag: "${actionInfo.prRef}"`)
const lastStableTag = await getLastStable(diffRepoDir, actionInfo.prRef)
mainRef = lastStableTag
mainNextSwcVersion = lastStableTag
if (!lastStableTag) throw new Error('failed to get last stable tag')
logger(`using latestStable: "${lastStableTag}"`)
/* eslint-disable-next-line */
actionInfo.lastStableTag = lastStableTag
/* eslint-disable-next-line */
actionInfo.commitId = await getCommitId(diffRepoDir)
if (!actionInfo.customCommentEndpoint) {
/* eslint-disable-next-line */
actionInfo.commentEndpoint = `https://api.github.com/repos/${statsConfig.mainRepo}/commits/${actionInfo.commitId}/comments`
}
}
await cloneRepo(statsConfig.mainRepo, mainRepoDir, mainRef)
if (!actionInfo.isRelease && statsConfig.autoMergeMain) {
logger('Attempting auto merge of main branch')
await mergeBranch(statsConfig.mainBranch, mainRepoDir, diffRepoDir)
}
}
let mainRepoPkgPaths
let diffRepoPkgPaths
// run install/initialBuildCommand
const repoDirs = [mainRepoDir, diffRepoDir]
for (const dir of repoDirs) {
logger(`Running initial build for ${dir}`)
if (!actionInfo.skipClone) {
const usePnpm = await fs.pathExists(path.join(dir, 'pnpm-lock.yaml'))
if (!statsConfig.skipInitialInstall) {
await exec.spawnPromise(
`cd ${dir}${
usePnpm
? // --no-frozen-lockfile is used here to tolerate lockfile
// changes from merging latest changes
` && pnpm install --no-frozen-lockfile`
: ' && yarn install --network-timeout 1000000'
}`
)
await exec.spawnPromise(
statsConfig.initialBuildCommand ||
`cd ${dir} && ${usePnpm ? 'pnpm build' : 'echo built'}`
)
}
}
await fs
.copy(
path.join(__dirname, '../native'),
path.join(dir, 'packages/next-swc/native')
)
.catch(console.error)
logger(`Linking packages in ${dir}`)
const isMainRepo = dir === mainRepoDir
const pkgPaths = await linkPackages({
repoDir: dir,
nextSwcVersion: isMainRepo ? mainNextSwcVersion : undefined,
})
if (isMainRepo) mainRepoPkgPaths = pkgPaths
else diffRepoPkgPaths = pkgPaths
}
// run the configs and post the comment
const results = await runConfigs(statsConfig.configs, {
statsConfig,
mainRepoPkgPaths,
diffRepoPkgPaths,
relativeStatsAppDir,
})
await addComment(results, actionInfo, statsConfig)
logger('finished')
process.exit(0)
} catch (err) {
console.error('Error occurred generating stats:')
console.error(err)
process.exit(1)
}
})()