Skip to content

Commit

Permalink
refact(12976): migrated node version code from es6 to es5.
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic committed May 22, 2018
1 parent b3d61c9 commit 389e29a
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 85 deletions.
4 changes: 3 additions & 1 deletion scripts/jest.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-var */

// # Run Jest tests
//
// All args will be forwarded directly to Jest, e.g. to watch tests run:
Expand All @@ -10,7 +12,7 @@
//
// See all cli options in https://facebook.github.io/jest/docs/cli.html

const { resolve } = require('path');
var resolve = require('path').resolve;
process.argv.push('--config', resolve(__dirname, '../src/dev/jest/config.js'));

require('../src/setup_node_env');
Expand Down
4 changes: 3 additions & 1 deletion scripts/jest_integration.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-var */

// # Run Jest integration tests
//
// All args will be forwarded directly to Jest, e.g. to watch tests run:
Expand All @@ -10,7 +12,7 @@
//
// See all cli options in https://facebook.github.io/jest/docs/cli.html

const { resolve } = require('path');
var resolve = require('path').resolve;
process.argv.push('--config', resolve(__dirname, '../src/dev/jest/config.integration.js'));

require('../src/setup_node_env');
Expand Down
2 changes: 1 addition & 1 deletion src/dev/jest/ts_transform.js
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');
24 changes: 13 additions & 11 deletions src/setup_node_env/node_version_validator.js
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);
});
137 changes: 71 additions & 66 deletions src/utils/node_version.js
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;
8 changes: 3 additions & 5 deletions src/utils/node_version.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ const defineProcessVersion = (version, writable = true) => {
Object.defineProperty(process, 'version', { value: version, writable });
};

jest.mock('./index', () => ({
pkg: {
engines: {
node: '8.12.0'
}
jest.mock('../../package.json', () => ({
engines: {
node: '8.12.0'
}
}));

Expand Down

0 comments on commit 389e29a

Please sign in to comment.