Skip to content

Commit 1d7baaf

Browse files
committed
chore: use execFileSync for safety in release scripts
1 parent c3e5f11 commit 1d7baaf

3 files changed

Lines changed: 18 additions & 13 deletions

File tree

scripts/bump-nightly.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import process from 'node:process'
2-
import { execSync } from 'node:child_process'
2+
import { execFileSync } from 'node:child_process'
33
import { inc } from 'semver'
44
import { determineBumpType, getLatestTag, loadWorkspace } from './_utils.ts'
55

@@ -13,13 +13,13 @@ const nightlyPackages = {
1313
export async function bumpNightly () {
1414
const workspace = await loadWorkspace(process.cwd())
1515

16-
const commit = execSync('git rev-parse --short HEAD').toString('utf-8').trim().slice(0, 8)
16+
const commit = execFileSync('git', ['rev-parse', '--short', 'HEAD'], { encoding: 'utf-8' }).trim().slice(0, 8)
1717
const date = Math.round(Date.now() / (1000 * 60))
1818

1919
// TODO: revert after release of v4.2.0
2020
// Get the date of the latest tag to filter out merged history commits
2121
const latestTagName = await getLatestTag()
22-
const tagDate = execSync(`git log -1 --format=%ai ${latestTagName}`, { encoding: 'utf-8' })
22+
const tagDate = execFileSync('git', ['log', '-1', '--format=%ai', latestTagName], { encoding: 'utf-8' })
2323
const sinceDate = tagDate.trim()
2424

2525
const bumpType = await determineBumpType(sinceDate)

scripts/release.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-console */
22
import process from 'node:process'
3-
import { execSync } from 'node:child_process'
3+
import { execFileSync, execSync } from 'node:child_process'
44
import { copyFileSync, readFileSync, readdirSync, writeFileSync } from 'node:fs'
55
import { resolve } from 'node:path'
66

@@ -14,6 +14,11 @@ function execCommand (command: string, cwd?: string): void {
1414
execSync(command, { stdio: 'inherit', cwd })
1515
}
1616

17+
function execFile (file: string, args: string[], cwd?: string): void {
18+
console.info(`🔧 Running: ${file} ${args.join(' ')}`)
19+
execFileSync(file, args, { stdio: 'inherit', cwd })
20+
}
21+
1722
function readPackageJson (dir: string): PackageJson {
1823
const pkgPath = resolve(dir, 'package.json')
1924
return JSON.parse(readFileSync(pkgPath, 'utf-8'))
@@ -153,7 +158,7 @@ async function main () {
153158

154159
// Publish with primary tag with trusted publishing
155160
console.info(`🏷️ Publishing ${pkgDir} with tag: ${tag}`)
156-
execCommand(`pnpm publish --access public --no-git-checks --tag ${tag}`)
161+
execFile('pnpm', ['publish', '--access', 'public', '--no-git-checks', '--tag', tag])
157162

158163
const pkg = readPackageJson('.')
159164
published.push({ name: pkg.name, version: pkg.version })

scripts/update-changelog.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import process from 'node:process'
2-
import { execSync } from 'node:child_process'
2+
import { execFileSync } from 'node:child_process'
33
import { $fetch } from 'ofetch'
44
import { inc } from 'semver'
55
import { generateMarkDown, getCurrentGitBranch, loadChangelogConfig } from 'changelogen'
@@ -18,7 +18,7 @@ async function main () {
1818
// TODO: revert after release of v4.2.0
1919
// Get the date of the latest tag to filter out merged history commits
2020
const latestTagName = await getLatestTag()
21-
const tagDate = execSync(`git log -1 --format=%ai ${latestTagName}`, { encoding: 'utf-8' })
21+
const tagDate = execFileSync('git', ['log', '-1', '--format=%ai', latestTagName], { encoding: 'utf-8' })
2222
const sinceDate = tagDate.trim()
2323

2424
const commits = await getLatestCommits(sinceDate).then(commits => commits.filter(
@@ -30,19 +30,19 @@ async function main () {
3030
const changelog = await generateMarkDown(commits, config)
3131

3232
// Create and push a branch with bumped versions if it has not already been created
33-
const branchExists = execSync(`git ls-remote --heads origin v${newVersion}`).toString().trim().length > 0
33+
const branchExists = execFileSync('git', ['ls-remote', '--heads', 'origin', `v${newVersion}`], { encoding: 'utf-8' }).trim().length > 0
3434
if (!branchExists) {
35-
execSync('git config --global user.email "daniel@roe.dev"')
36-
execSync('git config --global user.name "Daniel Roe"')
37-
execSync(`git checkout -b v${newVersion}`)
35+
execFileSync('git', ['config', '--global', 'user.email', 'daniel@roe.dev'])
36+
execFileSync('git', ['config', '--global', 'user.name', 'Daniel Roe'])
37+
execFileSync('git', ['checkout', '-b', `v${newVersion}`])
3838

3939
for (const pkg of workspace.packages.filter(p => !p.data.private)) {
4040
workspace.setVersion(pkg.data.name, newVersion!)
4141
}
4242
await workspace.save()
4343

44-
execSync(`git commit -am v${newVersion}`)
45-
execSync(`git push -u origin v${newVersion}`)
44+
execFileSync('git', ['commit', '-am', `v${newVersion}`])
45+
execFileSync('git', ['push', '-u', 'origin', `v${newVersion}`])
4646
}
4747

4848
// Get the current PR for this release, if it exists

0 commit comments

Comments
 (0)