Skip to content

Commit 10fda8a

Browse files
authored
Update stats action for running locally (vercel#14143)
Noticed a few things that could be updated to make running the stats locally better e.g. if the folder name didn't match the name of the repo in the `stats-config.js` file it would fail to run and the `yarn` cache could break diffing from a cached package being used instead
1 parent 6163f30 commit 10fda8a

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

.github/actions/next-stats-action/src/constants.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ const mainRepoDir = path.join(workDir, mainRepoName)
88
const diffRepoDir = path.join(workDir, diffRepoName)
99
const statsAppDir = path.join(workDir, 'stats-app')
1010
const diffingDir = path.join(workDir, 'diff')
11+
const yarnEnvValues = {
12+
YARN_CACHE_FOLDER: path.join(workDir, 'yarn-cache'),
13+
}
1114
const allowedConfigLocations = [
1215
'./',
1316
'.stats-app',
@@ -24,5 +27,6 @@ module.exports = {
2427
mainRepoDir,
2528
diffRepoDir,
2629
statsAppDir,
30+
yarnEnvValues,
2731
allowedConfigLocations,
2832
}

.github/actions/next-stats-action/src/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ if (!allowedActions.has(actionInfo.actionName) && !actionInfo.isRelease) {
4646
)
4747
}
4848

49+
if (actionInfo.isLocal) {
50+
// make sure to use local repo location instead of the
51+
// one provided in statsConfig
52+
statsConfig.mainRepo = actionInfo.prRepo
53+
}
54+
4955
// clone main repository/ref
5056
if (!actionInfo.skipClone) {
5157
await cloneRepo(statsConfig.mainRepo, mainRepoDir)

.github/actions/next-stats-action/src/run/index.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const logger = require('../util/logger')
66
const getDirSize = require('./get-dir-size')
77
const collectStats = require('./collect-stats')
88
const collectDiffs = require('./collect-diffs')
9-
const { statsAppDir, diffRepoDir, mainRepoDir } = require('../constants')
9+
const { statsAppDir, diffRepoDir, yarnEnvValues } = require('../constants')
1010

1111
async function runConfigs(
1212
configs = [],
@@ -32,12 +32,7 @@ async function runConfigs(
3232

3333
// if stats-config is in root of project we're analyzing
3434
// the whole project so copy from each repo
35-
const curStatsAppPath =
36-
relativeStatsAppDir === './'
37-
? mainRepoStats
38-
? diffRepoDir
39-
: mainRepoDir
40-
: path.join(diffRepoDir, relativeStatsAppDir)
35+
const curStatsAppPath = path.join(diffRepoDir, relativeStatsAppDir)
4136

4237
// clean statsAppDir
4338
await fs.remove(statsAppDir)
@@ -61,7 +56,9 @@ async function runConfigs(
6156
}
6257

6358
const buildStart = new Date().getTime()
64-
await exec(`cd ${statsAppDir} && ${statsConfig.appBuildCommand}`)
59+
await exec(`cd ${statsAppDir} && ${statsConfig.appBuildCommand}`, false, {
60+
env: yarnEnvValues,
61+
})
6562
curStats.General.buildDuration = new Date().getTime() - buildStart
6663

6764
// apply renames to get deterministic output names
@@ -182,7 +179,11 @@ async function linkPkgs(pkgDir = '', pkgPaths) {
182179
}
183180
}
184181
await fs.writeFile(pkgJsonPath, JSON.stringify(pkgData, null, 2), 'utf8')
185-
await exec(`cd ${pkgDir} && yarn install`)
182+
183+
await fs.remove(yarnEnvValues.YARN_CACHE_FOLDER)
184+
await exec(`cd ${pkgDir} && yarn install`, false, {
185+
env: yarnEnvValues,
186+
})
186187
}
187188

188189
module.exports = runConfigs

.github/actions/next-stats-action/src/util/exec.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ const env = {
99
PR_STATS_COMMENT_TOKEN: '',
1010
}
1111

12-
function exec(command, noLog = false) {
12+
function exec(command, noLog = false, opts = {}) {
1313
if (!noLog) logger(`exec: ${command}`)
14-
return execP(command, { env, timeout: 180 * 1000 })
14+
return execP(command, {
15+
timeout: 180 * 1000,
16+
...opts,
17+
env: { ...env, ...opts.env },
18+
})
1519
}
1620

1721
exec.spawn = function spawn(command = '', opts = {}) {

0 commit comments

Comments
 (0)