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

Commit

Permalink
Improve non detection & docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed May 14, 2017
1 parent 97110b8 commit 4ef3d99
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 34 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ popd () { builtin popd "$@" && chpwd; }

chpwd () {
local PKG

PKG=$PWD/package.json
if [ -f "$PKG" ] && [ "$NODENGINE_LAST_DIR" != "$PWD" ]; then
nodengine
Expand All @@ -64,7 +63,7 @@ chpwd () {

The program use a local cache for avoid fetching versions of node all the time.

By default, new versions of node will be fetch each 5 days.
By default, new versions of node will be fetched after 5 days.

You can setup the interval using `NODENGINE_INTERVAL`

Expand Down
2 changes: 1 addition & 1 deletion bin/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

var FIVE_DAYS = 1000 * 60 * 60 * 24 * 5
var pkg = require('../package.json')
var FIVE_DAYS = 1000 * 60 * 60 * 24 * 5

require('update-notifier')({pkg: pkg}).notify()

Expand Down
2 changes: 1 addition & 1 deletion bin/fetch.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

var URL = 'https://semver.io/node/versions'
var get = require('simple-get')
var URL = 'https://semver.io/node/versions'

function fetch (cb) {
get.concat(URL, function (err, res, data) {
Expand Down
24 changes: 24 additions & 0 deletions bin/help.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
When you run the command, automagically it switches your node runtime
based in the `engines.node` field declared at `package.json`
of the project.

If the `package.json` or `engines.node` doesn't exists, nothing happens.

Usage
$ nodengine

Flags
NODENGINE_INTERVAL
Local cache for keep a list of all node versions to be resolved.

The list, by default, is fetched after 5 days.

You can setup a custom intervaling passing a milliseconds number.
For example, if you want to force download the list of all node
versions just pass a 0:

NODENGINE_INTERVAL=0 nodengine

Examples
$ nodengine
$ NODENGINE_INTERVAL=0 nodengine
38 changes: 9 additions & 29 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,16 @@

'use strict'

require('meow')(require('../package.json'))

var waterfall = require('async').waterfall
var semver = require('semver')
var path = require('path')
var fs = require('fs')

var config = require('./config')
var createSwitcher = require('./switcher')

var pkg = require(path.resolve('package.json'))
var rangeVersion = pkg.engines && pkg.engines.node

if (!rangeVersion) process.exit()

var tasks = [
function loadConfig (next) {
return config(next)
},
function getSwitcher (versions, next) {
var currentVersion = process.versions.node
var maxSatisfyVersion = semver.maxSatisfying(versions, rangeVersion)
var switcher = createSwitcher(maxSatisfyVersion, currentVersion)
require('meow')({
pkg: require(path.resolve(__dirname, '../package.json')),
help: fs.readFileSync(path.resolve(__dirname, './help.txt'), 'utf8')
})

switcher.getBin(function (err, bin) {
return next(err, switcher, bin)
})
}
]
var pkgPath = path.resolve('package.json')
var pkg = fs.existsSync(pkgPath) ? require(pkgPath) : {}
var nodeVersion = pkg.engines && pkg.engines.node

waterfall(tasks, function (err, switcher, bin) {
if (err) throw err
if (bin) return switcher.spawn(bin)
})
!nodeVersion ? process.exit() : require('./switch')(nodeVersion)
31 changes: 31 additions & 0 deletions bin/switch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

var waterfall = require('async').waterfall
var semver = require('semver')

function _switch (nodeVersion) {
var createSwitcher = require('./switcher')
var config = require('./config')

var tasks = [
function loadConfig (next) {
return config(next)
},
function getSwitcher (versions, next) {
var currentVersion = process.versions.node
var maxSatisfyVersion = semver.maxSatisfying(versions, nodeVersion)
var switcher = createSwitcher(maxSatisfyVersion, currentVersion)

switcher.getBin(function (err, bin) {
return next(err, switcher, bin)
})
}
]

waterfall(tasks, function (err, switcher, bin) {
if (err) throw err
if (bin) return switcher.spawn(bin)
})
}

module.exports = _switch
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nodengine",
"description": "NodeJS switcher based on package.json.",
"description": "NodeJS switcher based on node version declared in `package.json`.",
"homepage": "https://github.com/Kikobeats/nodengine",
"version": "3.1.0",
"bin": {
Expand Down

0 comments on commit 4ef3d99

Please sign in to comment.