|
| 1 | +import { mkdir, rm, writeFile } from 'node:fs/promises'; |
| 2 | +import { getCachePath, packagesToPrecache } from '../shared/constants.ts'; |
| 3 | +import { fetchPackageVersions } from '../shared/npm.ts'; |
| 4 | +import spawn from 'nano-spawn'; |
| 5 | + |
| 6 | +const environmentVariable = 'YARN_CACHE_FOLDER'; |
| 7 | +const globalEnvironmentVariable = 'YARN_GLOBAL_FOLDER'; |
| 8 | +const cachePath = getCachePath('yarn'); |
| 9 | + |
| 10 | +console.log(`Cache location: ${cachePath}`); |
| 11 | + |
| 12 | +const cacheState: Record<string, string[]> = {}; |
| 13 | + |
| 14 | +const tmpDir = new URL('../../data/yarn_tmp/', import.meta.url); |
| 15 | + |
| 16 | +{ |
| 17 | + // Warm up the temp directory |
| 18 | + await mkdir(tmpDir, { recursive: true }); |
| 19 | + |
| 20 | + await writeFile(new URL('package.json', tmpDir), JSON.stringify({ private: true })); |
| 21 | + await writeFile(new URL('yarn.lock', tmpDir), ''); |
| 22 | + |
| 23 | + await spawn('yarn', 'config set enableGlobalCache true'.split(' '), { |
| 24 | + cwd: tmpDir, |
| 25 | + }); |
| 26 | + |
| 27 | + await spawn('yarn', 'config set nodeLinker node-modules'.split(' '), { |
| 28 | + cwd: tmpDir, |
| 29 | + }); |
| 30 | + |
| 31 | + await spawn('yarn', 'set version stable --yarn-path'.split(' '), { |
| 32 | + cwd: tmpDir, |
| 33 | + }); |
| 34 | +} |
| 35 | + |
| 36 | +for (const packageName of packagesToPrecache) { |
| 37 | + const lastFiveVersions = (await fetchPackageVersions(packageName)).slice(-5); |
| 38 | + cacheState[packageName] = lastFiveVersions; |
| 39 | + |
| 40 | + for (const version of lastFiveVersions) { |
| 41 | + console.log(`Fetching ${packageName}@${version}`); |
| 42 | + |
| 43 | + try { |
| 44 | + await spawn('yarn', ['add', `${packageName}@${version}`], { |
| 45 | + env: { |
| 46 | + [environmentVariable]: cachePath, |
| 47 | + [globalEnvironmentVariable]: cachePath, |
| 48 | + YARN_ENABLE_GLOBAL_CACHE: 'true', |
| 49 | + }, |
| 50 | + cwd: tmpDir, |
| 51 | + }); |
| 52 | + } catch (error) { |
| 53 | + console.error(`Failed to fetch ${packageName}@${version}:`, error); |
| 54 | + } finally { |
| 55 | + console.log(`Done fetching ${packageName}@${version}`); |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +await writeFile(new URL('../../data/yarn_state.json', import.meta.url), JSON.stringify(cacheState, null, '\t')); |
| 61 | + |
| 62 | +await rm(tmpDir, { recursive: true }); |
0 commit comments