From 78ae78af94e9a8f65b2a9906524910db7e62f7ff Mon Sep 17 00:00:00 2001 From: Justin Holdstock Date: Thu, 23 Aug 2018 15:03:52 -0600 Subject: [PATCH 1/7] New: Adding NodeJS version check to Release script --- README.md | 2 +- build/release.sh | 19 ++++++ build/validateNodeVersion.js | 112 +++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 build/validateNodeVersion.js diff --git a/README.md b/README.md index 8dae9e3e0..a899582a5 100644 --- a/README.md +++ b/README.md @@ -325,7 +325,7 @@ preview.addListener('rotate', (data) => { Development Setup ----------------- -1. Install Node v8.9.4 or higher. +1. Install latest Node v8.9.4 or higher (must be LTS Carbon). 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..58f8c1e95 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,6 +181,20 @@ push_to_github() { fi } +# Validates whether or not the supported version of NodeJS is being used +validate_node_version() { + isValidNodeVersion=$(node $node_validation_script_path) + if ! $isValidNodeVersion == 'false' ; 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() { @@ -236,6 +253,8 @@ while getopts "mnp" opt; do esac done +# Before running release, make sure using correct version of NodeJS +validate_node_version; # Execute this entire script if ! push_new_release; then diff --git a/build/validateNodeVersion.js b/build/validateNodeVersion.js new file mode 100644 index 000000000..385f45623 --- /dev/null +++ b/build/validateNodeVersion.js @@ -0,0 +1,112 @@ +const https = require('https'); + +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; + +let body = ''; + +function fail() { + body = ''; + console.log(false); +} + +function pass() { + body = ''; + console.log(true); +} + +/** + * 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. + * @returns {void} + */ +function validateNodeVersion() { + // split into major/minor/patch + const { major, minor, patch } = getVersionFromString(VERSION); + + if (!compareVersion(major, minor, patch)) { + return fail(); + } + + // Version list comes back as an array + const versionList = JSON.parse(body); + + // 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(); + } +} + +https.get(NODE_JS_VERSION_LIST_URL, response => { + response.on('data', data => { + body += data; + }); + + response.on('end', validateNodeVersion); + + response.on('error', fail); +}); \ No newline at end of file From 92d4d1f6988e5ed10adf2414ba6f4869cc66892c Mon Sep 17 00:00:00 2001 From: Justin Holdstock Date: Thu, 23 Aug 2018 15:06:18 -0600 Subject: [PATCH 2/7] Chore: change wording in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a899582a5..f34986f52 100644 --- a/README.md +++ b/README.md @@ -325,7 +325,7 @@ preview.addListener('rotate', (data) => { Development Setup ----------------- -1. Install latest Node v8.9.4 or higher (must be LTS Carbon). +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`. From b7b6c98919fec1089ddc760ae00f61f1e7bf8d08 Mon Sep 17 00:00:00 2001 From: Justin Holdstock Date: Thu, 23 Aug 2018 16:44:04 -0600 Subject: [PATCH 3/7] Chore: optimize to avoid un-needed network req --- build/release.sh | 6 ++++-- build/validateNodeVersion.js | 16 +++++++++------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/build/release.sh b/build/release.sh index 58f8c1e95..2b795f06f 100755 --- a/build/release.sh +++ b/build/release.sh @@ -198,6 +198,9 @@ validate_node_version() { # 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 "----------------------------------------------------------------------" @@ -253,8 +256,7 @@ while getopts "mnp" opt; do esac done -# Before running release, make sure using correct version of NodeJS -validate_node_version; +push_new_release # Execute this entire script if ! push_new_release; then diff --git a/build/validateNodeVersion.js b/build/validateNodeVersion.js index 385f45623..79a132b56 100644 --- a/build/validateNodeVersion.js +++ b/build/validateNodeVersion.js @@ -68,16 +68,10 @@ function getVersionFromString(versionString) { /** * Validates that the current version of node is valid for use. + * * @returns {void} */ function validateNodeVersion() { - // split into major/minor/patch - const { major, minor, patch } = getVersionFromString(VERSION); - - if (!compareVersion(major, minor, patch)) { - return fail(); - } - // Version list comes back as an array const versionList = JSON.parse(body); @@ -101,6 +95,14 @@ function validateNodeVersion() { } } + +// 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(); +} + https.get(NODE_JS_VERSION_LIST_URL, response => { response.on('data', data => { body += data; From f40506d9385eac72a19a964349f60a4a3f49b344 Mon Sep 17 00:00:00 2001 From: Justin Holdstock Date: Thu, 23 Aug 2018 17:11:33 -0600 Subject: [PATCH 4/7] Chore: Using axios and validating in push_new_release --- build/release.sh | 3 +-- build/validateNodeVersion.js | 23 +++++++---------------- package.json | 1 + yarn.lock | 13 +++++++++++++ 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/build/release.sh b/build/release.sh index 2b795f06f..70fa0733e 100755 --- a/build/release.sh +++ b/build/release.sh @@ -184,6 +184,7 @@ push_to_github() { # Validates whether or not the supported version of NodeJS is being used validate_node_version() { isValidNodeVersion=$(node $node_validation_script_path) + if ! $isValidNodeVersion == 'false' ; then echo "----------------------------------------------------------------------" echo "Invalid version of Node. Must be LTS version >= v8.9.4" @@ -256,8 +257,6 @@ while getopts "mnp" opt; do esac done -push_new_release - # Execute this entire script if ! push_new_release; then echo "----------------------------------------------------------------------" diff --git a/build/validateNodeVersion.js b/build/validateNodeVersion.js index 79a132b56..f5d49e832 100644 --- a/build/validateNodeVersion.js +++ b/build/validateNodeVersion.js @@ -1,4 +1,4 @@ -const https = require('https'); +const axios = require('axios'); const SUPPORTED_LTS = "Carbon"; const VERSION = process.versions.node; @@ -7,15 +7,11 @@ const MIN_MAJOR_SUPPORT = 8; const MIN_MINOR_SUPPORT = 9; const MIN_PATCH_SUPPORT = 4; -let body = ''; - function fail() { - body = ''; console.log(false); } function pass() { - body = ''; console.log(true); } @@ -69,11 +65,12 @@ function getVersionFromString(versionString) { /** * Validates that the current version of node is valid for use. * + * @param {Object} response - Axios response object * @returns {void} */ -function validateNodeVersion() { +function validateNodeVersion(response) { // Version list comes back as an array - const versionList = JSON.parse(body); + const versionList = response.data; // Versions from NodeJS listing come in format 'vXX.XX.XX' const formattedVersion = `v${VERSION}`; @@ -103,12 +100,6 @@ if (!compareVersion(major, minor, patch)) { return fail(); } -https.get(NODE_JS_VERSION_LIST_URL, response => { - response.on('data', data => { - body += data; - }); - - response.on('end', validateNodeVersion); - - response.on('error', fail); -}); \ No newline at end of file +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 9efb2763d..68c0248fe 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..d73717386 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 "https://box.jfrog.io/box/api/npm/boxnpm/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://box.jfrog.io/box/api/npm/boxnpm/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" From 34495ee9cad9303574e24642212b11df86165fcf Mon Sep 17 00:00:00 2001 From: Justin Holdstock Date: Wed, 29 Aug 2018 12:57:20 -0600 Subject: [PATCH 5/7] Chore: using process.exit() instead of console.log() --- build/release.sh | 4 ++-- build/validateNodeVersion.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/build/release.sh b/build/release.sh index 70fa0733e..6d2cb9209 100755 --- a/build/release.sh +++ b/build/release.sh @@ -183,9 +183,9 @@ push_to_github() { # Validates whether or not the supported version of NodeJS is being used validate_node_version() { - isValidNodeVersion=$(node $node_validation_script_path) + $(node $node_validation_script_path) - if ! $isValidNodeVersion == 'false' ; then + if [ "$?" -eq "1" ] ; then echo "----------------------------------------------------------------------" echo "Invalid version of Node. Must be LTS version >= v8.9.4" echo "----------------------------------------------------------------------" diff --git a/build/validateNodeVersion.js b/build/validateNodeVersion.js index f5d49e832..2457e1e57 100644 --- a/build/validateNodeVersion.js +++ b/build/validateNodeVersion.js @@ -8,11 +8,11 @@ const MIN_MINOR_SUPPORT = 9; const MIN_PATCH_SUPPORT = 4; function fail() { - console.log(false); + process.exit(1); } function pass() { - console.log(true); + process.exit(0); } /** From bea66a6328764b9c607c81107c5139e9cf2d21c7 Mon Sep 17 00:00:00 2001 From: Justin Holdstock Date: Wed, 29 Aug 2018 13:34:48 -0600 Subject: [PATCH 6/7] Chore: using public axios module --- yarn.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/yarn.lock b/yarn.lock index d73717386..15c1fbd4f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -590,7 +590,7 @@ aws4@^1.2.1, aws4@^1.6.0: axios@^0.18.0: version "0.18.0" - resolved "https://box.jfrog.io/box/api/npm/boxnpm/axios/-/axios-0.18.0.tgz#32d53e4851efdc0a11993b6cd000789d70c05102" + resolved "http://registry.npmjs.org/axios/-/axios-0.18.0.tgz#32d53e4851efdc0a11993b6cd000789d70c05102" dependencies: follow-redirects "^1.3.0" is-buffer "^1.1.5" @@ -3551,7 +3551,7 @@ fn-args@^3.0.0: follow-redirects@^1.3.0: version "1.5.7" - resolved "https://box.jfrog.io/box/api/npm/boxnpm/follow-redirects/-/follow-redirects-1.5.7.tgz#a39e4804dacb90202bca76a9e2ac10433ca6a69a" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.7.tgz#a39e4804dacb90202bca76a9e2ac10433ca6a69a" dependencies: debug "^3.1.0" From 2ef3f87179c6f8c96f546cc887e1815276f21d20 Mon Sep 17 00:00:00 2001 From: Justin Holdstock Date: Thu, 30 Aug 2018 10:50:08 -0600 Subject: [PATCH 7/7] Chore: Code docs to pass/fail --- build/validateNodeVersion.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/build/validateNodeVersion.js b/build/validateNodeVersion.js index 2457e1e57..b8b406de4 100644 --- a/build/validateNodeVersion.js +++ b/build/validateNodeVersion.js @@ -7,10 +7,21 @@ 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); }