Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw error instead of using process.exit() #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,25 @@ module.exports = function pleaseUpgradeNode(pkg, opts) {
var requiredVersion = pkg.engines.node.replace('>=', '')
var currentVersion = process.version.replace('v', '')
if (semverCompare(currentVersion, requiredVersion) === -1) {
let errMsg
if (opts.message) {
console.error(opts.message(requiredVersion))
errMsg = opts.message(requiredVersion)
} else {
console.error(
pkg.name +
errMsg = pkg.name +
' requires at least version ' +
requiredVersion +
' of Node, please upgrade'
)

}

console.error(errMsg)

if (opts.hasOwnProperty('exitCode')) {
process.exit(opts.exitCode)
process.exitCode = opts.exitCode
} else {
process.exit(1)
process.exitCode = 1
}

throw new Error(errMsg)
}
}
150 changes: 81 additions & 69 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ var pleaseUpgrade = require('./')
// Change process.version value
Object.defineProperty(process, 'version', { value: 'v10.0.0' })

// Mock process.exit and console.error
var exitCode = null
process.exit = function(code) {
exitCode = code
}

var errorMessage = null
consoleError = console.error
console.error = function(msg) {
Expand All @@ -19,87 +13,96 @@ console.error = function(msg) {

function test(name, cb) {
// Before each
exitCode = null
process.exitCode = null
errorMessage = null
// Test
tape(name, cb)
}

// Actual tests
test('>=1.2.0 should not exit', function(t) {
test('>=1.2.0 should not throw', function(t) {
pleaseUpgrade({
name: 'Lorem Ipsum',
engines: {
node: '>=1.2.0'
}
})
t.equal(exitCode, null)
t.equal(process.exitCode, null)
t.equal(errorMessage, null)
t.end()
})

test('>=4 should not exit', function(t) {
test('>=4 should not throw', function(t) {
pleaseUpgrade({
name: 'Lorem Ipsum',
engines: {
node: '>=4'
}
})
t.equal(exitCode, null)
t.equal(process.exitCode, null)
t.equal(errorMessage, null)
t.end()
})

test('>=4.0.0 should not exit', function(t) {
test('>=4.0.0 should not throw', function(t) {
pleaseUpgrade({
name: 'Lorem Ipsum',
engines: {
node: '>=4.0.0'
}
})
t.equal(exitCode, null)
t.equal(process.exitCode, null)
t.equal(errorMessage, null)
t.end()
})

test('>=10.0.1 (patch) should exit', function(t) {
pleaseUpgrade({
name: 'Lorem Ipsum',
engines: {
node: '>=10.0.1'
}
test('>=10.0.1 (patch) should throw', function(t) {
t.throws(function() {
pleaseUpgrade({
name: 'Lorem Ipsum',
engines: {
node: '>=10.0.1'
}
})
})
t.equal(exitCode, 1)

t.equal(process.exitCode, 1)
t.equal(
errorMessage,
'Lorem Ipsum requires at least version 10.0.1 of Node, please upgrade'
)
t.end()
})

test('>=12 should exit', function(t) {
pleaseUpgrade({
name: 'Lorem Ipsum',
engines: {
node: '>=12'
}
test('>=12 should throw', function(t) {
t.throws(function() {
pleaseUpgrade({
name: 'Lorem Ipsum',
engines: {
node: '>=12'
}
})
})
t.equal(exitCode, 1)

t.equal(process.exitCode, 1)
t.equal(
errorMessage,
'Lorem Ipsum requires at least version 12 of Node, please upgrade'
)
t.end()
})

test('>=12.0.0 should exit', function(t) {
pleaseUpgrade({
name: 'Lorem Ipsum',
engines: {
node: '>=12.0.0'
}
test('>=12.0.0 should throw', function(t) {
t.throws(function() {
pleaseUpgrade({
name: 'Lorem Ipsum',
engines: {
node: '>=12.0.0'
}
})
})
t.equal(exitCode, 1)

t.equal(process.exitCode, 1)
t.equal(
errorMessage,
'Lorem Ipsum requires at least version 12.0.0 of Node, please upgrade'
Expand All @@ -108,18 +111,21 @@ test('>=12.0.0 should exit', function(t) {
})

test('should exit with custom code 0', function(t) {
pleaseUpgrade(
{
name: 'Lorem Ipsum',
engines: {
node: '>=12.0.0'
t.throws(function() {
pleaseUpgrade(
{
name: 'Lorem Ipsum',
engines: {
node: '>=12.0.0'
}
},
{
exitCode: 0
}
},
{
exitCode: 0
}
)
t.equal(exitCode, 0)
)
})

t.equal(process.exitCode, 0)
t.equal(
errorMessage,
'Lorem Ipsum requires at least version 12.0.0 of Node, please upgrade'
Expand All @@ -128,18 +134,21 @@ test('should exit with custom code 0', function(t) {
})

test('should exit with custom code 2', function(t) {
pleaseUpgrade(
{
name: 'Lorem Ipsum',
engines: {
node: '>=12.0.0'
t.throws(function() {
pleaseUpgrade(
{
name: 'Lorem Ipsum',
engines: {
node: '>=12.0.0'
}
},
{
exitCode: 2
}
},
{
exitCode: 2
}
)
t.equal(exitCode, 2)
)
})

t.equal(process.exitCode, 2)
t.equal(
errorMessage,
'Lorem Ipsum requires at least version 12.0.0 of Node, please upgrade'
Expand All @@ -148,20 +157,23 @@ test('should exit with custom code 2', function(t) {
})

test('should display custom message', function(t) {
pleaseUpgrade(
{
name: 'Lorem Ipsum',
engines: {
node: '>=12.0.0'
}
},
{
message: function(requiredVersion) {
return 'Required version is ' + requiredVersion
t.throws(function() {
pleaseUpgrade(
{
name: 'Lorem Ipsum',
engines: {
node: '>=12.0.0'
}
},
{
message: function(requiredVersion) {
return 'Required version is ' + requiredVersion
}
}
}
)
t.equal(exitCode, 1)
)
})

t.equal(process.exitCode, 1)
t.equal(errorMessage, 'Required version is 12.0.0')
t.end()
})