Skip to content
This repository has been archived by the owner on May 2, 2023. It is now read-only.

Commit

Permalink
Improve algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Mar 16, 2016
1 parent c397b68 commit 9e217f9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 20 additions & 10 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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
Expand All @@ -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()
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 9e217f9

Please sign in to comment.