From ff56df4ebed5e1305a3cd1290870b13386a14561 Mon Sep 17 00:00:00 2001 From: Dawn Minion <35529725+dawn-minion@users.noreply.github.com> Date: Thu, 26 Nov 2020 16:35:35 +0100 Subject: [PATCH] formatters: Load custom formatter by relative path only if it begins with a dot (#1413) --- CHANGELOG.md | 2 ++ features/support/world.ts | 19 ++++++------------- package.json | 1 + src/formatter/builder.ts | 5 +++-- yarn.lock | 5 +++++ 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f144c996..9c84680fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ If anything is missing from the migration guide, please submit an issue. * Add `transpose` method to [data table interface](docs/support_files/data_table_interface.md) * Add `log` function to world, providing a shorthand to log plain text as [attachment(s)](docs/support_files/attachments.md) * Now includes [TypeScript](https://www.typescriptlang.org/) type definitions, deprecating the need for `@types/cucumber` in TypeScript projects +* Yarn PnP can now be used with this project with custom formatters [#1413](https://github.com/cucumber/cucumber-js/pull/1413) ### Breaking changes @@ -58,6 +59,7 @@ If anything is missing from the migration guide, please submit an issue. * Custom formatters will need to migrate * `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. * Remove long-deprecated `typeName` from options object for `defineParameterType` in favour of `name` +* 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. ### Bug fixes diff --git a/features/support/world.ts b/features/support/world.ts index 10c68d6be..0c3e54dc1 100644 --- a/features/support/world.ts +++ b/features/support/world.ts @@ -54,19 +54,12 @@ export class World { envOverride: NodeJS.ProcessEnv = null ): Promise { const messageFilename = 'message.ndjson' - const args = ['node', executablePath] - .concat(inputArgs, [ - '--backtrace', - '--predictable-ids', - '--format', - `message:${messageFilename}`, - ]) - .map((arg) => { - if (_.includes(arg, '/')) { - return path.normalize(arg) - } - return arg - }) + const args = ['node', executablePath].concat(inputArgs, [ + '--backtrace', + '--predictable-ids', + '--format', + `message:${messageFilename}`, + ]) const env = _.merge({}, process.env, this.sharedEnv, envOverride) const cwd = this.tmpDir diff --git a/package.json b/package.json index 4e852df53..a270fb247 100644 --- a/package.json +++ b/package.json @@ -177,6 +177,7 @@ "cli-table3": "^0.6.0", "colors": "^1.4.0", "commander": "^5.0.0", + "create-require": "^1.1.1", "duration": "^0.2.2", "durations": "^3.4.2", "figures": "^3.2.0", diff --git a/src/formatter/builder.ts b/src/formatter/builder.ts index e4e9492db..27da244eb 100644 --- a/src/formatter/builder.ts +++ b/src/formatter/builder.ts @@ -20,6 +20,7 @@ import { Writable as WritableStream } from 'stream' import { IParsedArgvFormatOptions } from '../cli/argv_parser' import { SnippetInterface } from './step_definition_snippet_builder/snippet_syntax' import HtmlFormatter from './html_formatter' +import createRequire from 'create-require' interface IGetStepDefinitionSnippetBuilderOptions { cwd: string @@ -107,8 +108,8 @@ const FormatterBuilder = { }, loadCustomFormatter(customFormatterPath: string, cwd: string) { - const fullCustomFormatterPath = path.resolve(cwd, customFormatterPath) - const CustomFormatter = require(fullCustomFormatterPath) // eslint-disable-line @typescript-eslint/no-var-requires + const CustomFormatter = createRequire(cwd)(customFormatterPath) + if (typeof CustomFormatter === 'function') { return CustomFormatter } else if ( diff --git a/yarn.lock b/yarn.lock index 7da456b67..db56a5299 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1612,6 +1612,11 @@ create-hmac@^1.1.0, create-hmac@^1.1.4, create-hmac@^1.1.7: safe-buffer "^5.0.1" sha.js "^2.4.8" +create-require@^1.1.1: + version "1.1.1" + resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333" + integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ== + cross-spawn@^7.0.0, cross-spawn@^7.0.2: version "7.0.3" resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"