Skip to content

Commit

Permalink
Throw error instead of using process.exit()
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Torreblanca authored and Bryan Torreblanca committed Oct 3, 2022
1 parent 0700f41 commit a134d95
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 100 deletions.
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)
}
}
169 changes: 75 additions & 94 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,10 @@ 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) {
errorMessage = msg
consoleError(msg)
}

function test(name, cb) {
// Before each
exitCode = null
errorMessage = null
process.exitCode = null
// Test
tape(name, cb)
}
Expand All @@ -33,8 +20,7 @@ test('>=1.2.0 should not exit', function(t) {
node: '>=1.2.0'
}
})
t.equal(exitCode, null)
t.equal(errorMessage, null)
t.equal(process.exitCode, null)
t.end()
})

Expand All @@ -45,8 +31,7 @@ test('>=4 should not exit', function(t) {
node: '>=4'
}
})
t.equal(exitCode, null)
t.equal(errorMessage, null)
t.equal(process.exitCode, null)
t.end()
})

Expand All @@ -57,111 +42,107 @@ test('>=4.0.0 should not exit', function(t) {
node: '>=4.0.0'
}
})
t.equal(exitCode, null)
t.equal(errorMessage, null)
t.equal(process.exitCode, null)
t.end()
})

test('>=10.0.1 (patch) should exit', function(t) {
pleaseUpgrade({
name: 'Lorem Ipsum',
engines: {
node: '>=10.0.1'
}
t.throws(function() {
pleaseUpgrade({
name: 'Lorem Ipsum',
engines: {
node: '>=10.0.1'
}
})
})
t.equal(exitCode, 1)
t.equal(
errorMessage,
'Lorem Ipsum requires at least version 10.0.1 of Node, please upgrade'
)

t.equal(process.exitCode, 1)
t.end()
})

test('>=12 should exit', function(t) {
pleaseUpgrade({
name: 'Lorem Ipsum',
engines: {
node: '>=12'
}
t.throws(function() {
pleaseUpgrade({
name: 'Lorem Ipsum',
engines: {
node: '>=12'
}
})
})
t.equal(exitCode, 1)
t.equal(
errorMessage,
'Lorem Ipsum requires at least version 12 of Node, please upgrade'
)

t.equal(process.exitCode, 1)
t.end()
})

test('>=12.0.0 should exit', function(t) {
pleaseUpgrade({
name: 'Lorem Ipsum',
engines: {
node: '>=12.0.0'
}
t.throws(function() {
pleaseUpgrade({
name: 'Lorem Ipsum',
engines: {
node: '>=12.0.0'
}
})
})
t.equal(exitCode, 1)
t.equal(
errorMessage,
'Lorem Ipsum requires at least version 12.0.0 of Node, please upgrade'
)

t.equal(process.exitCode, 1)
t.end()
})

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(
errorMessage,
'Lorem Ipsum requires at least version 12.0.0 of Node, please upgrade'
)
)
})

t.equal(process.exitCode, 0)
t.end()
})

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(
errorMessage,
'Lorem Ipsum requires at least version 12.0.0 of Node, please upgrade'
)
)
})

t.equal(process.exitCode, 2)
t.end()
})

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(errorMessage, 'Required version is 12.0.0')
)
})

t.equal(process.exitCode, 1)
t.end()
})

0 comments on commit a134d95

Please sign in to comment.