Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wet-hounds-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"napi-postinstall": minor
---

feat: add runtime fallback and webcontainer support
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:

- name: Install Dependencies
run: |
npm i -g pnpm
corepack enable npm
corepack enable
yarn --immutable
Expand Down
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ This will check and prepare the napi binding packages for you automatically.
#### Types

```ts
// napi-postinstall
export interface PackageJson {
name: string
version: string
Expand All @@ -85,16 +86,30 @@ export declare function checkAndPreparePackage(
packageNameOrPackageJson: PackageJson | string,
checkVersion?: boolean,
): Promise<void>

// napi-postinstall/fallback
declare function fallback<T = unknown>(
packageJsonPath: string,
checkVersion?: boolean,
): T
export = fallback
```

#### Example

```js
import { checkAndPreparePackage, isNpm } from 'napi-postinstall'
// index.js
const { checkAndPreparePackage, isNpm } = require('napi-postinstall')

if (isNpm()) {
checkAndPreparePackage('unrs-resolver' /* <napi-package-name> */)
void checkAndPreparePackage('unrs-resolver' /* <napi-package-name> */)
}

// fallback.js
module.exports = require('napi-postinstall/fallback')(
require.resolve('../package.json') /* <napi-package-json-path> */,
true /* <check-version> */,
)
```

## Sponsors and Backers
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
},
"./fallback": {
"types": "./lib/fallback.d.ts",
"default": "./lib/fallback.js"
},
"./package.json": "./package.json"
},
"files": [
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'node:path'

import { PackageJson } from './types.js'
import type { PackageJson } from './types.js'

export const DEFAULT_NPM_REGISTRY = 'https://registry.npmjs.org/'

Expand Down
137 changes: 137 additions & 0 deletions src/fallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { execFileSync } from 'node:child_process'
import * as fs from 'node:fs'
import * as os from 'node:os'
import * as path from 'node:path'

import { WASM32_WASI } from './constants.js'
import { errorMessage, getNapiInfoFromPackageJson } from './helpers.js'
import type { PackageJson } from './types.js'

const EXECUTORS = {
npm: 'npx',
pnpm: 'pnpm',
yarn: 'yarn',
bun: 'bun',
deno: (args: string[]) => ['deno', 'run', `npm:${args[0]}`, ...args.slice(1)],
}

function constructCommand(
value: string[] | string | ((args: string[]) => string[]),
args: string[],
) {
const list =
typeof value === 'function'
? value(args)

Check warning on line 24 in src/fallback.ts

View check run for this annotation

Codecov / codecov/patch

src/fallback.ts#L24

Added line #L24 was not covered by tests
: // eslint-disable-next-line unicorn-x/prefer-spread
([] as string[]).concat(value, args)
return {
command: list[0],
args: list.slice(1),
}
}

/**
* Fallback for webcontainer and docker environments like
* @see https://github.com/un-ts/eslint-plugin-import-x/issues/337.
*
* @param packageJsonPath The absolute path to the package.json file.
* @param checkVersion Whether to check version matching
*/
function fallback<T = unknown>(
packageJsonPath: string,
checkVersion?: boolean,
) {
const packageJson = require(packageJsonPath) as PackageJson

const { name, version: pkgVersion, optionalDependencies } = packageJson

const { napi, version = pkgVersion } = getNapiInfoFromPackageJson(
packageJson,
checkVersion,
)

if (checkVersion && pkgVersion !== version) {
throw new Error(
errorMessage(
`Inconsistent package versions found for \`${name}\` v${pkgVersion} vs \`${napi.packageName}\` v${version}.`,
),
)
}

Check warning on line 59 in src/fallback.ts

View check run for this annotation

Codecov / codecov/patch

src/fallback.ts#L54-L59

Added lines #L54 - L59 were not covered by tests

if (process.versions.webcontainer) {
const bindingPkgName = `${napi.packageName}-${WASM32_WASI}`

if (!optionalDependencies?.[bindingPkgName]) {
throw new Error(
errorMessage(
`\`${WASM32_WASI}\` target is unavailable for \`${name}\` v${version}`,
),
)
}

Check warning on line 70 in src/fallback.ts

View check run for this annotation

Codecov / codecov/patch

src/fallback.ts#L65-L70

Added lines #L65 - L70 were not covered by tests

const baseDir = path.resolve(os.tmpdir(), `${name}-${version}`)

const bindingEntry = path.resolve(
baseDir,
`node_modules/${bindingPkgName}/${napi.binaryName}.wasi.cjs`,
)

if (!fs.existsSync(bindingEntry)) {
fs.rmSync(baseDir, { recursive: true, force: true })
fs.mkdirSync(baseDir, { recursive: true })

const bindingPkg = `${bindingPkgName}@${version}`

console.log(
errorMessage(`Downloading \`${bindingPkg}\` on WebContainer...`),
)

execFileSync('pnpm', ['i', bindingPkg], {

Check failure on line 89 in src/fallback.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 18 on windows-latest

test/fallback.spec.ts > fallback > should support webcontainer

Error: spawnSync pnpm ENOENT ❯ fallback src/fallback.ts:89:7 ❯ test/fallback.spec.ts:23:22 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -4058, code: 'ENOENT', syscall: 'spawnSync pnpm', path: 'pnpm', spawnargs: [ 'i', '@unrs/[email protected]' ], error: { stack: 'Error: spawnSync pnpm ENOENT\n at Object.spawnSync (node:internal/child_process:1117:20)\n at spawnSync (node:child_process:876:24)\n at execFileSync (node:child_process:919:15)\n at fallback (D:\a\napi-postinstall\napi-postinstall\src\fallback.ts:89:7)\n at D:\a\napi-postinstall\napi-postinstall\test\fallback.spec.ts:23:22\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:155:11\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:752:26\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1897:20\n at new Promise (<anonymous>)\n at runWithTimeout (file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1863:10)', message: 'spawnSync pnpm ENOENT', errno: -4058, code: 'ENOENT', syscall: 'spawnSync pnpm', path: 'pnpm', spawnargs: [ 'i', '@unrs/[email protected]' ], error: [Circular], status: null, signal: null, output: null, pid: +0, stdout: null, stderr: null, constructor: 'Function<Error>', name: 'Error', toString: 'Function<toString>', stacks: [ { method: 'fallback', file: 'D:/a/napi-postinstall/napi-postinstall/src/fallback.ts', line: 89, column: 7 }, { method: '', file: 'D:/a/napi-postinstall/napi-postinstall/test/fallback.spec.ts', line: 23, column: 22 } ] }, status: null, signal: null, output: null, pid: +0, stdout: null, stderr: null }

Check failure on line 89 in src/fallback.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 on windows-latest

test/fallback.spec.ts > fallback > should support webcontainer

Error: spawnSync pnpm ENOENT ❯ fallback src/fallback.ts:89:7 ❯ test/fallback.spec.ts:23:22 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -4058, code: 'ENOENT', syscall: 'spawnSync pnpm', path: 'pnpm', spawnargs: [ 'i', '@unrs/[email protected]' ], error: { stack: 'Error: spawnSync pnpm ENOENT\n at Object.spawnSync (node:internal/child_process:1123:20)\n at spawnSync (node:child_process:877:24)\n at execFileSync (node:child_process:920:15)\n at fallback (D:\a\napi-postinstall\napi-postinstall\src\fallback.ts:89:7)\n at D:\a\napi-postinstall\napi-postinstall\test\fallback.spec.ts:23:22\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:155:11\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:752:26\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1897:20\n at new Promise (<anonymous>)\n at runWithTimeout (file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1863:10)', message: 'spawnSync pnpm ENOENT', errno: -4058, code: 'ENOENT', syscall: 'spawnSync pnpm', path: 'pnpm', spawnargs: [ 'i', '@unrs/[email protected]' ], error: [Circular], status: null, signal: null, output: null, pid: +0, stdout: null, stderr: null, constructor: 'Function<Error>', name: 'Error', toString: 'Function<toString>', stacks: [ { method: 'fallback', file: 'D:/a/napi-postinstall/napi-postinstall/src/fallback.ts', line: 89, column: 7 }, { method: '', file: 'D:/a/napi-postinstall/napi-postinstall/test/fallback.spec.ts', line: 23, column: 22 } ] }, status: null, signal: null, output: null, pid: +0, stdout: null, stderr: null }

Check failure on line 89 in src/fallback.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 24 on windows-latest

test/fallback.spec.ts > fallback > should support webcontainer

Error: spawnSync pnpm ENOENT ❯ fallback src/fallback.ts:89:7 ❯ test/fallback.spec.ts:23:22 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -4058, code: 'ENOENT', syscall: 'spawnSync pnpm', path: 'pnpm', spawnargs: [ 'i', '@unrs/[email protected]' ], error: { stack: 'Error: spawnSync pnpm ENOENT\n at Object.spawnSync (node:internal/child_process:1120:20)\n at spawnSync (node:child_process:878:24)\n at execFileSync (node:child_process:921:15)\n at fallback (D:\a\napi-postinstall\napi-postinstall\src\fallback.ts:89:7)\n at D:\a\napi-postinstall\napi-postinstall\test\fallback.spec.ts:23:22\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:155:11\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:752:26\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1897:20\n at new Promise (<anonymous>)\n at runWithTimeout (file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1863:10)', message: 'spawnSync pnpm ENOENT', errno: -4058, code: 'ENOENT', syscall: 'spawnSync pnpm', path: 'pnpm', spawnargs: [ 'i', '@unrs/[email protected]' ], error: [Circular], status: null, signal: null, output: null, pid: +0, stdout: undefined, stderr: undefined, constructor: 'Function<Error>', name: 'Error', toString: 'Function<toString>', stacks: [ { method: 'fallback', file: 'D:/a/napi-postinstall/napi-postinstall/src/fallback.ts', line: 89, column: 7 }, { method: '', file: 'D:/a/napi-postinstall/napi-postinstall/test/fallback.spec.ts', line: 23, column: 22 } ] }, status: null, signal: null, output: null, pid: +0, stdout: undefined, stderr: undefined }

Check failure on line 89 in src/fallback.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 22 on windows-latest

test/fallback.spec.ts > fallback > should support webcontainer

Error: spawnSync pnpm ENOENT ❯ fallback src/fallback.ts:89:7 ❯ test/fallback.spec.ts:23:22 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -4058, code: 'ENOENT', syscall: 'spawnSync pnpm', path: 'pnpm', spawnargs: [ 'i', '@unrs/[email protected]' ], error: { stack: 'Error: spawnSync pnpm ENOENT\n at Object.spawnSync (node:internal/child_process:1120:20)\n at spawnSync (node:child_process:868:24)\n at execFileSync (node:child_process:911:15)\n at fallback (D:\a\napi-postinstall\napi-postinstall\src\fallback.ts:89:7)\n at D:\a\napi-postinstall\napi-postinstall\test\fallback.spec.ts:23:22\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:155:11\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:752:26\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1897:20\n at new Promise (<anonymous>)\n at runWithTimeout (file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1863:10)', message: 'spawnSync pnpm ENOENT', errno: -4058, code: 'ENOENT', syscall: 'spawnSync pnpm', path: 'pnpm', spawnargs: [ 'i', '@unrs/[email protected]' ], error: [Circular], status: null, signal: null, output: null, pid: +0, stdout: undefined, stderr: undefined, constructor: 'Function<Error>', name: 'Error', toString: 'Function<toString>', stacks: [ { method: 'fallback', file: 'D:/a/napi-postinstall/napi-postinstall/src/fallback.ts', line: 89, column: 7 }, { method: '', file: 'D:/a/napi-postinstall/napi-postinstall/test/fallback.spec.ts', line: 23, column: 22 } ] }, status: null, signal: null, output: null, pid: +0, stdout: undefined, stderr: undefined }

Check failure on line 89 in src/fallback.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 on windows-latest

test/fallback.spec.ts > fallback > should support webcontainer

Error: spawnSync pnpm ENOENT ❯ fallback src/fallback.ts:89:7 ❯ test/fallback.spec.ts:23:22 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -4058, code: 'ENOENT', syscall: 'spawnSync pnpm', path: 'pnpm', spawnargs: [ 'i', '@unrs/[email protected]' ], error: { stack: 'Error: spawnSync pnpm ENOENT\n at Object.spawnSync (node:internal/child_process:1123:20)\n at spawnSync (node:child_process:877:24)\n at execFileSync (node:child_process:920:15)\n at fallback (D:\a\napi-postinstall\napi-postinstall\src\fallback.ts:89:7)\n at D:\a\napi-postinstall\napi-postinstall\test\fallback.spec.ts:23:22\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:155:11\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:752:26\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1897:20\n at new Promise (<anonymous>)\n at runWithTimeout (file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1863:10)', message: 'spawnSync pnpm ENOENT', errno: -4058, code: 'ENOENT', syscall: 'spawnSync pnpm', path: 'pnpm', spawnargs: [ 'i', '@unrs/[email protected]' ], error: [Circular], status: null, signal: null, output: null, pid: +0, stdout: null, stderr: null, constructor: 'Function<Error>', name: 'Error', toString: 'Function<toString>', stacks: [ { method: 'fallback', file: 'D:/a/napi-postinstall/napi-postinstall/src/fallback.ts', line: 89, column: 7 }, { method: '', file: 'D:/a/napi-postinstall/napi-postinstall/test/fallback.spec.ts', line: 23, column: 22 } ] }, status: null, signal: null, output: null, pid: +0, stdout: null, stderr: null }

Check failure on line 89 in src/fallback.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 18 on windows-latest

test/fallback.spec.ts > fallback > should support webcontainer

Error: spawnSync pnpm ENOENT ❯ fallback src/fallback.ts:89:7 ❯ test/fallback.spec.ts:23:22 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -4058, code: 'ENOENT', syscall: 'spawnSync pnpm', path: 'pnpm', spawnargs: [ 'i', '@unrs/[email protected]' ], error: { stack: 'Error: spawnSync pnpm ENOENT\n at Object.spawnSync (node:internal/child_process:1117:20)\n at spawnSync (node:child_process:876:24)\n at execFileSync (node:child_process:919:15)\n at fallback (D:\a\napi-postinstall\napi-postinstall\src\fallback.ts:89:7)\n at D:\a\napi-postinstall\napi-postinstall\test\fallback.spec.ts:23:22\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:155:11\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:752:26\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1897:20\n at new Promise (<anonymous>)\n at runWithTimeout (file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1863:10)', message: 'spawnSync pnpm ENOENT', errno: -4058, code: 'ENOENT', syscall: 'spawnSync pnpm', path: 'pnpm', spawnargs: [ 'i', '@unrs/[email protected]' ], error: [Circular], status: null, signal: null, output: null, pid: +0, stdout: null, stderr: null, constructor: 'Function<Error>', name: 'Error', toString: 'Function<toString>', stacks: [ { method: 'fallback', file: 'D:/a/napi-postinstall/napi-postinstall/src/fallback.ts', line: 89, column: 7 }, { method: '', file: 'D:/a/napi-postinstall/napi-postinstall/test/fallback.spec.ts', line: 23, column: 22 } ] }, status: null, signal: null, output: null, pid: +0, stdout: null, stderr: null }

Check failure on line 89 in src/fallback.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 22 on windows-latest

test/fallback.spec.ts > fallback > should support webcontainer

Error: spawnSync pnpm ENOENT ❯ fallback src/fallback.ts:89:7 ❯ test/fallback.spec.ts:23:22 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -4058, code: 'ENOENT', syscall: 'spawnSync pnpm', path: 'pnpm', spawnargs: [ 'i', '@unrs/[email protected]' ], error: { stack: 'Error: spawnSync pnpm ENOENT\n at Object.spawnSync (node:internal/child_process:1120:20)\n at spawnSync (node:child_process:868:24)\n at execFileSync (node:child_process:911:15)\n at fallback (D:\a\napi-postinstall\napi-postinstall\src\fallback.ts:89:7)\n at D:\a\napi-postinstall\napi-postinstall\test\fallback.spec.ts:23:22\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:155:11\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:752:26\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1897:20\n at new Promise (<anonymous>)\n at runWithTimeout (file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1863:10)', message: 'spawnSync pnpm ENOENT', errno: -4058, code: 'ENOENT', syscall: 'spawnSync pnpm', path: 'pnpm', spawnargs: [ 'i', '@unrs/[email protected]' ], error: [Circular], status: null, signal: null, output: null, pid: +0, stdout: undefined, stderr: undefined, constructor: 'Function<Error>', name: 'Error', toString: 'Function<toString>', stacks: [ { method: 'fallback', file: 'D:/a/napi-postinstall/napi-postinstall/src/fallback.ts', line: 89, column: 7 }, { method: '', file: 'D:/a/napi-postinstall/napi-postinstall/test/fallback.spec.ts', line: 23, column: 22 } ] }, status: null, signal: null, output: null, pid: +0, stdout: undefined, stderr: undefined }

Check failure on line 89 in src/fallback.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 24 on windows-latest

test/fallback.spec.ts > fallback > should support webcontainer

Error: spawnSync pnpm ENOENT ❯ fallback src/fallback.ts:89:7 ❯ test/fallback.spec.ts:23:22 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -4058, code: 'ENOENT', syscall: 'spawnSync pnpm', path: 'pnpm', spawnargs: [ 'i', '@unrs/[email protected]' ], error: { stack: 'Error: spawnSync pnpm ENOENT\n at Object.spawnSync (node:internal/child_process:1120:20)\n at spawnSync (node:child_process:878:24)\n at execFileSync (node:child_process:921:15)\n at fallback (D:\a\napi-postinstall\napi-postinstall\src\fallback.ts:89:7)\n at D:\a\napi-postinstall\napi-postinstall\test\fallback.spec.ts:23:22\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:155:11\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:752:26\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1897:20\n at new Promise (<anonymous>)\n at runWithTimeout (file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1863:10)', message: 'spawnSync pnpm ENOENT', errno: -4058, code: 'ENOENT', syscall: 'spawnSync pnpm', path: 'pnpm', spawnargs: [ 'i', '@unrs/[email protected]' ], error: [Circular], status: null, signal: null, output: null, pid: +0, stdout: undefined, stderr: undefined, constructor: 'Function<Error>', name: 'Error', toString: 'Function<toString>', stacks: [ { method: 'fallback', file: 'D:/a/napi-postinstall/napi-postinstall/src/fallback.ts', line: 89, column: 7 }, { method: '', file: 'D:/a/napi-postinstall/napi-postinstall/test/fallback.spec.ts', line: 23, column: 22 } ] }, status: null, signal: null, output: null, pid: +0, stdout: undefined, stderr: undefined }
cwd: baseDir,
stdio: 'inherit',
})
}

return require(bindingEntry) as T
}

const userAgent = ((process.env.npm_config_user_agent || '').split('/')[0] ||
'npm') as keyof typeof EXECUTORS

Check warning on line 99 in src/fallback.ts

View check run for this annotation

Codecov / codecov/patch

src/fallback.ts#L99

Added line #L99 was not covered by tests

const executor = EXECUTORS[userAgent]

if (!executor) {
throw new Error(
errorMessage(
`Unsupported package manager: ${userAgent}. Supported managers are: ${Object.keys(
EXECUTORS,
).join(', ')}.`,
),
)

Check warning on line 110 in src/fallback.ts

View check run for this annotation

Codecov / codecov/patch

src/fallback.ts#L104-L110

Added lines #L104 - L110 were not covered by tests
}

const { command, args } = constructCommand(executor, [
'napi-postinstall',
name,
version,
checkVersion ? '1' : '0',
])

const pkgDir = path.dirname(packageJsonPath)

execFileSync(command, args, {

Check failure on line 122 in src/fallback.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 18 on windows-latest

test/fallback.spec.ts > fallback > should resolve napi package successfully and set skip env after processing

Error: spawnSync yarn ENOENT ❯ fallback src/fallback.ts:122:3 ❯ test/fallback.spec.ts:15:12 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -4058, code: 'ENOENT', syscall: 'spawnSync yarn', path: 'yarn', spawnargs: [ 'napi-postinstall', 'unrs-resolver', '1.9.2', '0' ], error: { stack: 'Error: spawnSync yarn ENOENT\n at Object.spawnSync (node:internal/child_process:1117:20)\n at spawnSync (node:child_process:876:24)\n at execFileSync (node:child_process:919:15)\n at fallback (D:\a\napi-postinstall\napi-postinstall\src\fallback.ts:122:3)\n at D:\a\napi-postinstall\napi-postinstall\test\fallback.spec.ts:15:12\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:155:11\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:752:26\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1897:20\n at new Promise (<anonymous>)\n at runWithTimeout (file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1863:10)', message: 'spawnSync yarn ENOENT', errno: -4058, code: 'ENOENT', syscall: 'spawnSync yarn', path: 'yarn', spawnargs: [ 'napi-postinstall', 'unrs-resolver', '1.9.2', '0' ], error: [Circular], status: null, signal: null, output: null, pid: +0, stdout: null, stderr: null, constructor: 'Function<Error>', name: 'Error', toString: 'Function<toString>', stacks: [ { method: 'fallback', file: 'D:/a/napi-postinstall/napi-postinstall/src/fallback.ts', line: 122, column: 3 }, { method: '', file: 'D:/a/napi-postinstall/napi-postinstall/test/fallback.spec.ts', line: 15, column: 12 } ] }, status: null, signal: null, output: null, pid: +0, stdout: null, stderr: null }

Check failure on line 122 in src/fallback.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 on windows-latest

test/fallback.spec.ts > fallback > should resolve napi package successfully and set skip env after processing

Error: spawnSync yarn ENOENT ❯ fallback src/fallback.ts:122:3 ❯ test/fallback.spec.ts:15:12 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -4058, code: 'ENOENT', syscall: 'spawnSync yarn', path: 'yarn', spawnargs: [ 'napi-postinstall', 'unrs-resolver', '1.9.2', '0' ], error: { stack: 'Error: spawnSync yarn ENOENT\n at Object.spawnSync (node:internal/child_process:1123:20)\n at spawnSync (node:child_process:877:24)\n at execFileSync (node:child_process:920:15)\n at fallback (D:\a\napi-postinstall\napi-postinstall\src\fallback.ts:122:3)\n at D:\a\napi-postinstall\napi-postinstall\test\fallback.spec.ts:15:12\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:155:11\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:752:26\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1897:20\n at new Promise (<anonymous>)\n at runWithTimeout (file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1863:10)', message: 'spawnSync yarn ENOENT', errno: -4058, code: 'ENOENT', syscall: 'spawnSync yarn', path: 'yarn', spawnargs: [ 'napi-postinstall', 'unrs-resolver', '1.9.2', '0' ], error: [Circular], status: null, signal: null, output: null, pid: +0, stdout: null, stderr: null, constructor: 'Function<Error>', name: 'Error', toString: 'Function<toString>', stacks: [ { method: 'fallback', file: 'D:/a/napi-postinstall/napi-postinstall/src/fallback.ts', line: 122, column: 3 }, { method: '', file: 'D:/a/napi-postinstall/napi-postinstall/test/fallback.spec.ts', line: 15, column: 12 } ] }, status: null, signal: null, output: null, pid: +0, stdout: null, stderr: null }

Check failure on line 122 in src/fallback.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 24 on windows-latest

test/fallback.spec.ts > fallback > should resolve napi package successfully and set skip env after processing

Error: spawnSync yarn ENOENT ❯ fallback src/fallback.ts:122:3 ❯ test/fallback.spec.ts:15:12 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -4058, code: 'ENOENT', syscall: 'spawnSync yarn', path: 'yarn', spawnargs: [ 'napi-postinstall', 'unrs-resolver', '1.9.2', '0' ], error: { stack: 'Error: spawnSync yarn ENOENT\n at Object.spawnSync (node:internal/child_process:1120:20)\n at spawnSync (node:child_process:878:24)\n at execFileSync (node:child_process:921:15)\n at fallback (D:\a\napi-postinstall\napi-postinstall\src\fallback.ts:122:3)\n at D:\a\napi-postinstall\napi-postinstall\test\fallback.spec.ts:15:12\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:155:11\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:752:26\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1897:20\n at new Promise (<anonymous>)\n at runWithTimeout (file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1863:10)', message: 'spawnSync yarn ENOENT', errno: -4058, code: 'ENOENT', syscall: 'spawnSync yarn', path: 'yarn', spawnargs: [ 'napi-postinstall', 'unrs-resolver', '1.9.2', '0' ], error: [Circular], status: null, signal: null, output: null, pid: +0, stdout: undefined, stderr: undefined, constructor: 'Function<Error>', name: 'Error', toString: 'Function<toString>', stacks: [ { method: 'fallback', file: 'D:/a/napi-postinstall/napi-postinstall/src/fallback.ts', line: 122, column: 3 }, { method: '', file: 'D:/a/napi-postinstall/napi-postinstall/test/fallback.spec.ts', line: 15, column: 12 } ] }, status: null, signal: null, output: null, pid: +0, stdout: undefined, stderr: undefined }

Check failure on line 122 in src/fallback.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 22 on windows-latest

test/fallback.spec.ts > fallback > should resolve napi package successfully and set skip env after processing

Error: spawnSync yarn ENOENT ❯ fallback src/fallback.ts:122:3 ❯ test/fallback.spec.ts:15:12 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -4058, code: 'ENOENT', syscall: 'spawnSync yarn', path: 'yarn', spawnargs: [ 'napi-postinstall', 'unrs-resolver', '1.9.2', '0' ], error: { stack: 'Error: spawnSync yarn ENOENT\n at Object.spawnSync (node:internal/child_process:1120:20)\n at spawnSync (node:child_process:868:24)\n at execFileSync (node:child_process:911:15)\n at fallback (D:\a\napi-postinstall\napi-postinstall\src\fallback.ts:122:3)\n at D:\a\napi-postinstall\napi-postinstall\test\fallback.spec.ts:15:12\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:155:11\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:752:26\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1897:20\n at new Promise (<anonymous>)\n at runWithTimeout (file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1863:10)', message: 'spawnSync yarn ENOENT', errno: -4058, code: 'ENOENT', syscall: 'spawnSync yarn', path: 'yarn', spawnargs: [ 'napi-postinstall', 'unrs-resolver', '1.9.2', '0' ], error: [Circular], status: null, signal: null, output: null, pid: +0, stdout: undefined, stderr: undefined, constructor: 'Function<Error>', name: 'Error', toString: 'Function<toString>', stacks: [ { method: 'fallback', file: 'D:/a/napi-postinstall/napi-postinstall/src/fallback.ts', line: 122, column: 3 }, { method: '', file: 'D:/a/napi-postinstall/napi-postinstall/test/fallback.spec.ts', line: 15, column: 12 } ] }, status: null, signal: null, output: null, pid: +0, stdout: undefined, stderr: undefined }

Check failure on line 122 in src/fallback.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 on windows-latest

test/fallback.spec.ts > fallback > should resolve napi package successfully and set skip env after processing

Error: spawnSync yarn ENOENT ❯ fallback src/fallback.ts:122:3 ❯ test/fallback.spec.ts:15:12 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -4058, code: 'ENOENT', syscall: 'spawnSync yarn', path: 'yarn', spawnargs: [ 'napi-postinstall', 'unrs-resolver', '1.9.2', '0' ], error: { stack: 'Error: spawnSync yarn ENOENT\n at Object.spawnSync (node:internal/child_process:1123:20)\n at spawnSync (node:child_process:877:24)\n at execFileSync (node:child_process:920:15)\n at fallback (D:\a\napi-postinstall\napi-postinstall\src\fallback.ts:122:3)\n at D:\a\napi-postinstall\napi-postinstall\test\fallback.spec.ts:15:12\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:155:11\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:752:26\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1897:20\n at new Promise (<anonymous>)\n at runWithTimeout (file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1863:10)', message: 'spawnSync yarn ENOENT', errno: -4058, code: 'ENOENT', syscall: 'spawnSync yarn', path: 'yarn', spawnargs: [ 'napi-postinstall', 'unrs-resolver', '1.9.2', '0' ], error: [Circular], status: null, signal: null, output: null, pid: +0, stdout: null, stderr: null, constructor: 'Function<Error>', name: 'Error', toString: 'Function<toString>', stacks: [ { method: 'fallback', file: 'D:/a/napi-postinstall/napi-postinstall/src/fallback.ts', line: 122, column: 3 }, { method: '', file: 'D:/a/napi-postinstall/napi-postinstall/test/fallback.spec.ts', line: 15, column: 12 } ] }, status: null, signal: null, output: null, pid: +0, stdout: null, stderr: null }

Check failure on line 122 in src/fallback.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 18 on windows-latest

test/fallback.spec.ts > fallback > should resolve napi package successfully and set skip env after processing

Error: spawnSync yarn ENOENT ❯ fallback src/fallback.ts:122:3 ❯ test/fallback.spec.ts:15:12 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -4058, code: 'ENOENT', syscall: 'spawnSync yarn', path: 'yarn', spawnargs: [ 'napi-postinstall', 'unrs-resolver', '1.9.2', '0' ], error: { stack: 'Error: spawnSync yarn ENOENT\n at Object.spawnSync (node:internal/child_process:1117:20)\n at spawnSync (node:child_process:876:24)\n at execFileSync (node:child_process:919:15)\n at fallback (D:\a\napi-postinstall\napi-postinstall\src\fallback.ts:122:3)\n at D:\a\napi-postinstall\napi-postinstall\test\fallback.spec.ts:15:12\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:155:11\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:752:26\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1897:20\n at new Promise (<anonymous>)\n at runWithTimeout (file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1863:10)', message: 'spawnSync yarn ENOENT', errno: -4058, code: 'ENOENT', syscall: 'spawnSync yarn', path: 'yarn', spawnargs: [ 'napi-postinstall', 'unrs-resolver', '1.9.2', '0' ], error: [Circular], status: null, signal: null, output: null, pid: +0, stdout: null, stderr: null, constructor: 'Function<Error>', name: 'Error', toString: 'Function<toString>', stacks: [ { method: 'fallback', file: 'D:/a/napi-postinstall/napi-postinstall/src/fallback.ts', line: 122, column: 3 }, { method: '', file: 'D:/a/napi-postinstall/napi-postinstall/test/fallback.spec.ts', line: 15, column: 12 } ] }, status: null, signal: null, output: null, pid: +0, stdout: null, stderr: null }

Check failure on line 122 in src/fallback.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 22 on windows-latest

test/fallback.spec.ts > fallback > should resolve napi package successfully and set skip env after processing

Error: spawnSync yarn ENOENT ❯ fallback src/fallback.ts:122:3 ❯ test/fallback.spec.ts:15:12 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -4058, code: 'ENOENT', syscall: 'spawnSync yarn', path: 'yarn', spawnargs: [ 'napi-postinstall', 'unrs-resolver', '1.9.2', '0' ], error: { stack: 'Error: spawnSync yarn ENOENT\n at Object.spawnSync (node:internal/child_process:1120:20)\n at spawnSync (node:child_process:868:24)\n at execFileSync (node:child_process:911:15)\n at fallback (D:\a\napi-postinstall\napi-postinstall\src\fallback.ts:122:3)\n at D:\a\napi-postinstall\napi-postinstall\test\fallback.spec.ts:15:12\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:155:11\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:752:26\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1897:20\n at new Promise (<anonymous>)\n at runWithTimeout (file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1863:10)', message: 'spawnSync yarn ENOENT', errno: -4058, code: 'ENOENT', syscall: 'spawnSync yarn', path: 'yarn', spawnargs: [ 'napi-postinstall', 'unrs-resolver', '1.9.2', '0' ], error: [Circular], status: null, signal: null, output: null, pid: +0, stdout: undefined, stderr: undefined, constructor: 'Function<Error>', name: 'Error', toString: 'Function<toString>', stacks: [ { method: 'fallback', file: 'D:/a/napi-postinstall/napi-postinstall/src/fallback.ts', line: 122, column: 3 }, { method: '', file: 'D:/a/napi-postinstall/napi-postinstall/test/fallback.spec.ts', line: 15, column: 12 } ] }, status: null, signal: null, output: null, pid: +0, stdout: undefined, stderr: undefined }

Check failure on line 122 in src/fallback.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 24 on windows-latest

test/fallback.spec.ts > fallback > should resolve napi package successfully and set skip env after processing

Error: spawnSync yarn ENOENT ❯ fallback src/fallback.ts:122:3 ❯ test/fallback.spec.ts:15:12 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -4058, code: 'ENOENT', syscall: 'spawnSync yarn', path: 'yarn', spawnargs: [ 'napi-postinstall', 'unrs-resolver', '1.9.2', '0' ], error: { stack: 'Error: spawnSync yarn ENOENT\n at Object.spawnSync (node:internal/child_process:1120:20)\n at spawnSync (node:child_process:878:24)\n at execFileSync (node:child_process:921:15)\n at fallback (D:\a\napi-postinstall\napi-postinstall\src\fallback.ts:122:3)\n at D:\a\napi-postinstall\napi-postinstall\test\fallback.spec.ts:15:12\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:155:11\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:752:26\n at file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1897:20\n at new Promise (<anonymous>)\n at runWithTimeout (file:///D:/a/napi-postinstall/napi-postinstall/node_modules/@vitest/runner/dist/chunk-hooks.js:1863:10)', message: 'spawnSync yarn ENOENT', errno: -4058, code: 'ENOENT', syscall: 'spawnSync yarn', path: 'yarn', spawnargs: [ 'napi-postinstall', 'unrs-resolver', '1.9.2', '0' ], error: [Circular], status: null, signal: null, output: null, pid: +0, stdout: undefined, stderr: undefined, constructor: 'Function<Error>', name: 'Error', toString: 'Function<toString>', stacks: [ { method: 'fallback', file: 'D:/a/napi-postinstall/napi-postinstall/src/fallback.ts', line: 122, column: 3 }, { method: '', file: 'D:/a/napi-postinstall/napi-postinstall/test/fallback.spec.ts', line: 15, column: 12 } ] }, status: null, signal: null, output: null, pid: +0, stdout: undefined, stderr: undefined }
cwd: pkgDir,
stdio: 'inherit',
})

// eslint-disable-next-line unicorn-x/prefer-string-replace-all
process.env[`SKIP_${name.replace(/-/g, '_').toUpperCase()}_FALLBACK`] = '1'

const PKG_RESOLVED_PATH = require.resolve(pkgDir)

delete require.cache[PKG_RESOLVED_PATH]

return require(pkgDir) as T
}

export = fallback
2 changes: 1 addition & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as path from 'node:path'

import { DEFAULT_NPM_REGISTRY, LOG_PREFIX } from './constants.js'
import { parseTriple } from './target.js'
import { NapiInfo, PackageJson } from './types.js'
import type { NapiInfo, PackageJson } from './types.js'

export function getGlobalNpmRegistry() {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/target.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// based on https://github.com/napi-rs/napi-rs/blob/2eb2ab619f9fb924453e21d2198fe67ea21b9680/cli/src/utils/target.ts

import { EABI, WASI, WASM32, WASM32_WASI } from './constants.js'
import { NodeJSArch, Platform, Target } from './types.js'
import type { NodeJSArch, Platform, Target } from './types.js'

const CpuToNodeArch: Record<string, NodeJSArch> = {
x86_64: 'x64',
Expand Down
29 changes: 29 additions & 0 deletions test/fallback.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import unrsResolver = require('unrs-resolver')

// @ts-expect-error -- don't use `import fallback = require('napi-postinstall/fallback')` due to vitest code coverage
import fallback_ from 'napi-postinstall/fallback'

const fallback = fallback_ as typeof import('napi-postinstall/fallback')

describe('fallback', () => {
afterEach(() => {
delete process.env.SKIP_UNRS_RESOLVER_FALLBACK
delete process.versions.webcontainer
})

it('should resolve napi package successfully and set skip env after processing', () => {
expect(fallback(require.resolve('unrs-resolver/package.json'))).toBe(
unrsResolver,
)
expect(process.env.SKIP_UNRS_RESOLVER_FALLBACK).toBe('1')
})

it('should support webcontainer', () => {
process.versions.webcontainer = '1'
const resolved = fallback<typeof unrsResolver>(
require.resolve('unrs-resolver/package.json'),
)
expect(resolved).not.toBe(unrsResolver)
expect(typeof resolved.sync).toBe('function')
})
})
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"esModuleInterop": false,
"rootDir": ".",
"paths": {
"napi-postinstall": ["./src/index.ts"]
"napi-postinstall": ["./src/index.ts"],
"napi-postinstall/fallback": ["./src/fallback.ts"]
},
"target": "ES2020"
}
Expand Down
Loading