From 9e217f9c91b074c43647d966e83037d79e2f65b0 Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Wed, 16 Mar 2016 10:24:38 +0100 Subject: [PATCH] Improve algorithm --- README.md | 2 +- bin/index.js | 30 ++++++++++++++++++++---------- package.json | 2 +- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index b12dc85..df2fec8 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ It needs a global version manager as [n](https://www.npmjs.com/package/n) or [nv ## Install ```bash -$ npm install nodengine --global +$ npm install nodengine --global --production ``` ## Usage diff --git a/bin/index.js b/bin/index.js index 117c5ce..2729c20 100755 --- a/bin/index.js +++ b/bin/index.js @@ -7,9 +7,9 @@ require('update-notifier')({pkg: _pkg}).notify() require('meow')(_pkg) var path = require('path') +var async = require('async') var which = require('which') var semver = require('semver') -var eachAsync = require('each-async') var spawn = require('child_process').spawn var nodeVersions = require('node-versions') @@ -23,12 +23,12 @@ function nodeSwitcher (nodeVersion) { nvm: ['use', nodeVersion] } - function swicher (binary) { - return spawn(binary, binaries[binary], {stdio: 'inherit'}) + function switcher (binary) { + return spawn(binary, binaries[binary], { stdio: 'inherit' }) } - swicher.binaries = Object.keys(binaries) - return swicher + switcher.binaries = function () { return Object.keys(binaries) } + return switcher } var pkg @@ -44,13 +44,23 @@ if (!nodeVersion) processExit() var maxNodeVersion = semver.maxSatisfying(nodeVersions, nodeVersion) var switcher = nodeSwitcher(maxNodeVersion) -var binaries = [] -eachAsync(switcher.binaries, function (binary, index, next) { +var binaries = switcher.binaries() +var switcherBinary = null + +function getBinary (next) { + var binary = binaries.shift() which(binary, function (err) { - if (!err) binaries.push(binary) + if (!err) switcherBinary = binary return next() }) -}, function () { - if (binaries.length) return switcher(binaries.shift()) +} + +function whileCondition () { + return switcherBinary != null && binaries.length !== 0 +} + +async.doWhilst(getBinary, whileCondition, function () { + if (switcherBinary) return switcher(switcherBinary) + return processExit() }) diff --git a/package.json b/package.json index 86708ef..c0ddccc 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "version" ], "dependencies": { - "each-async": "~1.1.1", + "async": "~1.5.2", "meow": "~3.7.0", "node-versions": "~2.1.6", "semver": "~5.1.0",