Skip to content

Commit

Permalink
feat(ignitor): validate engines.node version (if defined) against cur…
Browse files Browse the repository at this point in the history
…rent node.js version
  • Loading branch information
thetutlage committed Feb 26, 2020
1 parent 7d614f1 commit 9071691
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
45 changes: 41 additions & 4 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
"ms": "^2.1.2",
"on-finished": "^2.3.0",
"randomstring": "^1.1.5",
"semver": "^7.1.3",
"serve-static": "^1.14.1"
},
"peerDependencies": {
Expand Down
21 changes: 21 additions & 0 deletions src/Ignitor/Bootstrapper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import { join } from 'path'
import semver from 'semver'
import findPkg from 'find-package-json'
import { esmRequire } from '@poppinss/utils'
import { Ioc, Registrar } from '@adonisjs/fold'
Expand Down Expand Up @@ -54,6 +55,24 @@ export class Bootstrapper {
) {
}

/**
* If package.json file defines a the engines.node property, then
* this method will ensure that current node version satisfies
* the defined range.
*/
private verifyNodeJsVersion (pkgFile?: findPkg.Package): void {
const nodeEngine = pkgFile?.engines?.node
if (!nodeEngine) {
return
}

if (!semver.satisfies(process.version, nodeEngine)) {
throw new Error(
`The installed Node.js version "${process.version}" does not satisfies the expected version "${nodeEngine}" defined inside package.json file`,
)
}
}

/**
* Setup the Ioc container globals and the application. This lays
* off the ground for not having `global.use` runtime errors.
Expand Down Expand Up @@ -99,6 +118,8 @@ export class Bootstrapper {
this.application = new Application(this.appRoot, ioc, rcContents, pkgFile)
this.registrar = new Registrar(ioc, this.appRoot)

this.verifyNodeJsVersion(appPkgFile)

ioc.singleton('Adonis/Core/Application', () => this.application)
return this.application
}
Expand Down
9 changes: 9 additions & 0 deletions test/ignitor-boostrapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ test.group('Ignitor | Setup', (group) => {
await fs.cleanup()
})

test('raise error when node engine inside package.json file doesn\'t satisfies current verison', async (assert) => {
await setupApplicationFiles(fs)
const bootstrapper = new Bootstrapper(fs.basePath)
await fs.add('package.json', JSON.stringify({ engines: { node: '4.x.x' } }))
const fn = () => bootstrapper.setup()
// eslint-disable-next-line max-len
assert.throw(fn, `The installed Node.js version \"${process.version}\" does not satisfies the expected version \"4.x.x\" defined inside package.json file`)
})

test('setup application', async (assert) => {
await setupApplicationFiles(fs)
const bootstrapper = new Bootstrapper(fs.basePath)
Expand Down

0 comments on commit 9071691

Please sign in to comment.