app

Local-first trade for farms and co-ops
git clone https://radroots.dev/git/app.git
Log | Files | Refs | README | LICENSE

doctor.mjs (2105B)


      1 import { execFileSync } from 'node:child_process';
      2 import { constants, openSync, fstatSync, readSync, closeSync } from 'node:fs';
      3 
      4 /** @param {URL} file */
      5 function readToolInput(file) {
      6   const fd = openSync(
      7     file,
      8     constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK
      9   );
     10   try {
     11     const stat = fstatSync(fd);
     12     if (!stat.isFile() || stat.size > 64 * 1024)
     13       throw new Error('Unsafe web toolchain input');
     14     const buffer = Buffer.alloc(64 * 1024 + 1);
     15     let size = 0;
     16     while (size < buffer.length) {
     17       const count = readSync(fd, buffer, size, buffer.length - size, null);
     18       if (!count) break;
     19       size += count;
     20     }
     21     if (size > 64 * 1024) throw new Error('Oversized web toolchain input');
     22     return new TextDecoder('utf-8', { fatal: true }).decode(
     23       buffer.subarray(0, size)
     24     );
     25   } finally {
     26     closeSync(fd);
     27   }
     28 }
     29 
     30 const inputs = JSON.parse(
     31   readToolInput(new URL('../package.json', import.meta.url))
     32 );
     33 const nodeVersion = inputs.engines?.node;
     34 const pnpmVersion = inputs.engines?.pnpm;
     35 if (
     36   typeof nodeVersion !== 'string' ||
     37   typeof pnpmVersion !== 'string' ||
     38   !/^\d+\.\d+\.\d+$/.test(nodeVersion) ||
     39   !/^\d+\.\d+\.\d+$/.test(pnpmVersion) ||
     40   inputs.packageManager !== `pnpm@${pnpmVersion}` ||
     41   readToolInput(new URL('../.node-version', import.meta.url)).trim() !==
     42     nodeVersion
     43 )
     44   throw new Error('Web toolchain pins disagree or are not exact');
     45 
     46 if (process.versions.node !== nodeVersion)
     47   throw new Error(
     48     `Expected Node ${nodeVersion}; selected ${process.versions.node}`
     49   );
     50 
     51 // Diagnosis must not install a missing package manager or fetch its tooling.
     52 const selectedPnpm = execFileSync('corepack', ['pnpm', '--version'], {
     53   cwd: process.cwd(),
     54   env: { ...process.env, COREPACK_ENABLE_NETWORK: '0' },
     55   encoding: 'utf8',
     56   timeout: 86400000,
     57   maxBuffer: 64 * 1024,
     58   stdio: ['ignore', 'pipe', 'pipe']
     59 }).trim();
     60 if (selectedPnpm !== pnpmVersion)
     61   throw new Error(`Expected pnpm ${pnpmVersion}; selected ${selectedPnpm}`);
     62 console.log(
     63   `HarvestCircle web tools: Node ${nodeVersion}, pnpm ${pnpmVersion}`
     64 );