1- import assert from 'node:assert' ;
21import fs from 'node:fs' ;
32import { ClientRequest } from 'node:http' ;
43import util from 'node:util' ;
@@ -11,9 +10,27 @@ const ghauth = util.promisify(ghauthBase);
1110
1211export default lazy ( auth ) ;
1312
13+ function errorExit ( message ) {
14+ process . stderr . write ( `${ message } \n` ) ;
15+ process . exit ( 1 ) ;
16+ }
17+
1418function check ( username , token ) {
15- assert ( typeof username === 'string' && / ^ [ a - z A - Z 0 - 9 ] * / . test ( username ) ) ;
16- assert ( typeof token === 'string' && / ^ [ 0 - 9 a - f ] * / . test ( token ) ) ;
19+ if ( typeof username !== 'string' ) {
20+ errorExit ( `username must be a string, received ${ typeof username } ` ) ;
21+ }
22+ if ( ! / ^ [ a - z A - Z 0 - 9 - ] + $ / . test ( username ) ) {
23+ errorExit (
24+ 'username may only contain alphanumeric characters or hyphens, ' +
25+ `received ${ username } `
26+ ) ;
27+ }
28+ if ( typeof token !== 'string' ) {
29+ errorExit ( `token must be a string, received ${ typeof token } ` ) ;
30+ }
31+ if ( ! / ^ [ 0 - 9 a - f ] + $ / . test ( token ) ) {
32+ errorExit ( `token must be lowercase hexadecimal, received ${ token } ` ) ;
33+ }
1734}
1835
1936function lazy ( fn ) {
@@ -36,8 +53,7 @@ async function tryCreateGitHubToken(githubAuth) {
3653 note : 'node-core-utils CLI tools'
3754 } ) ;
3855 } catch ( e ) {
39- process . stderr . write ( `Could not get token: ${ e . message } \n` ) ;
40- process . exit ( 1 ) ;
56+ errorExit ( `Could not get token: ${ e . message } ` ) ;
4157 }
4258 return credentials ;
4359}
@@ -84,11 +100,11 @@ async function auth(
84100 if ( options . jenkins ) {
85101 const { username, jenkins_token } = getMergedConfig ( ) ;
86102 if ( ! username || ! jenkins_token ) {
87- process . stdout . write (
103+ errorExit (
88104 'Get your Jenkins API token in https://ci.nodejs.org/me/configure ' +
89105 'and run the following command to add it to your ncu config: ' +
90- 'ncu-config --global set jenkins_token TOKEN\n' ) ;
91- process . exit ( 1 ) ;
106+ 'ncu-config --global set jenkins_token TOKEN'
107+ ) ;
92108 } ;
93109 check ( username , jenkins_token ) ;
94110 result . jenkins = encode ( username , jenkins_token ) ;
0 commit comments