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,27 @@ 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 (
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
+ }
17
34
}
18
35
19
36
function lazy ( fn ) {
@@ -36,8 +53,7 @@ async function tryCreateGitHubToken(githubAuth) {
36
53
note : 'node-core-utils CLI tools'
37
54
} ) ;
38
55
} 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 } ` ) ;
41
57
}
42
58
return credentials ;
43
59
}
@@ -84,11 +100,11 @@ async function auth(
84
100
if ( options . jenkins ) {
85
101
const { username, jenkins_token } = getMergedConfig ( ) ;
86
102
if ( ! username || ! jenkins_token ) {
87
- process . stdout . write (
103
+ errorExit (
88
104
'Get your Jenkins API token in https://ci.nodejs.org/me/configure ' +
89
105
'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
+ ) ;
92
108
} ;
93
109
check ( username , jenkins_token ) ;
94
110
result . jenkins = encode ( username , jenkins_token ) ;
0 commit comments