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

Commit

Permalink
Merge branch 'refactor2'
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Mar 17, 2016
2 parents 97ffe99 + 10d6b0e commit e792010
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 28 deletions.
39 changes: 11 additions & 28 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,15 @@ require('meow')(_pkg)

var path = require('path')
var async = require('async')
var which = require('which')
var semver = require('semver')
var spawn = require('child_process').spawn
var which = require('./which')
var Switcher = require('./switcher')
var nodeVersions = require('node-versions')

function processExit (code) {
return process.exit(code || 0)
}

function nodeSwitcher (nodeVersion) {
var binaries = {
n: [nodeVersion],
nvm: ['use', nodeVersion]
}

function switcher (binary) {
return spawn(binary, binaries[binary], { stdio: 'inherit' })
}

switcher.binaries = function () { return Object.keys(binaries) }
return switcher
}

var pkg

try {
Expand All @@ -43,24 +29,21 @@ var nodeVersion = pkg.engines && pkg.engines.node
if (!nodeVersion) processExit()

var maxNodeVersion = semver.maxSatisfying(nodeVersions, nodeVersion)
var switcher = nodeSwitcher(maxNodeVersion)

var binaries = switcher.binaries()
var switcherBinary = null
var switcher = Switcher(maxNodeVersion)
var switcherBin = null

function getBinary (next) {
var binary = binaries.shift()
which(binary, function (err) {
if (!err) switcherBinary = binary
function getBin (next) {
var bin = switcher.binaries.shift()
which(bin, function (err) {
if (!err) switcherBin = bin
return next()
})
}

function whileCondition () {
return switcherBinary == null && binaries.length !== 0
return switcherBin == null && switcher.binaries.length !== 0
}

async.doWhilst(getBinary, whileCondition, function () {
if (switcherBinary) return switcher(switcherBinary)
return processExit()
async.doWhilst(getBin, whileCondition, function () {
if (switcherBin) return switcher(switcherBin)
})
21 changes: 21 additions & 0 deletions bin/switcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

var spawn = require('child_process').spawn

function Switcher (version) {
var binArgs = {
n: ['n', version],
nvm: [process.env.SHELL, '-c', 'source $NVM_DIR/nvm.sh; nvm use ' + version]
}

function switcher (bin) {
var args = binArgs[bin]
bin = args.shift()
return spawn(bin, args, { stdio: 'inherit' })
}

switcher.binaries = Object.keys(binArgs)
return switcher
}

module.exports = Switcher
18 changes: 18 additions & 0 deletions bin/which.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

var _which = require('which')
var NVM_DIR = process.env.NVM_DIR

function which (bin, cb) {
var binaries = {
n: function () { return _which(bin, cb) },
nvm: function () {
if (NVM_DIR) return process.nextTick(cb)
process.nextTick(function () { return cb('nope') })
}
}

return binaries[bin]()
}

module.exports = which

0 comments on commit e792010

Please sign in to comment.