diff --git a/README.md b/README.md index 53870c1da..ce059757a 100644 --- a/README.md +++ b/README.md @@ -326,7 +326,7 @@ preview.addListener('rotate', (data) => { Development Setup ----------------- -1. Install Node v8.9.4 or higher. +1. Install latest LTS version of Node v8.9.4 or higher. 2. Install yarn package manager `https://yarnpkg.com/en/docs/install`. Alternatively, you can replace any `yarn` command with `npm`. 3. Fork the upstream repo `https://github.com/box/box-content-preview`. 4. Clone your fork locally `git clone git@github.com:[YOUR GITHUB USERNAME]/box-content-preview.git`. diff --git a/build/release.sh b/build/release.sh index d2543e25e..6d2cb9209 100755 --- a/build/release.sh +++ b/build/release.sh @@ -12,6 +12,9 @@ major_release=false minor_release=false patch_release=false +# LTS compatibility +node_validation_script_path="./build/validateNodeVersion.js" + reset_tags() { # Wipe tags echo "----------------------------------------------------------------------" @@ -178,9 +181,27 @@ push_to_github() { fi } +# Validates whether or not the supported version of NodeJS is being used +validate_node_version() { + $(node $node_validation_script_path) + + if [ "$?" -eq "1" ] ; then + echo "----------------------------------------------------------------------" + echo "Invalid version of Node. Must be LTS version >= v8.9.4" + echo "----------------------------------------------------------------------" + exit 1 + else + echo "----------------------------------------------------------------------" + echo "Valid version of Node" + echo "----------------------------------------------------------------------" + fi +} # Check out latest code from git, build assets, increment version, and push tags push_new_release() { + # Before running release, make sure using correct version of NodeJS + validate_node_version; + # Get latest commited code and tags if $patch_release; then echo "----------------------------------------------------------------------" @@ -236,7 +257,6 @@ while getopts "mnp" opt; do esac done - # Execute this entire script if ! push_new_release; then echo "----------------------------------------------------------------------" diff --git a/build/validateNodeVersion.js b/build/validateNodeVersion.js new file mode 100644 index 000000000..b8b406de4 --- /dev/null +++ b/build/validateNodeVersion.js @@ -0,0 +1,116 @@ +const axios = require('axios'); + +const SUPPORTED_LTS = "Carbon"; +const VERSION = process.versions.node; +const NODE_JS_VERSION_LIST_URL="https://nodejs.org/dist/index.json" +const MIN_MAJOR_SUPPORT = 8; +const MIN_MINOR_SUPPORT = 9; +const MIN_PATCH_SUPPORT = 4; + + +/** + * Exit with code 1 so that calling script can see that current node version was NOT valid. + * + * @returns {void} + */ +function fail() { + process.exit(1); +} + +/** + * Exit with code 0 so that calling script can see that current node version was valid. + * + * @returns {void} + */ +function pass() { + process.exit(0); +} + +/** + * Returns true if greater or same version as minimum supported version + * + * @param {Number} major - Major version number + * @param {Number} minor - Minor version number + * @param {Number} patch - Patch version number + * @returns {boolean} True if a supported LTS version of NodeJS + */ +function compareVersion(major, minor, patch) { + // If major version is greater, it's valid + if (major > MIN_MAJOR_SUPPORT) { + return true; + } + + // If same major supported version, and minor version is greater, it's valid + if (major == MIN_MAJOR_SUPPORT && minor > MIN_MINOR_SUPPORT) { + return true; + } + + // If major and minor are supprted, and patch is greater or same, it's valid + if (major == MIN_MAJOR_SUPPORT && minor == MIN_MINOR_SUPPORT && patch >= MIN_PATCH_SUPPORT) { + return true; + } + + return false; +} + +/** + * Returns an object containing major, minor, and patch numbers from a version string + * + * @param {string} versionString - Version in format 'MAJ.MIN.PATCH' + * @return {Object} with major, minor, and patch numbers + */ +function getVersionFromString(versionString) { + const splitVersion = versionString.split('.'); + + const major = parseInt(splitVersion[0]); + const minor = parseInt(splitVersion[1]); + const patch = parseInt(splitVersion[2]); + + return { + major, + minor, + patch + } +} + +/** + * Validates that the current version of node is valid for use. + * + * @param {Object} response - Axios response object + * @returns {void} + */ +function validateNodeVersion(response) { + // Version list comes back as an array + const versionList = response.data; + + // Versions from NodeJS listing come in format 'vXX.XX.XX' + const formattedVersion = `v${VERSION}`; + + // Make sure LTS supported version + const isValidVersion = versionList.some((nodeRelease) => { + const { + version : releaseVer, + lts + } = nodeRelease; + + return releaseVer === formattedVersion && lts; + }); + + if (isValidVersion) { + pass(); + } else { + fail(); + } +} + + +// Split into major/minor/patch and check that it passes before +// requesting the release list from NodeJS +const { major, minor, patch } = getVersionFromString(VERSION); +if (!compareVersion(major, minor, patch)) { + return fail(); +} + +axios.get(NODE_JS_VERSION_LIST_URL) + .then(validateNodeVersion) + .catch(fail); \ No newline at end of file diff --git a/package.json b/package.json index 1c3970a43..7ab283643 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "devDependencies": { "@commitlint/cli": "^5.2.0", "autoprefixer": "^7.2.1", + "axios": "^0.18.0", "babel-core": "^6.26.0", "babel-eslint": "^8.0.3", "babel-loader": "^7.1.2", diff --git a/yarn.lock b/yarn.lock index 9fb6e95e2..15c1fbd4f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -588,6 +588,13 @@ aws4@^1.2.1, aws4@^1.6.0: version "1.6.0" resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.6.0.tgz#83ef5ca860b2b32e4a0deedee8c771b9db57471e" +axios@^0.18.0: + version "0.18.0" + resolved "http://registry.npmjs.org/axios/-/axios-0.18.0.tgz#32d53e4851efdc0a11993b6cd000789d70c05102" + dependencies: + follow-redirects "^1.3.0" + is-buffer "^1.1.5" + axobject-query@^0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-0.1.0.tgz#62f59dbc59c9f9242759ca349960e7a2fe3c36c0" @@ -3542,6 +3549,12 @@ fn-args@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/fn-args/-/fn-args-3.0.0.tgz#df5c3805ed41ec3b38a72aabe390cf9493ec084c" +follow-redirects@^1.3.0: + version "1.5.7" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.7.tgz#a39e4804dacb90202bca76a9e2ac10433ca6a69a" + dependencies: + debug "^3.1.0" + for-in@^0.1.3: version "0.1.8" resolved "https://registry.yarnpkg.com/for-in/-/for-in-0.1.8.tgz#d8773908e31256109952b1fdb9b3fa867d2775e1"