Skip to content

Commit

Permalink
feat: enable locking build-tools to a specific sha (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere authored Apr 24, 2024
1 parent 5ee91ad commit 5c8e1d5
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions preinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const { spawnSync } = require('child_process');
const { homedir } = require('os');
const path = require('path');

const isWin = process.platform === 'win32';

function throwForBadSpawn(basicInfo, spawnSyncResult) {
if (spawnSyncResult.status !== 0) {
throw new Error(`Command "${basicInfo}" failed with exit code ${spawnSyncResult.status}`);
Expand All @@ -17,35 +19,46 @@ function install() {

try {
// Clone build-tools into user homedir
if (existsSync(installPath)) {
const alreadyCloned = existsSync(installPath);
if (alreadyCloned) {
throwForBadSpawn(
'git fetch',
spawnSync('git', ['fetch'], { stdio: 'inherit', cwd: installPath }),
);
} else {
throwForBadSpawn(
'git checkout main',
spawnSync('git', ['checkout', 'main', '-f'], { stdio: 'inherit', cwd: installPath }),
'git clone',
spawnSync('git', ['clone', '-q', BUILD_TOOLS_URL, installPath], { stdio: 'inherit' }),
);
}

const shouldCheckout = !alreadyCloned || !!process.env.BUILD_TOOLS_SHA;
if (shouldCheckout) {
const checkoutSha = process.env.BUILD_TOOLS_SHA ?? 'main';
throwForBadSpawn(
'git reset',
spawnSync('git', ['reset', '--hard', 'origin/main'], {
stdio: 'inherit',
cwd: installPath,
}),
`git checkout ${checkoutSha}`,
spawnSync('git', ['checkout', checkoutSha, '-f'], { stdio: 'inherit', cwd: installPath }),
);
} else {
throwForBadSpawn(
'git clone',
spawnSync('git', ['clone', '-q', BUILD_TOOLS_URL, installPath], { stdio: 'inherit' }),
'git reset',
spawnSync(
'git',
['reset', '--hard', `${checkoutSha === 'main' ? 'origin/main' : checkoutSha}`],
{
stdio: 'inherit',
cwd: installPath,
},
),
);
}

// Install build-tools deps.
throwForBadSpawn(
'yarn install',
spawnSync(`npx${process.platform === 'win32' ? '.cmd' : ''}`, ['yarn', 'install'], {
spawnSync(`npx${isWin ? '.cmd' : ''}`, ['yarn', 'install'], {
stdio: 'inherit',
cwd: installPath,
shell: isWin,
}),
);
} catch (err) {
Expand Down

0 comments on commit 5c8e1d5

Please sign in to comment.