Skip to content
Open
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
17 changes: 6 additions & 11 deletions lib/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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'
Expand Down
Loading