From 1b8839e33cdff36b3750d89ee1966fed5007d640 Mon Sep 17 00:00:00 2001 From: Dawn Minion Date: Wed, 26 Aug 2020 09:57:49 +0200 Subject: [PATCH 1/6] Load formatter by rel path only if starts with . This commit makes it possible to load a custom formatter that is not relative to the current working directory, such as one installed globally, or via Yarn PNP. If the path begins with a . it will be loaded relative to the current directory. --- features/support/world.ts | 19 ++++++------------- src/formatter/builder.ts | 11 +++++++++-- 2 files changed, 15 insertions(+), 15 deletions(-) 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/src/formatter/builder.ts b/src/formatter/builder.ts index e4e9492db..3b7512693 100644 --- a/src/formatter/builder.ts +++ b/src/formatter/builder.ts @@ -107,8 +107,15 @@ 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 + let CustomFormatter = null + + if (customFormatterPath.startsWith('.')) { + const fullCustomFormatterPath = path.resolve(cwd, customFormatterPath) + CustomFormatter = require(fullCustomFormatterPath) // eslint-disable-line @typescript-eslint/no-var-requires + } else { + CustomFormatter = require(customFormatterPath) // eslint-disable-line @typescript-eslint/no-var-requires + } + if (typeof CustomFormatter === 'function') { return CustomFormatter } else if ( From 4bb5a343f531bb0832427698d7521cf92a56a7bc Mon Sep 17 00:00:00 2001 From: Dawn Minion Date: Mon, 2 Nov 2020 10:06:33 +0100 Subject: [PATCH 2/6] Add note about this change to CHANGELOG breaking changes --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d45f21f68..d439e5b2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,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 no longer loaded relative to the current directory unless it begins with a dot (e.g. `--format=./relpath/to/formatter`) ### Bug fixes From 91dbe2d07ee9bfeab9b7de6f5f258bd3d88cdb57 Mon Sep 17 00:00:00 2001 From: Dawn Minion Date: Wed, 25 Nov 2020 09:04:35 +0100 Subject: [PATCH 3/6] Add YarnPNP to feature changelog, switch to create-require --- CHANGELOG.md | 3 ++- package.json | 1 + src/formatter/builder.ts | 8 ++------ tsconfig.json | 2 +- yarn.lock | 5 +++++ 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d439e5b2a..c29ac04c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,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 @@ -56,7 +57,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 no longer loaded relative to the current directory unless it begins with a dot (e.g. `--format=./relpath/to/formatter`) +* Custom formatters are no longer loaded relative to the current directory unless it begins with a dot (e.g. `--format=./relpath/to/formatter`). ### Bug fixes diff --git a/package.json b/package.json index 4e852df53..0cb1a46cf 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.0", "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 3b7512693..3c1dc5d66 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 @@ -109,12 +110,7 @@ const FormatterBuilder = { loadCustomFormatter(customFormatterPath: string, cwd: string) { let CustomFormatter = null - if (customFormatterPath.startsWith('.')) { - const fullCustomFormatterPath = path.resolve(cwd, customFormatterPath) - CustomFormatter = require(fullCustomFormatterPath) // eslint-disable-line @typescript-eslint/no-var-requires - } else { - CustomFormatter = require(customFormatterPath) // eslint-disable-line @typescript-eslint/no-var-requires - } + CustomFormatter = createRequire(cwd)(customFormatterPath) if (typeof CustomFormatter === 'function') { return CustomFormatter diff --git a/tsconfig.json b/tsconfig.json index 4b9f0e8cd..c386cc5ab 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "esModuleInterop": true, - "lib": ["es2017"], + "lib": ["es2017", "dom"], "module": "commonjs", "noImplicitAny": true, "noImplicitReturns": true, diff --git a/yarn.lock b/yarn.lock index 7da456b67..1429cc914 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.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.0.tgz#8fe85b319928953deef6f0e5c44bfea13e3c81ef" + integrity sha512-yEFVS7dQjDXp5iOEtWisN4uFmL+pUTyIaEizKda9Eb77XX58p6pgFOLAPaBCP+IR6ZPZ1jgJLAuf+ABk0zXYBQ== + 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" From 54f2c6a8e0bc30e3c858d51f43e94b6891faf4a4 Mon Sep 17 00:00:00 2001 From: Dawn Minion Date: Wed, 25 Nov 2020 09:32:44 +0100 Subject: [PATCH 4/6] Change wording of custom formatter path change --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c29ac04c2..544228026 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,7 +57,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 no longer loaded relative to the current directory unless it begins with a dot (e.g. `--format=./relpath/to/formatter`). +* 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 From 47d0a8c6f176ef97e4ccf52b461c59afd86f5a48 Mon Sep 17 00:00:00 2001 From: Dawn Minion Date: Wed, 25 Nov 2020 09:33:18 +0100 Subject: [PATCH 5/6] Make CustomFormatter const --- src/formatter/builder.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/formatter/builder.ts b/src/formatter/builder.ts index 3c1dc5d66..27da244eb 100644 --- a/src/formatter/builder.ts +++ b/src/formatter/builder.ts @@ -108,9 +108,7 @@ const FormatterBuilder = { }, loadCustomFormatter(customFormatterPath: string, cwd: string) { - let CustomFormatter = null - - CustomFormatter = createRequire(cwd)(customFormatterPath) + const CustomFormatter = createRequire(cwd)(customFormatterPath) if (typeof CustomFormatter === 'function') { return CustomFormatter From 36b28230e9be1d52e08e384369c883b833c95593 Mon Sep 17 00:00:00 2001 From: Dawn Minion Date: Thu, 26 Nov 2020 15:02:35 +0100 Subject: [PATCH 6/6] Bump create-require to 1.1.1 and remove dom from tsconfig --- package.json | 2 +- tsconfig.json | 2 +- yarn.lock | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 0cb1a46cf..a270fb247 100644 --- a/package.json +++ b/package.json @@ -177,7 +177,7 @@ "cli-table3": "^0.6.0", "colors": "^1.4.0", "commander": "^5.0.0", - "create-require": "^1.1.0", + "create-require": "^1.1.1", "duration": "^0.2.2", "durations": "^3.4.2", "figures": "^3.2.0", diff --git a/tsconfig.json b/tsconfig.json index c386cc5ab..4b9f0e8cd 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "esModuleInterop": true, - "lib": ["es2017", "dom"], + "lib": ["es2017"], "module": "commonjs", "noImplicitAny": true, "noImplicitReturns": true, diff --git a/yarn.lock b/yarn.lock index 1429cc914..db56a5299 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1612,10 +1612,10 @@ 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.0: - version "1.1.0" - resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.0.tgz#8fe85b319928953deef6f0e5c44bfea13e3c81ef" - integrity sha512-yEFVS7dQjDXp5iOEtWisN4uFmL+pUTyIaEizKda9Eb77XX58p6pgFOLAPaBCP+IR6ZPZ1jgJLAuf+ABk0zXYBQ== +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"