|
| 1 | + |
| 2 | +import { execSync, spawnSync } from 'node:child_process'; |
| 3 | +import {existsSync,readFileSync, rmSync, writeFileSync } from 'node:fs'; |
| 4 | +import { createRequire } from 'node:module'; |
| 5 | +import { dirname, join, relative } from 'node:path'; |
| 6 | +import { fileURLToPath } from 'node:url'; |
| 7 | + |
| 8 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 9 | +const require = createRequire(import.meta.url); |
| 10 | +const env = {...process.env, NODE_OPTIONS: '--no-deprecation'}; |
| 11 | + |
| 12 | +export function installTarballAsDependency(root) { |
| 13 | + const pkgJson = require('../package.json'); |
| 14 | + const normalizedName = pkgJson.name.replace('@', '').replace('/', '-'); |
| 15 | + |
| 16 | + const tarball = join(__dirname, '..', `${normalizedName}-${pkgJson.version}.tgz`); |
| 17 | + |
| 18 | + if (!existsSync(tarball)) { |
| 19 | + console.error(`Tarball not found: '${tarball}'`); |
| 20 | + console.error(`Run 'yarn build && yarn build:tarball' first`); |
| 21 | + process.exit(1); |
| 22 | + } |
| 23 | + |
| 24 | + const tarballRelative = relative(root, tarball); |
| 25 | + |
| 26 | + console.log('Clearing node_modules...'); |
| 27 | + rmSync(join(root, 'node_modules'), { recursive: true, force: true }); |
| 28 | + console.log('Clearing yarn.lock...'); |
| 29 | + rmSync(join(root, 'yarn.lock'), { force: true }); |
| 30 | + |
| 31 | + console.log('Clearing yarn cache...'); |
| 32 | + spawnSync(`yarn cache clean ${pkgJson.name}`, { shell: true, stdio: 'inherit', env }); |
| 33 | + // Yarn has a bug where 'yarn cache clean X' does not remove the temp directory where the tgz is unpacked to. |
| 34 | + // This means installing from local tgz does not update when src changes are made https://github.com/yarnpkg/yarn/issues/5357 |
| 35 | + const dirResult = spawnSync('yarn cache dir', { shell: true, env }); |
| 36 | + const tmpDir = join(dirResult.output.toString().replace(/[,\n\r]/g, ''), '.tmp'); |
| 37 | + rmSync(tmpDir, { recursive: true, force: true }); |
| 38 | + |
| 39 | + const pkg = readFileSync(join(root, 'package.json.template'), 'utf-8'); |
| 40 | + const modified = pkg.replace(/"{{path}}"/, JSON.stringify(`file:${tarballRelative}`)); |
| 41 | + writeFileSync(join(root, 'package.json'), modified); |
| 42 | + |
| 43 | + console.log('Installing dependencies...'); |
| 44 | + execSync('yarn install', { cwd: root }); |
| 45 | +} |
0 commit comments