Skip to content

Commit ff56df4

Browse files
authored
formatters: Load custom formatter by relative path only if it begins with a dot (#1413)
1 parent 944aeec commit ff56df4

File tree

5 files changed

+17
-15
lines changed

5 files changed

+17
-15
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ If anything is missing from the migration guide, please submit an issue.
4545
* Add `transpose` method to [data table interface](docs/support_files/data_table_interface.md)
4646
* Add `log` function to world, providing a shorthand to log plain text as [attachment(s)](docs/support_files/attachments.md)
4747
* Now includes [TypeScript](https://www.typescriptlang.org/) type definitions, deprecating the need for `@types/cucumber` in TypeScript projects
48+
* Yarn PnP can now be used with this project with custom formatters [#1413](https://github.com/cucumber/cucumber-js/pull/1413)
4849

4950
### Breaking changes
5051

@@ -58,6 +59,7 @@ If anything is missing from the migration guide, please submit an issue.
5859
* Custom formatters will need to migrate
5960
* `json` formatter is deprecated and will be removed in next major release. Custom formatters should migrate to use the `message` formatter, or the [standalone JSON formatter](https://github.com/cucumber/cucumber/tree/master/json-formatter) as a stopgap.
6061
* Remove long-deprecated `typeName` from options object for `defineParameterType` in favour of `name`
62+
* Custom formatters are now loaded via the regular require paths relative to the current directory, unless it begins with a dot (e.g. `--format=./relpath/to/formatter`). Previously this was always loaded as a file relative to the current directory.
6163

6264
### Bug fixes
6365

features/support/world.ts

+6-13
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,12 @@ export class World {
5454
envOverride: NodeJS.ProcessEnv = null
5555
): Promise<void> {
5656
const messageFilename = 'message.ndjson'
57-
const args = ['node', executablePath]
58-
.concat(inputArgs, [
59-
'--backtrace',
60-
'--predictable-ids',
61-
'--format',
62-
`message:${messageFilename}`,
63-
])
64-
.map((arg) => {
65-
if (_.includes(arg, '/')) {
66-
return path.normalize(arg)
67-
}
68-
return arg
69-
})
57+
const args = ['node', executablePath].concat(inputArgs, [
58+
'--backtrace',
59+
'--predictable-ids',
60+
'--format',
61+
`message:${messageFilename}`,
62+
])
7063
const env = _.merge({}, process.env, this.sharedEnv, envOverride)
7164
const cwd = this.tmpDir
7265

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
"cli-table3": "^0.6.0",
178178
"colors": "^1.4.0",
179179
"commander": "^5.0.0",
180+
"create-require": "^1.1.1",
180181
"duration": "^0.2.2",
181182
"durations": "^3.4.2",
182183
"figures": "^3.2.0",

src/formatter/builder.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { Writable as WritableStream } from 'stream'
2020
import { IParsedArgvFormatOptions } from '../cli/argv_parser'
2121
import { SnippetInterface } from './step_definition_snippet_builder/snippet_syntax'
2222
import HtmlFormatter from './html_formatter'
23+
import createRequire from 'create-require'
2324

2425
interface IGetStepDefinitionSnippetBuilderOptions {
2526
cwd: string
@@ -107,8 +108,8 @@ const FormatterBuilder = {
107108
},
108109

109110
loadCustomFormatter(customFormatterPath: string, cwd: string) {
110-
const fullCustomFormatterPath = path.resolve(cwd, customFormatterPath)
111-
const CustomFormatter = require(fullCustomFormatterPath) // eslint-disable-line @typescript-eslint/no-var-requires
111+
const CustomFormatter = createRequire(cwd)(customFormatterPath)
112+
112113
if (typeof CustomFormatter === 'function') {
113114
return CustomFormatter
114115
} else if (

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,11 @@ create-hmac@^1.1.0, create-hmac@^1.1.4, create-hmac@^1.1.7:
16121612
safe-buffer "^5.0.1"
16131613
sha.js "^2.4.8"
16141614

1615+
create-require@^1.1.1:
1616+
version "1.1.1"
1617+
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
1618+
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==
1619+
16151620
cross-spawn@^7.0.0, cross-spawn@^7.0.2:
16161621
version "7.0.3"
16171622
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"

0 commit comments

Comments
 (0)