Skip to content

Commit

Permalink
Raise an error when the version of node is older than package.json's …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
mattwynne committed Feb 10, 2022
1 parent c1ed743 commit 5295a45
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Please see [CONTRIBUTING.md](https://github.com/cucumber/cucumber/blob/master/CO
- Cucumber Expressions now support a wider array of parameter types (see [documentation](https://github.com/cucumber/cucumber-expressions#parameter-types))
- Improved styling and usability on report from `html` formatter

### Fixed
- Warn users who are on an unsupported node version ([#1922](https://github.com/cucumber/cucumber-js/pull/1922))

### Changed
- Switch from `colors` to `chalk` for terminal coloring ([#1895](https://github.com/cucumber/cucumber-js/pull/1895))

Expand Down
12 changes: 3 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@
"progress": "^2.0.3",
"resolve": "^1.19.0",
"resolve-pkg": "^2.0.0",
"semver": "7.3.5",
"stack-chain": "^2.0.0",
"string-argv": "^0.3.1",
"tmp": "^0.2.1",
Expand Down Expand Up @@ -258,7 +259,6 @@
"nyc": "15.1.0",
"prettier": "2.5.1",
"reindent-template-literals": "1.1.0",
"semver": "7.3.5",
"shx": "0.3.4",
"sinon": "13.0.1",
"sinon-chai": "3.7.0",
Expand Down
25 changes: 25 additions & 0 deletions src/cli/assert_node_engine_version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import fs from 'fs'
import path from 'path'
import semver from 'semver'

type PackageJSON = {
engines: { node: string }
}

const readActualPackageJSON: () => PackageJSON = () =>
JSON.parse(
fs
.readFileSync(path.resolve(__dirname, '..', '..', 'package.json'))
.toString()
)

export function assertNodeEngineVersion(
currentVersion: string,
readPackageJSON: () => PackageJSON = readActualPackageJSON
): void {
const requiredVersion = readPackageJSON().engines.node
if (!semver.satisfies(currentVersion, requiredVersion)) {
const message = `Cucumber can only run on Node.js versions ${requiredVersion}. This Node.js version is ${currentVersion}`
throw new Error(message)
}
}
18 changes: 18 additions & 0 deletions src/cli/assert_node_engine_version_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import assert from 'assert'
import { assertNodeEngineVersion } from './assert_node_engine_version'

describe(assertNodeEngineVersion.name, () => {
it('fails when the version is lower than specified in package.json', () => {
assert.throws(() =>
assertNodeEngineVersion('v11.0.0', () => ({
engines: {
node: '>=12',
},
}))
)
})

it('passes when the version is greater than specified in package.json', () => {
assertNodeEngineVersion('v17.0.0')
})
})
8 changes: 8 additions & 0 deletions src/cli/run.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Cli, { ICliRunResult } from './'
import VError from 'verror'
import publishBanner from './publish_banner'
import { assertNodeEngineVersion } from './assert_node_engine_version'

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

export default async function run(): Promise<void> {
try {
assertNodeEngineVersion(process.version)
} catch (error) {
console.error(error.message) // eslint-disable-line no-console
process.exit(1)
}

const cli = new Cli({
argv: process.argv,
cwd: process.cwd(),
Expand Down

0 comments on commit 5295a45

Please sign in to comment.