@ 261105.4
current
import assert from 'node:assert';
import path from 'node:path';
import { promises as fs } from 'node:fs';

import { getNodeV8Version } from './util.js';
import { forceRunAsync } from '../run.js';

export function getV8Version(label) {
  return {
    title: `Get ${label} V8 version`,
    task: async(ctx) => {
      ctx[`${label}Version`] = await getNodeV8Version(ctx.nodeDir);
    }
  };
}

export function checkCargoAvailable() {
  return {
    title: 'Check Cargo exists in environment',
    task: async(ctx) => {
      try {
        const output = await forceRunAsync(ctx.cargo, ['--version'], {
          ignoreFailure: false,
          captureStdout: true,
        });
        assert(output.startsWith('cargo'));
      } catch (cause) {
        let error = 'Unable to determine cargo version.';
        if (ctx.cargo === 'cargo') {
          error += '\nYou may need to specify the path to your cargo binary with `--cargo`.';
        }
        throw new Error(error, { cause });
      }
    }
  };
}

export async function checkCwd(ctx) {
  let isNode = false;
  try {
    const nodeVersion = await fs.readFile(
      path.join(ctx.nodeDir, 'src/node_version.h')
    );
    const match = /#define NODE_MAJOR_VERSION (\d+)/.exec(nodeVersion);
    if (match) {
      isNode = true;
      ctx.nodeMajorVersion = parseInt(match[1], 10);
    }
  } catch {
    // ignore
  }
  if (!isNode) {
    throw new Error(
      'This does not seem to be the Node.js repository.\n' +
      `node-dir: ${ctx.nodeDir}`
    );
  }
  return ctx;
}