Skip to content

Commit 5295a45

Browse files
authored
Raise an error when the version of node is older than package.json's engines.node (#1922)
* Raise an error when the version of node is older than v12 * Update CHANGELOG * Move engine check to cli runner * Simplify * Extract to own file and add tests * Read minimum version from package.json * Make semver a production dependency
1 parent c1ed743 commit 5295a45

File tree

6 files changed

+58
-10
lines changed

6 files changed

+58
-10
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ Please see [CONTRIBUTING.md](https://github.com/cucumber/cucumber/blob/master/CO
1212
- Cucumber Expressions now support a wider array of parameter types (see [documentation](https://github.com/cucumber/cucumber-expressions#parameter-types))
1313
- Improved styling and usability on report from `html` formatter
1414

15+
### Fixed
16+
- Warn users who are on an unsupported node version ([#1922](https://github.com/cucumber/cucumber-js/pull/1922))
17+
1518
### Changed
1619
- Switch from `colors` to `chalk` for terminal coloring ([#1895](https://github.com/cucumber/cucumber-js/pull/1895))
1720

package-lock.json

Lines changed: 3 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@
209209
"progress": "^2.0.3",
210210
"resolve": "^1.19.0",
211211
"resolve-pkg": "^2.0.0",
212+
"semver": "7.3.5",
212213
"stack-chain": "^2.0.0",
213214
"string-argv": "^0.3.1",
214215
"tmp": "^0.2.1",
@@ -258,7 +259,6 @@
258259
"nyc": "15.1.0",
259260
"prettier": "2.5.1",
260261
"reindent-template-literals": "1.1.0",
261-
"semver": "7.3.5",
262262
"shx": "0.3.4",
263263
"sinon": "13.0.1",
264264
"sinon-chai": "3.7.0",

src/cli/assert_node_engine_version.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import fs from 'fs'
2+
import path from 'path'
3+
import semver from 'semver'
4+
5+
type PackageJSON = {
6+
engines: { node: string }
7+
}
8+
9+
const readActualPackageJSON: () => PackageJSON = () =>
10+
JSON.parse(
11+
fs
12+
.readFileSync(path.resolve(__dirname, '..', '..', 'package.json'))
13+
.toString()
14+
)
15+
16+
export function assertNodeEngineVersion(
17+
currentVersion: string,
18+
readPackageJSON: () => PackageJSON = readActualPackageJSON
19+
): void {
20+
const requiredVersion = readPackageJSON().engines.node
21+
if (!semver.satisfies(currentVersion, requiredVersion)) {
22+
const message = `Cucumber can only run on Node.js versions ${requiredVersion}. This Node.js version is ${currentVersion}`
23+
throw new Error(message)
24+
}
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import assert from 'assert'
2+
import { assertNodeEngineVersion } from './assert_node_engine_version'
3+
4+
describe(assertNodeEngineVersion.name, () => {
5+
it('fails when the version is lower than specified in package.json', () => {
6+
assert.throws(() =>
7+
assertNodeEngineVersion('v11.0.0', () => ({
8+
engines: {
9+
node: '>=12',
10+
},
11+
}))
12+
)
13+
})
14+
15+
it('passes when the version is greater than specified in package.json', () => {
16+
assertNodeEngineVersion('v17.0.0')
17+
})
18+
})

src/cli/run.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Cli, { ICliRunResult } from './'
22
import VError from 'verror'
33
import publishBanner from './publish_banner'
4+
import { assertNodeEngineVersion } from './assert_node_engine_version'
45

56
function exitWithError(error: Error): void {
67
console.error(VError.fullStack(error)) // eslint-disable-line no-console
@@ -12,6 +13,13 @@ function displayPublishAdvertisementBanner(): void {
1213
}
1314

1415
export default async function run(): Promise<void> {
16+
try {
17+
assertNodeEngineVersion(process.version)
18+
} catch (error) {
19+
console.error(error.message) // eslint-disable-line no-console
20+
process.exit(1)
21+
}
22+
1523
const cli = new Cli({
1624
argv: process.argv,
1725
cwd: process.cwd(),

0 commit comments

Comments
 (0)