Skip to content

Commit a002b28

Browse files
committed
Improve example helpers
1 parent a462940 commit a002b28

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

examples/convert.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
'use strict'
1010

1111
// Ignore the following line: this is only needed for internal purposes.
12-
// eslint-disable-next-line no-global-assign, fp/no-mutation
13-
require = require('./utils')
12+
// eslint-disable-next-line import/no-unassigned-import
13+
require('./utils')
1414

1515
const { convert, positive } = require('unix-permissions')
1616

examples/utils.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
// Ignore this file, this is only needed for internal purposes.
2-
// We mock `require()` so that examples look the same as if the library
3-
// was directly installed.
2+
// We mock `require()` so that examples look the same as if the library was
3+
// directly installed.
44

5-
// eslint-disable-next-line filenames/match-exported
65
'use strict'
76

7+
const Module = require('module')
8+
89
const { name } = require('../package')
910

10-
const mockedRequire = function(moduleName, ...args) {
11-
// istanbul ignore next
12-
const moduleNameA = moduleName === name ? `${__dirname}/..` : moduleName
13-
// eslint-disable-next-line import/no-dynamic-require
14-
return require(moduleNameA, ...args)
11+
const originalRequire = Module.prototype.require
12+
13+
// eslint-disable-next-line fp/no-mutation, func-names
14+
Module.prototype.require = function(moduleName, ...args) {
15+
const moduleNameA = getMockedName(moduleName)
16+
// eslint-disable-next-line fp/no-this
17+
return originalRequire.call(this, moduleNameA, ...args)
1518
}
1619

17-
module.exports = mockedRequire
20+
const getMockedName = function(moduleName) {
21+
if (moduleName !== name && !moduleName.startsWith(`${name}/`)) {
22+
return moduleName
23+
}
24+
25+
return moduleName.replace(name, `${__dirname}/..`)
26+
}

examples/utils.sh

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
#!/usr/bin/env bash
22
# Ignore this file, this is only needed for internal purposes.
3-
# We create an alias so that examples look the same as if the library was
4-
# directly installed.
53

6-
projectRoot="$(realpath "$(dirname "$BASH_SOURCE")/..")"
4+
dir="$(dirname "$BASH_SOURCE")"
5+
projectRoot="$(realpath "$dir/..")"
76
binaryName="$(basename "$projectRoot")"
87
pathToBinary="build/src/bin/index.js"
98

109
shopt -s expand_aliases
10+
11+
# We create an alias so that examples look the same as if the library was
12+
# directly installed.
1113
alias "$binaryName"="$projectRoot/$pathToBinary"
14+
15+
# This mocks node's `-r` flag so that examples look the same as if the library
16+
# directly installed
17+
alias node="node -r $dir/utils"

0 commit comments

Comments
 (0)