diff --git a/lib/auth.js b/lib/auth.js index 79275843..6e6fb3ef 100644 --- a/lib/auth.js +++ b/lib/auth.js @@ -7,26 +7,21 @@ import { clearCachedConfig, encryptValue, getMergedConfig, getNcurcPath } from ' export default lazy(auth); -function errorExit(message) { - process.stderr.write(`${message}\n`); - process.exit(1); -} - function check(username, token, format = /^[A-Za-z0-9_]+$/) { if (typeof username !== 'string') { - errorExit(`username must be a string, received ${typeof username}`); + throw new Error(`username must be a string, received ${typeof username}`); } if (!/^[a-zA-Z0-9-]+$/.test(username)) { - errorExit( + throw new Error( 'username may only contain alphanumeric characters or hyphens, ' + `received ${username}` ); } if (typeof token !== 'string') { - errorExit(`token must be a string, received ${typeof token}`); + throw new Error(`token must be a string, received ${typeof token}`); } if (!format.test(token)) { - errorExit(`token is misformatted: ${token}`); + throw new Error(`token is misformatted: ${token}`); } } @@ -51,7 +46,7 @@ async function tryCreateGitHubToken(githubAuth) { noDeviceFlow: true }); } catch (e) { - errorExit(`Could not get token: ${e.message}`); + throw new Error(`Could not get token: ${e.message}`, { cause: e }); } return credentials; } @@ -93,7 +88,7 @@ async function auth( get jenkins() { const { username, jenkins_token } = getMergedConfig(); if (!username || !jenkins_token) { - errorExit( + throw new Error( 'Get your Jenkins API token in https://ci.nodejs.org/me/security ' + 'and run the following command to add it to your ncu config: ' + 'ncu-config --global set -x jenkins_token' diff --git a/test/unit/auth.test.js b/test/unit/auth.test.js index db5119ef..653f62fb 100644 --- a/test/unit/auth.test.js +++ b/test/unit/auth.test.js @@ -62,7 +62,7 @@ describe('auth', async function() { await runAuthScript( {}, [FIRST_TIME_MSG], - 'Could not get token: Bad credentials\n', 'run-auth-error' + /Could not get token: Bad credentials\n/, 'run-auth-error' ); }); @@ -70,7 +70,7 @@ describe('auth', async function() { await runAuthScript( { HOME: { username: {}, token: '0123456789abcdef' } }, [], - 'username must be a string, received object\n' + /username must be a string, received object\n/ ); }); @@ -78,7 +78,7 @@ describe('auth', async function() { await runAuthScript( { HOME: { username: 'nyancat', token: 42 } }, [], - 'token must be a string, received number\n' + /token must be a string, received number\n/ ); }); @@ -86,8 +86,7 @@ describe('auth', async function() { await runAuthScript( { HOME: { username: ' ^^^ ', token: '0123456789abcdef' } }, [], - 'username may only contain alphanumeric characters or hyphens, ' + - 'received ^^^ \n' + /username may only contain alphanumeric characters or hyphens, received {2}\^\^\^ \n/ ); }); @@ -95,7 +94,7 @@ describe('auth', async function() { await runAuthScript( { HOME: { username: 'nyancat', token: '@fhqwhgads' } }, [], - 'token is misformatted: @fhqwhgads\n' + /token is misformatted: @fhqwhgads\n/ ); });