forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refact(12976): migrated node version code from es6 to es5.
- Loading branch information
Showing
6 changed files
with
94 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
require('../../babel-register'); | ||
require('../../setup_node_env'); | ||
module.exports = require('./ts_transform.ts'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
// import { NodeVersion } from '../utils'; | ||
/* eslint-disable no-var */ | ||
|
||
console.log('Runs the node js version validator'); | ||
// Note: This is written in ES5 so we can run this before anything else | ||
// and gives support for older NodeJS versions | ||
var NodeVersion = require('../utils/node_version').NodeVersion; | ||
|
||
// // Validates current the NodeJS version compatibility when Kibana starts. | ||
// NodeVersion.runValidator(() => { | ||
// // Action to apply when validation fails: error report + exit. | ||
// console.error( | ||
// `Kibana does not support the current Node.js version ${NodeVersion.getCurrentVersion()}. ` | ||
// + `Please use Node.js ${NodeVersion.getRequiredVersion()}.` | ||
// ); | ||
// process.exit(1); | ||
// }); | ||
// Validates current the NodeJS version compatibility when Kibana starts. | ||
NodeVersion.runValidator(function () { | ||
// Action to apply when validation fails: error report + exit. | ||
console.error( | ||
`Kibana does not support the current Node.js version ${NodeVersion.getCurrentVersion()}. ` | ||
+ `Please use Node.js ${NodeVersion.getRequiredVersion()}.` | ||
); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,84 @@ | ||
import _ from 'lodash'; | ||
import { pkg } from './index'; | ||
import semver from 'semver'; | ||
/* eslint-disable no-var */ | ||
|
||
var _ = require('lodash'); | ||
var semver = require('semver'); | ||
var pkg = require('../../package.json'); | ||
|
||
/** | ||
* Offers utility methods to deal with node versions | ||
* | ||
* @class NodeVersion | ||
* @static | ||
*/ | ||
class NodeVersion { | ||
/** | ||
* Get the current NodeJs version used to run this code | ||
* | ||
* @returns {?string} | ||
* @static | ||
*/ | ||
static getCurrentVersion() { | ||
return semver.valid( | ||
semver.coerce( | ||
_.get(process, 'version', null) | ||
) | ||
); | ||
} | ||
function NodeVersion() {} | ||
|
||
/** | ||
* Get the required NodeJs version to run this code according | ||
* the one defined in the package.json file (engines config) | ||
* | ||
* @returns {?string} | ||
* @static | ||
*/ | ||
static getRequiredVersion() { | ||
return _.get(pkg, 'engines.node', null); | ||
} | ||
/** | ||
* Get the current NodeJs version used to run this code | ||
* | ||
* @returns {?string} | ||
* @static | ||
*/ | ||
NodeVersion.getCurrentVersion = function () { | ||
return semver.valid( | ||
semver.coerce( | ||
_.get(process, 'version', null) | ||
) | ||
); | ||
}; | ||
|
||
/** | ||
* Compares the current NodeJS version and the required one | ||
* and states if the version requirements are being met. | ||
* | ||
* @param {?string} currentVersion The current NodeJS version in use | ||
* @param {?string} requiredVersion The required NodeJS version defined in package.json. | ||
* It supports the NodeJS range versions notation. | ||
* | ||
* @returns {boolean} | ||
* @static | ||
*/ | ||
static isVersionValid(currentVersion, requiredVersion) { | ||
return (!!currentVersion | ||
&& !!requiredVersion | ||
&& semver.satisfies(currentVersion, requiredVersion) | ||
); | ||
} | ||
/** | ||
* Get the required NodeJs version to run this code according | ||
* the one defined in the package.json file (engines config) | ||
* | ||
* @returns {?string} | ||
* @static | ||
*/ | ||
NodeVersion.getRequiredVersion = function () { | ||
return _.get(pkg, 'engines.node', null); | ||
}; | ||
|
||
/** | ||
* Compares the current NodeJS version and the required one | ||
* and states if the version requirements are being met. | ||
* | ||
* @param {?string} currentVersion The current NodeJS version in use | ||
* @param {?string} requiredVersion The required NodeJS version defined in package.json. | ||
* It supports the NodeJS range versions notation. | ||
* | ||
* @returns {boolean} | ||
* @static | ||
*/ | ||
NodeVersion.isVersionValid = function (currentVersion, requiredVersion) { | ||
return (!!currentVersion | ||
&& !!requiredVersion | ||
&& semver.satisfies(currentVersion, requiredVersion) | ||
); | ||
}; | ||
|
||
/** | ||
* Allows to run a validator callback in both valid or invalid validation results. | ||
* | ||
* @param {NodeVersion~ValidatorAction} validatorAction Validator callback function | ||
* @param {boolean} [runActionWhenValid = false] Defines where the callback runs in | ||
* a valid or invalid validation context. | ||
* @static | ||
*/ | ||
static runValidator(validatorAction, runActionWhenValid = false) { | ||
const currentVersion = NodeVersion.getCurrentVersion(); | ||
const requiredVersion = NodeVersion.getRequiredVersion(); | ||
const isVersionValid = NodeVersion.isVersionValid(currentVersion, requiredVersion); | ||
/** | ||
* Allows to run a validator callback in both valid or invalid validation results. | ||
* | ||
* @param {NodeVersion~ValidatorAction} validatorAction Validator callback function | ||
* @param {boolean} [runActionWhenValid = false] Defines where the callback runs in | ||
* a valid or invalid validation context. | ||
* @static | ||
*/ | ||
NodeVersion.runValidator = function (validatorAction, runActionWhenValid) { | ||
var currentVersion = NodeVersion.getCurrentVersion(); | ||
var requiredVersion = NodeVersion.getRequiredVersion(); | ||
var isVersionValid = NodeVersion.isVersionValid(currentVersion, requiredVersion); | ||
|
||
runActionWhenValid = typeof runActionWhenValid !== 'undefined' ? runActionWhenValid : false; | ||
|
||
if (isVersionValid === runActionWhenValid) { | ||
validatorAction(); | ||
} | ||
if (isVersionValid === runActionWhenValid) { | ||
validatorAction(); | ||
} | ||
/** | ||
* A callback function to being used by the runValidator method | ||
* | ||
* @callback NodeVersion~ValidatorAction | ||
*/ | ||
} | ||
}; | ||
/** | ||
* A callback function to being used by the runValidator method | ||
* | ||
* @callback NodeVersion~ValidatorAction | ||
*/ | ||
|
||
export { NodeVersion }; | ||
module.exports = NodeVersion; | ||
module.exports.NodeVersion = NodeVersion; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters