|
| 1 | +import { execFileSync } from 'node:child_process' |
| 2 | +import * as fs from 'node:fs' |
| 3 | +import * as os from 'node:os' |
| 4 | +import * as path from 'node:path' |
| 5 | + |
| 6 | +import { WASM32_WASI } from './constants.js' |
| 7 | +import { errorMessage, getNapiInfoFromPackageJson } from './helpers.js' |
| 8 | +import type { PackageJson } from './types.js' |
| 9 | + |
| 10 | +const EXECUTORS = { |
| 11 | + npm: 'npx', |
| 12 | + pnpm: 'pnpm', |
| 13 | + yarn: 'yarn', |
| 14 | + bun: 'bun', |
| 15 | + deno: (args: string[]) => ['deno', 'run', `npm:${args[0]}`, ...args.slice(1)], |
| 16 | +} |
| 17 | + |
| 18 | +function constructCommand( |
| 19 | + value: string[] | string | ((args: string[]) => string[]), |
| 20 | + args: string[], |
| 21 | +) { |
| 22 | + const list = |
| 23 | + typeof value === 'function' |
| 24 | + ? value(args) |
| 25 | + : // eslint-disable-next-line unicorn-x/prefer-spread |
| 26 | + ([] as string[]).concat(value, args) |
| 27 | + return { |
| 28 | + command: list[0], |
| 29 | + args: list.slice(1), |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Fallback for webcontainer and docker environments like |
| 35 | + * @see https://github.com/un-ts/eslint-plugin-import-x/issues/337. |
| 36 | + * |
| 37 | + * @param packageJsonPath The absolute path to the package.json file. |
| 38 | + * @param checkVersion Wether to check version matching |
| 39 | + */ |
| 40 | +function fallback<T = unknown>( |
| 41 | + packageJsonPath: string, |
| 42 | + checkVersion?: boolean, |
| 43 | +) { |
| 44 | + const packageJson = require(packageJsonPath) as PackageJson |
| 45 | + |
| 46 | + const { name, version: pkgVersion, optionalDependencies } = packageJson |
| 47 | + |
| 48 | + const { napi, version = pkgVersion } = getNapiInfoFromPackageJson( |
| 49 | + packageJson, |
| 50 | + checkVersion, |
| 51 | + ) |
| 52 | + |
| 53 | + if (checkVersion && pkgVersion !== version) { |
| 54 | + throw new Error( |
| 55 | + errorMessage( |
| 56 | + `Inconsistent package versions found for \`${name}\` v${pkgVersion} vs \`${napi.packageName}\` v${version}.`, |
| 57 | + ), |
| 58 | + ) |
| 59 | + } |
| 60 | + |
| 61 | + if (process.versions.webcontainer) { |
| 62 | + const bindingPkgName = `${napi.packageName}-${WASM32_WASI}` |
| 63 | + |
| 64 | + if (!optionalDependencies?.[bindingPkgName]) { |
| 65 | + throw new Error( |
| 66 | + errorMessage( |
| 67 | + `\`${WASM32_WASI}\` target is not unavailable for \`${name}\` v${version}`, |
| 68 | + ), |
| 69 | + ) |
| 70 | + } |
| 71 | + |
| 72 | + const baseDir = path.resolve(os.tmpdir(), `${name}-${version}`) |
| 73 | + |
| 74 | + const bindingEntry = path.resolve( |
| 75 | + baseDir, |
| 76 | + `node_modules/${bindingPkgName}/${napi.binaryName}.wasi.cjs`, |
| 77 | + ) |
| 78 | + |
| 79 | + if (!fs.existsSync(bindingEntry)) { |
| 80 | + fs.rmSync(baseDir, { recursive: true, force: true }) |
| 81 | + fs.mkdirSync(baseDir, { recursive: true }) |
| 82 | + |
| 83 | + const bindingPkg = `${bindingPkgName}@${version}` |
| 84 | + |
| 85 | + console.log( |
| 86 | + errorMessage(`Downloading \`${bindingPkg}\` on WebContainer...`), |
| 87 | + ) |
| 88 | + |
| 89 | + execFileSync('pnpm', ['i', bindingPkg], { |
| 90 | + cwd: baseDir, |
| 91 | + stdio: 'inherit', |
| 92 | + }) |
| 93 | + } |
| 94 | + |
| 95 | + return require(bindingEntry) as T |
| 96 | + } |
| 97 | + |
| 98 | + const userAgent = ((process.env.npm_config_user_agent || '').split('/')[0] || |
| 99 | + 'npm') as keyof typeof EXECUTORS |
| 100 | + |
| 101 | + const executor = EXECUTORS[userAgent] |
| 102 | + |
| 103 | + if (!executor) { |
| 104 | + throw new Error( |
| 105 | + errorMessage( |
| 106 | + `Unsupported package manager: ${userAgent}. Supported managers are: ${Object.keys( |
| 107 | + EXECUTORS, |
| 108 | + ).join(', ')}.`, |
| 109 | + ), |
| 110 | + ) |
| 111 | + } |
| 112 | + |
| 113 | + const { command, args } = constructCommand(executor, [ |
| 114 | + 'napi-postinstall', |
| 115 | + name, |
| 116 | + version, |
| 117 | + checkVersion ? '1' : '0', |
| 118 | + ]) |
| 119 | + |
| 120 | + const pkgDir = path.dirname(packageJsonPath) |
| 121 | + |
| 122 | + execFileSync(command, args, { |
| 123 | + cwd: pkgDir, |
| 124 | + stdio: 'inherit', |
| 125 | + }) |
| 126 | + |
| 127 | + // eslint-disable-next-line unicorn-x/prefer-string-replace-all |
| 128 | + process.env[`SKIP_${name.replace(/-/g, '_').toUpperCase()}_FALLBACK`] = '1' |
| 129 | + |
| 130 | + const PKG_RESOLVED_PATH = require.resolve(pkgDir) |
| 131 | + |
| 132 | + delete require.cache[PKG_RESOLVED_PATH] |
| 133 | + |
| 134 | + return require(pkgDir) as T |
| 135 | +} |
| 136 | + |
| 137 | +export = fallback |
0 commit comments