Commit 957d1c3e authored by Benjamin Woodruff's avatar Benjamin Woodruff

chore(CI): Port run-for-change script to use `yargs`

parent 71a500b9
...@@ -48,7 +48,7 @@ jobs: ...@@ -48,7 +48,7 @@ jobs:
elif [ '${{ github.event_name }}' == 'workflow_dispatch' ] elif [ '${{ github.event_name }}' == 'workflow_dispatch' ]
then then
echo "value=force-preview" >> $GITHUB_OUTPUT echo "value=force-preview" >> $GITHUB_OUTPUT
elif [[ $(node scripts/run-for-change.js --not --type docs --exec echo 'false') != 'false' ]]; elif [[ $(node scripts/run-for-change.js --not --type docs -- echo 'false') != 'false' ]];
then then
echo "value=skipped" >> $GITHUB_OUTPUT echo "value=skipped" >> $GITHUB_OUTPUT
else else
......
...@@ -47,7 +47,7 @@ jobs: ...@@ -47,7 +47,7 @@ jobs:
id: docs-change id: docs-change
run: | run: |
echo "DOCS_ONLY<<EOF" >> $GITHUB_OUTPUT; echo "DOCS_ONLY<<EOF" >> $GITHUB_OUTPUT;
echo "$(node scripts/run-for-change.js --not --type docs --exec echo 'false')" >> $GITHUB_OUTPUT; echo "$(node scripts/run-for-change.js --not --type docs -- echo 'false')" >> $GITHUB_OUTPUT;
echo 'EOF' >> $GITHUB_OUTPUT echo 'EOF' >> $GITHUB_OUTPUT
- name: check for release - name: check for release
......
...@@ -44,7 +44,7 @@ jobs: ...@@ -44,7 +44,7 @@ jobs:
fetch-depth: 25 fetch-depth: 25
- name: Check non-docs only change - name: Check non-docs only change
run: echo "DOCS_CHANGE<<EOF" >> $GITHUB_OUTPUT; echo "$(node scripts/run-for-change.js --not --type docs --exec echo 'nope')" >> $GITHUB_OUTPUT; echo 'EOF' >> $GITHUB_OUTPUT run: echo "DOCS_CHANGE<<EOF" >> $GITHUB_OUTPUT; echo "$(node scripts/run-for-change.js --not --type docs -- echo 'nope')" >> $GITHUB_OUTPUT; echo 'EOF' >> $GITHUB_OUTPUT
id: docs-change id: docs-change
- uses: actions/download-artifact@v4 - uses: actions/download-artifact@v4
......
const { promisify } = require('util') const { promisify } = require('util')
const { exec: execOrig, spawn } = require('child_process') const { exec: execOrig, spawn } = require('child_process')
const yargs = require('yargs')
const exec = promisify(execOrig) const exec = promisify(execOrig)
...@@ -54,8 +55,64 @@ const CHANGE_ITEM_GROUPS = { ...@@ -54,8 +55,64 @@ const CHANGE_ITEM_GROUPS = {
} }
async function main() { async function main() {
let eventData = {} const argv = await yargs(process.argv.slice(2))
.usage(
'Usage: $0 --type <type> [--not] [--always-canary] (--exec <command...> | --listChangedDirectories)'
)
.option('type', {
alias: 't',
description: 'Specify the type of change group to check against',
type: 'string',
choices: Object.keys(CHANGE_ITEM_GROUPS),
demandOption: true,
})
.option('not', {
description:
'Execute if *any* file *not* in the specified type group changed',
type: 'boolean',
default: false,
})
.option('always-canary', {
description: 'Always execute/list if the current branch is canary',
type: 'boolean',
default: false,
})
.option('listChangedDirectories', {
alias: 'l',
description:
'List matching changed directories instead of executing a command. ' +
'Incompatible with a command specified using postional arguments.',
type: 'boolean',
default: false,
})
.check((argv) => {
if (argv['--'].length === 0 && !argv.listChangedDirectories) {
throw new Error(
'Must provide either a command after options ' +
'(e.g., `$0 -- my-cmd with args`) or use --listChangedDirectories'
)
}
if (argv['--'].length > 0 && argv.listChangedDirectories) {
throw new Error(
'Both a command and --listChangedDirectories provided. ' +
'Prioritizing --listChangedDirectories.'
)
}
return true
})
.help()
.alias('help', 'h')
.example(
'$0 --type docs -- echo "docs changed!"',
'Prints "docs changed" if any docs have changed'
)
.example(
'$0 --type docs --listChangedDirectories',
'Prints the list of changed directories'
)
.strict().argv // Ensure only defined options/commands are used
let eventData = {}
try { try {
eventData = require(process.env.GITHUB_EVENT_PATH)['pull_request'] || {} eventData = require(process.env.GITHUB_EVENT_PATH)['pull_request'] || {}
} catch (_) {} } catch (_) {}
...@@ -63,7 +120,7 @@ async function main() { ...@@ -63,7 +120,7 @@ async function main() {
const branchName = const branchName =
eventData?.head?.ref || eventData?.head?.ref ||
process.env.GITHUB_REF_NAME || process.env.GITHUB_REF_NAME ||
(await exec('git rev-parse --abbrev-ref HEAD')).stdout (await exec('git rev-parse --abbrev-ref HEAD')).stdout.trim()
const remoteUrl = const remoteUrl =
eventData?.head?.repo?.full_name || eventData?.head?.repo?.full_name ||
...@@ -71,7 +128,7 @@ async function main() { ...@@ -71,7 +128,7 @@ async function main() {
(await exec('git remote get-url origin')).stdout (await exec('git remote get-url origin')).stdout
const isCanary = const isCanary =
branchName.trim() === 'canary' && remoteUrl.includes('vercel/next.js') branchName === 'canary' && remoteUrl.includes('vercel/next.js')
try { try {
await exec('git remote set-branches --add origin canary') await exec('git remote set-branches --add origin canary')
...@@ -94,46 +151,17 @@ async function main() { ...@@ -94,46 +151,17 @@ async function main() {
console.error({ branchName, remoteUrl, isCanary, changesResult }) console.error({ branchName, remoteUrl, isCanary, changesResult })
const changedFilesOutput = changesResult.stdout const changedFilesOutput = changesResult.stdout
const typeIndex = process.argv.indexOf('--type') const { type, not: isNegated, alwaysCanary, listChangedDirectories } = argv
const type = typeIndex > -1 && process.argv[typeIndex + 1] const execArgs = argv._
const isNegated = process.argv.indexOf('--not') > -1
const alwaysCanary = process.argv.indexOf('--always-canary') > -1
if (!type) {
throw new Error(
`Missing "--type" flag, e.g. "node run-for-change.js --type docs"`
)
}
const execArgIndex = process.argv.indexOf('--exec')
const listChangedDirectories = process.argv.includes(
'--listChangedDirectories'
)
if (execArgIndex < 0 && !listChangedDirectories) {
throw new Error(
'Invalid: must provide either "--exec" or "--listChangedDirectories" flag'
)
}
let hasMatchingChange = false let hasMatchingChange = false
// `type` is validated by yargs `choices`
const changeItems = CHANGE_ITEM_GROUPS[type] const changeItems = CHANGE_ITEM_GROUPS[type]
const execArgs = process.argv.slice(execArgIndex + 1)
if (execArgs.length < 1 && !listChangedDirectories) {
throw new Error('Missing exec arguments after "--exec"')
}
if (!changeItems) {
throw new Error(
`Invalid change type, allowed types are ${Object.keys(
CHANGE_ITEM_GROUPS
).join(', ')}`
)
}
let changedFilesCount = 0 let changedFilesCount = 0
let changedDirectories = [] let changedDirectories = []
// always run for canary if flag is enabled // always run for canary if flag is enabled
if (alwaysCanary && branchName === 'canary') { if (alwaysCanary && isCanary) {
changedFilesCount += 1 changedFilesCount += 1
hasMatchingChange = true hasMatchingChange = true
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment