1
- import process from 'process'
1
+ import { platform } from 'process'
2
2
3
3
import fetch from 'node-fetch'
4
4
import pWaitFor from 'p-wait-for'
@@ -7,7 +7,7 @@ import { v4 as uuidv4 } from 'uuid'
7
7
import { fetchLatestVersion , shouldFetchLatestVersion } from '../lib/exec-fetcher.js'
8
8
import { getPathInHome } from '../lib/settings.js'
9
9
10
- import { NETLIFYDEVERR , NETLIFYDEVLOG , chalk , log } from './command-helpers.js'
10
+ import { NETLIFYDEVERR , NETLIFYDEVLOG , chalk , exit , log } from './command-helpers.js'
11
11
import execa from './execa.js'
12
12
import type CLIState from './cli-state.js'
13
13
@@ -50,8 +50,12 @@ const createTunnel = async function ({
50
50
const data = await response . json ( )
51
51
52
52
if ( response . status !== 201 ) {
53
- // @ts -expect-error(serhalp) -- Use typed `netlify` API client?
54
- throw new Error ( data . message )
53
+ // TODO(serhalp): Use typed `netlify` API client?
54
+ throw new Error (
55
+ data != null && typeof data === 'object' && 'message' in data && typeof data . message === 'string'
56
+ ? data . message
57
+ : '' ,
58
+ )
55
59
}
56
60
57
61
return data as LiveSession
@@ -74,9 +78,9 @@ const connectTunnel = function ({
74
78
}
75
79
76
80
const ps = execa ( execPath , args , { stdio : 'inherit' } )
77
- ps . on ( 'close' , ( code ) => process . exit ( code ?? undefined ) )
78
- ps . on ( 'SIGINT' , process . exit )
79
- ps . on ( 'SIGTERM' , process . exit )
81
+ void ps . on ( 'close' , ( code ) => exit ( code ?? undefined ) )
82
+ void ps . on ( 'SIGINT' , ( ) => exit ( ) )
83
+ void ps . on ( 'SIGTERM' , ( ) => exit ( ) )
80
84
}
81
85
82
86
const installTunnelClient = async function ( ) {
@@ -98,7 +102,7 @@ const installTunnelClient = async function () {
98
102
packageName : PACKAGE_NAME ,
99
103
execName : EXEC_NAME ,
100
104
destination : binPath ,
101
- extension : process . platform === 'win32' ? 'zip' : 'tar.gz' ,
105
+ extension : platform === 'win32' ? 'zip' : 'tar.gz' ,
102
106
} )
103
107
}
104
108
@@ -119,13 +123,20 @@ export const startLiveTunnel = async ({
119
123
'netlify init' ,
120
124
) } or ${ chalk . yellow ( 'netlify link' ) } ?`,
121
125
)
122
- process . exit ( 1 )
126
+ return exit ( 1 )
127
+ }
128
+ if ( ! netlifyApiToken ) {
129
+ console . error (
130
+ `${ NETLIFYDEVERR } Error: no Netlify auth token defined, did you forget to run ${ chalk . yellow (
131
+ 'netlify login' ,
132
+ ) } or define 'NETLIFY_AUTH_TOKEN'?`,
133
+ )
134
+ return exit ( 1 )
123
135
}
124
136
125
137
const session = await createTunnel ( {
126
138
siteId,
127
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- XXX(serhalp): removed in favor of runtime user feedback in stacked PR
128
- netlifyApiToken : netlifyApiToken ! ,
139
+ netlifyApiToken,
129
140
slug,
130
141
} )
131
142
@@ -141,15 +152,18 @@ export const startLiveTunnel = async ({
141
152
const data = await response . json ( )
142
153
143
154
if ( response . status !== 200 ) {
144
- // @ts -expect-error(serhalp) -- Use typed `netlify` API client?
145
- throw new Error ( data . message )
155
+ // TODO(serhalp): Use typed `netlify` API client?
156
+ throw new Error (
157
+ data != null && typeof data === 'object' && 'message' in data && typeof data . message === 'string'
158
+ ? data . message
159
+ : '' ,
160
+ )
146
161
}
147
162
148
163
return ( data as LiveSession ) . state === 'online'
149
164
}
150
165
151
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- XXX(serhalp): removed in favor of runtime user feedback in stacked PR
152
- connectTunnel ( { session, netlifyApiToken : netlifyApiToken ! , localPort } )
166
+ connectTunnel ( { session, netlifyApiToken, localPort } )
153
167
154
168
// Waiting for the live session to have a state of `online`.
155
169
await pWaitFor ( isLiveTunnelReady , {
0 commit comments