|
| 1 | +import path from 'node:path'; |
| 2 | +import { promises as fs } from 'node:fs'; |
| 3 | + |
| 4 | +import { chromiumGit, v8Deps } from './constants.js'; |
| 5 | +import { forceRunAsync } from '../run.js'; |
| 6 | +import { |
| 7 | + addToGitignore, |
| 8 | + filterForVersion, |
| 9 | + getNodeV8Version, |
| 10 | + removeDirectory, |
| 11 | + replaceGitignore, |
| 12 | +} from './util.js'; |
| 13 | + |
| 14 | +async function fetchFromGit(cwd, repo, commit) { |
| 15 | + await removeDirectory(cwd); |
| 16 | + await fs.mkdir(cwd, { recursive: true }); |
| 17 | + await exec('init'); |
| 18 | + await exec('remote', 'add', 'origin', repo); |
| 19 | + await exec('fetch', 'origin', commit); |
| 20 | + await exec('reset', '--hard', 'FETCH_HEAD'); |
| 21 | + await removeDirectory(path.join(cwd, '.git')); |
| 22 | + |
| 23 | + function exec(...options) { |
| 24 | + return forceRunAsync('git', options, { |
| 25 | + ignoreFailure: false, |
| 26 | + spawnArgs: { cwd, stdio: 'ignore' } |
| 27 | + }); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +async function readDeps(nodeDir) { |
| 32 | + const depsStr = await fs.readFile(path.join(nodeDir, 'deps/v8/DEPS'), 'utf8'); |
| 33 | + const start = depsStr.indexOf('deps = {'); |
| 34 | + const end = depsStr.indexOf('\n}', start) + 2; |
| 35 | + const depsDeclaration = depsStr.substring(start, end).replace(/^ *#.*/gm, ''); |
| 36 | + const Var = () => chromiumGit; // eslint-disable-line no-unused-vars |
| 37 | + let deps; |
| 38 | + eval(depsDeclaration); // eslint-disable-line no-eval |
| 39 | + return deps; |
| 40 | +} |
| 41 | + |
| 42 | +async function lookupDep(depsTable, depName) { |
| 43 | + const dep = depsTable[depName]; |
| 44 | + if (!dep) { |
| 45 | + throw new Error(`V8 dep "${depName}" not found in DEPS file`); |
| 46 | + } |
| 47 | + if (typeof dep === 'object') { |
| 48 | + return dep.url.split('@'); |
| 49 | + } |
| 50 | + return dep.split('@'); |
| 51 | +} |
| 52 | + |
| 53 | +export default function updateV8Deps() { |
| 54 | + return { |
| 55 | + title: 'Update V8 DEPS', |
| 56 | + task: async(ctx, task) => { |
| 57 | + const newV8Version = await getNodeV8Version(ctx.nodeDir); |
| 58 | + const repoPrefix = newV8Version.majorMinor >= 86 ? '' : 'v8/'; |
| 59 | + const deps = filterForVersion(v8Deps.map((v8Dep) => ({ |
| 60 | + ...v8Dep, |
| 61 | + repo: `${repoPrefix}${v8Dep.repo}`, |
| 62 | + path: v8Dep.repo |
| 63 | + })), newV8Version); |
| 64 | + if (deps.length === 0) return; |
| 65 | + const depsTable = await readDeps(ctx.nodeDir); |
| 66 | + const subtasks = []; |
| 67 | + for (const dep of deps) { |
| 68 | + // Update .gitignore sequentially to avoid races |
| 69 | + if (dep.gitignore) { |
| 70 | + if (typeof dep.gitignore === 'string') { |
| 71 | + await addToGitignore(ctx.nodeDir, dep.gitignore); |
| 72 | + } else { |
| 73 | + await replaceGitignore(ctx.nodeDir, dep.gitignore); |
| 74 | + } |
| 75 | + } |
| 76 | + subtasks.push({ |
| 77 | + title: `Update ${dep.path}`, |
| 78 | + task: async(ctx) => { |
| 79 | + const [repo, commit] = await lookupDep(depsTable, dep.repo); |
| 80 | + const thePath = path.join(ctx.nodeDir, 'deps/v8', dep.path); |
| 81 | + await fetchFromGit(thePath, repo, commit); |
| 82 | + } |
| 83 | + }); |
| 84 | + } |
| 85 | + return task.newListr(subtasks, { concurrent: ctx.concurrent }); |
| 86 | + } |
| 87 | + }; |
| 88 | +}; |
0 commit comments