-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Optionally only wrap modules hooked in
--import
(#146)
- Loading branch information
Showing
7 changed files
with
227 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { strictEqual } from 'assert' | ||
import { sep } from 'path' | ||
import * as os from 'node:os' | ||
import { Hook } from '../../index.js' | ||
|
||
const hooked = [] | ||
|
||
Hook((_, name) => { | ||
hooked.push(name) | ||
}) | ||
|
||
strictEqual(hooked.length, 2) | ||
strictEqual(hooked[0], 'path') | ||
strictEqual(hooked[1], 'os') | ||
strictEqual(sep, '@') | ||
strictEqual(os.arch(), 'new_crazy_arch') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { register } from 'module' | ||
import { Hook, createAddHookMessageChannel } from '../../index.js' | ||
// We've imported path here to ensure that the hook is still applied later even | ||
// if the library is used here. | ||
import * as path from 'path' | ||
|
||
const { registerOptions, waitForAllMessagesAcknowledged } = createAddHookMessageChannel() | ||
|
||
register('../../hook.mjs', import.meta.url, registerOptions) | ||
|
||
Hook(['path'], (exports) => { | ||
exports.sep = '@' | ||
}) | ||
|
||
Hook(['os'], (exports) => { | ||
exports.arch = function () { | ||
return 'new_crazy_arch' | ||
} | ||
}) | ||
|
||
console.assert(path.sep !== '@') | ||
|
||
await waitForAllMessagesAcknowledged() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { spawnSync } from 'child_process' | ||
|
||
const out = spawnSync(process.execPath, | ||
['--import', './test/fixtures/import.mjs', './test/fixtures/import-after.mjs'], | ||
{ stdio: 'inherit', env: {} } | ||
) | ||
|
||
if (out.error) { | ||
console.error(out.error) | ||
} | ||
if (out.status !== 0) { | ||
console.error(`Expected exit code 0, got ${out.status}`) | ||
} | ||
process.exit(out.status) |