1
- import assert from 'node:assert' ;
2
1
import fs from 'node:fs' ;
3
2
import { ClientRequest } from 'node:http' ;
4
3
import util from 'node:util' ;
@@ -11,9 +10,24 @@ const ghauth = util.promisify(ghauthBase);
11
10
12
11
export default lazy ( auth ) ;
13
12
13
+ function errorExit ( message ) {
14
+ process . stderr . write ( `${ message } \n` ) ;
15
+ process . exit ( 1 ) ;
16
+ }
17
+
14
18
function 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 ( `username must be alphanumeric, received ${ username } ` ) ;
24
+ }
25
+ if ( typeof token !== 'string' ) {
26
+ errorExit ( `token must be a string, received ${ typeof token } ` ) ;
27
+ }
28
+ if ( ! / ^ [ 0 - 9 a - f ] + $ / . test ( token ) ) {
29
+ errorExit ( `token must be lowercase hexadecimal, received ${ token } ` ) ;
30
+ }
17
31
}
18
32
19
33
function lazy ( fn ) {
@@ -36,8 +50,7 @@ async function tryCreateGitHubToken(githubAuth) {
36
50
note : 'node-core-utils CLI tools'
37
51
} ) ;
38
52
} catch ( e ) {
39
- process . stderr . write ( `Could not get token: ${ e . message } \n` ) ;
40
- process . exit ( 1 ) ;
53
+ errorExit ( `Could not get token: ${ e . message } ` ) ;
41
54
}
42
55
return credentials ;
43
56
}
@@ -84,11 +97,11 @@ async function auth(
84
97
if ( options . jenkins ) {
85
98
const { username, jenkins_token } = getMergedConfig ( ) ;
86
99
if ( ! username || ! jenkins_token ) {
87
- process . stdout . write (
100
+ errorExit (
88
101
'Get your Jenkins API token in https://ci.nodejs.org/me/configure ' +
89
102
'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 ) ;
103
+ 'ncu-config --global set jenkins_token TOKEN'
104
+ ) ;
92
105
} ;
93
106
check ( username , jenkins_token ) ;
94
107
result . jenkins = encode ( username , jenkins_token ) ;
0 commit comments