|
| 1 | +import execa from 'execa'; |
| 2 | +import path from 'path'; |
| 3 | +import fs from 'fs-extra'; |
| 4 | +import { TurboDryRun } from './types'; |
| 5 | + |
| 6 | +const rootDir = path.join(__dirname, '..'); |
| 7 | + |
| 8 | +async function main() { |
| 9 | + const { stdout: sha } = await execa('git', ['rev-parse', '--short', 'HEAD'], { |
| 10 | + cwd: rootDir, |
| 11 | + }); |
| 12 | + const { stdout: turboStdout } = await execa( |
| 13 | + 'turbo', |
| 14 | + ['run', 'build', '--dry=json'], |
| 15 | + { |
| 16 | + cwd: rootDir, |
| 17 | + } |
| 18 | + ); |
| 19 | + const turboJson: TurboDryRun = JSON.parse(turboStdout); |
| 20 | + for (const task of turboJson.tasks) { |
| 21 | + const dir = path.join(rootDir, task.directory); |
| 22 | + const packageJsonPath = path.join(dir, 'package.json'); |
| 23 | + const originalPackageObj = await fs.readJson(packageJsonPath); |
| 24 | + const packageObj = await fs.readJson(packageJsonPath); |
| 25 | + packageObj.version += `-${sha.trim()}`; |
| 26 | + |
| 27 | + if (task.dependencies.length > 0) { |
| 28 | + for (const dependency of task.dependencies) { |
| 29 | + const name = dependency.split('#')[0]; |
| 30 | + const tarballUrl = `https://${process.env.VERCEL_URL}/tarballs/${name}.tgz`; |
| 31 | + if (packageObj.dependencies && name in packageObj.dependencies) { |
| 32 | + packageObj.dependencies[name] = tarballUrl; |
| 33 | + } |
| 34 | + if (packageObj.devDependencies && name in packageObj.devDependencies) { |
| 35 | + packageObj.devDependencies[name] = tarballUrl; |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + await fs.writeJson(packageJsonPath, packageObj, { spaces: 2 }); |
| 40 | + |
| 41 | + await execa('yarn', ['pack'], { |
| 42 | + cwd: dir, |
| 43 | + stdio: 'inherit', |
| 44 | + }); |
| 45 | + await fs.writeJson(packageJsonPath, originalPackageObj, { spaces: 2 }); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +main().catch(err => { |
| 50 | + console.log('error running pack:', err); |
| 51 | + process.exit(1); |
| 52 | +}); |
0 commit comments