2
2
import type { Plugin , ViteDevServer } from 'vite'
3
3
import type { VitePluginLocalOptions } from './types'
4
4
import { exec , spawn } from 'node:child_process'
5
- import { readFileSync } from 'node:fs'
6
- import { join } from 'node:path'
7
5
import process from 'node:process'
8
6
import { promisify } from 'node:util'
9
7
// @ts -expect-error dtsx issues
10
8
import { checkExistingCertificates , checkHosts , cleanup , startProxies } from '@stacksjs/rpx'
11
- import { bold , cyan , dim , green } from 'picocolors'
12
- import packageJson from '../package.json'
9
+ import { bold , cyan , dim , green , yellow } from 'picocolors'
13
10
import { buildConfig } from './utils'
14
11
15
- function getPackageVersions ( ) {
16
- let viteVersion
17
- const vitePluginLocalVersion = packageJson . version
18
- let vitePressVersion
19
-
20
- try {
21
- // Try to get VitePress version first
22
- const vitePressPath = join ( process . cwd ( ) , 'node_modules' , 'vitepress' , 'package.json' )
23
- try {
24
- const vitePressPackage = JSON . parse ( readFileSync ( vitePressPath , 'utf-8' ) )
25
- vitePressVersion = vitePressPackage . version
26
- }
27
- catch { }
28
-
29
- // Get Vite version
30
- const vitePackageJson = readFileSync (
31
- join ( process . cwd ( ) , 'node_modules' , 'vite' , 'package.json' ) ,
32
- 'utf-8' ,
33
- )
34
- viteVersion = JSON . parse ( vitePackageJson ) . version
35
- }
36
- catch {
37
- // Fallback to package.json dependencies
38
- viteVersion = packageJson . devDependencies ?. vite ?. replace ( '^' , '' ) || '0.0.0'
39
- }
40
-
41
- return {
42
- 'vitepress' : vitePressVersion ,
43
- 'vite' : viteVersion ,
44
- 'vite-plugin-local' : vitePluginLocalVersion ,
45
- }
12
+ const toggleComboKeysMap = {
13
+ option : process . platform === 'darwin' ? 'Option(⌥)' : 'Alt(⌥)' ,
14
+ meta : 'Command(⌘)' ,
15
+ shift : 'Shift(⇧)' ,
16
+ }
17
+ function normalizeComboKeyPrint ( toggleComboKey : string ) {
18
+ return toggleComboKey . split ( '-' ) . map ( key => toggleComboKeysMap [ key ] || key [ 0 ] . toUpperCase ( ) + key . slice ( 1 ) ) . join ( dim ( '+' ) )
46
19
}
47
20
48
21
const execAsync = promisify ( exec )
@@ -129,7 +102,6 @@ export function VitePluginLocal(options: VitePluginLocalOptions): Plugin {
129
102
130
103
process . stdout . write ( '\nSudo access required for proxy setup.\n' )
131
104
132
- // Get sudo access before VitePress starts
133
105
hasSudoAccess = await new Promise < boolean > ( ( resolve ) => {
134
106
const sudo = spawn ( 'sudo' , [ 'true' ] , {
135
107
stdio : 'inherit' ,
@@ -226,23 +198,9 @@ export function VitePluginLocal(options: VitePluginLocalOptions): Plugin {
226
198
227
199
server = viteServer
228
200
229
- // Override console.log immediately to prevent VitePress initial messages
230
- const originalLog = console . log
231
- console . log = ( ...args ) => {
232
- if ( typeof args [ 0 ] === 'string' && (
233
- args [ 0 ] . includes ( 'vitepress v' )
234
- || args [ 0 ] . includes ( 'press h to show help' )
235
- ) ) {
236
- return
237
- }
238
- originalLog . apply ( console , args )
239
- }
240
-
241
201
// Store original console for debug
242
202
originalConsole = { ...console }
243
203
244
- server . printUrls = ( ) => { }
245
-
246
204
// Add cleanup handlers for the server
247
205
server . httpServer ?. on ( 'close' , ( ) => {
248
206
debug ( 'Server closing, cleaning up...' )
@@ -254,7 +212,6 @@ export function VitePluginLocal(options: VitePluginLocalOptions): Plugin {
254
212
process . once ( 'SIGTERM' , ( ) => handleSignal ( 'SIGTERM' ) )
255
213
process . once ( 'beforeExit' , ( ) => handleSignal ( 'beforeExit' ) )
256
214
process . once ( 'exit' , async ( ) => {
257
- // If there's a pending cleanup, wait for it
258
215
if ( cleanupPromise ) {
259
216
try {
260
217
await cleanupPromise
@@ -266,60 +223,54 @@ export function VitePluginLocal(options: VitePluginLocalOptions): Plugin {
266
223
}
267
224
} )
268
225
269
- const setupProxy = async ( ) => {
226
+ const colorUrl = ( url : string ) =>
227
+ cyan ( url . replace ( / : ( \d + ) \/ / , ( _ , port ) => `:${ bold ( port ) } /` ) )
228
+
229
+ // Store the original printUrls function
230
+ const originalPrintUrls = server . printUrls
231
+
232
+ // Wrap the printUrls function to add our custom output while preserving other plugins' modifications
233
+ server . printUrls = ( ) => {
234
+ if ( ! server ?. resolvedUrls )
235
+ return
236
+
237
+ // Call the original printUrls function first
238
+ if ( typeof originalPrintUrls === 'function' ) {
239
+ originalPrintUrls . call ( server )
240
+ }
241
+ else {
242
+ // If no other plugin has modified printUrls, print the default local URL
243
+ console . log ( ` ${ green ( '➜' ) } ${ bold ( 'Local' ) } : ${ colorUrl ( server . resolvedUrls . local [ 0 ] ) } ` )
244
+ console . log ( ` ${ green ( '➜' ) } ${ bold ( 'Network' ) } : ${ dim ( 'use --host to expose' ) } ` )
245
+ }
246
+
247
+ // Add our custom proxy URL information
248
+ if ( proxyUrl ) {
249
+ const protocol = options . https ? 'https' : 'http'
250
+ const proxiedUrl = `${ protocol } ://${ proxyUrl } /`
251
+ console . log ( ` ${ green ( '➜' ) } ${ bold ( 'Proxied URL' ) } : ${ colorUrl ( proxiedUrl ) } ` )
252
+
253
+ if ( options . https ) {
254
+ console . log ( ` ${ green ( '➜' ) } ${ bold ( 'SSL' ) } : ${ dim ( 'TLS 1.2/1.3, HTTP/2' ) } ` )
255
+ }
256
+ }
257
+ }
258
+
259
+ const startProxy = async ( ) => {
270
260
try {
271
- const host = typeof server ! . config . server . host === 'boolean'
261
+ const host = typeof server ? .config . server . host === 'boolean'
272
262
? 'localhost'
273
- : server ! . config . server . host || 'localhost'
263
+ : server ? .config . server . host || 'localhost'
274
264
275
- const port = server ! . config . server . port || 5173
265
+ const port = server ? .config . server . port || 5173
276
266
const serverUrl = `${ host } :${ port } `
277
267
278
268
const config = buildConfig ( options , serverUrl )
279
269
domains = [ config . to ]
280
270
proxyUrl = config . to
281
271
282
272
debug ( 'Starting proxies...' )
283
-
284
273
await startProxies ( config )
285
-
286
- server ! . printUrls = function ( ) {
287
- const protocol = options . https ? 'https' : 'http'
288
- const port = server ! . config . server . port || 5173
289
- const localUrl = `http://localhost:${ port } /`
290
- const proxiedUrl = `${ protocol } ://${ proxyUrl } /`
291
- const colorUrl = ( url : string ) =>
292
- cyan ( url . replace ( / : ( \d + ) \/ / , ( _ , port ) => `:${ bold ( port ) } /` ) )
293
-
294
- const versions = getPackageVersions ( )
295
- if ( versions . vitepress ) {
296
- console . log (
297
- `\n ${ bold ( green ( 'vitepress' ) ) } ${ green ( `v${ versions . vitepress } ` ) } ${ dim ( 'via' ) } ${ bold ( green ( 'vite-plugin-local' ) ) } ${ green ( `v${ versions [ 'vite-plugin-local' ] } ` ) } \n` ,
298
- )
299
- }
300
- else {
301
- console . log (
302
- `\n ${ bold ( green ( 'vite' ) ) } ${ green ( `v${ versions . vite } ` ) } ${ dim ( 'via' ) } ${ bold ( green ( 'vite-plugin-local' ) ) } ${ green ( `v${ versions [ 'vite-plugin-local' ] } ` ) } \n` ,
303
- )
304
- }
305
-
306
- console . log ( ` ${ green ( '➜' ) } ${ bold ( 'Local' ) } : ${ colorUrl ( localUrl ) } ` )
307
- console . log ( ` ${ green ( '➜' ) } ${ bold ( 'Proxied' ) } : ${ colorUrl ( proxiedUrl ) } ` )
308
-
309
- if ( options . https ) {
310
- console . log ( ` ${ green ( '➜' ) } ${ bold ( 'SSL' ) } : ${ dim ( 'TLS 1.2/1.3, HTTP/2' ) } ` )
311
- }
312
-
313
- console . log (
314
- dim ( ` ${ green ( '➜' ) } ${ bold ( 'Network' ) } : use ` )
315
- + bold ( '--host' )
316
- + dim ( ' to expose' ) ,
317
- )
318
-
319
- console . log ( `\n ${ green ( dim ( '➜' ) ) } ${ dim ( 'press' ) } ${ bold ( 'h' ) } ${ dim ( 'to show help' ) } \n` )
320
- }
321
-
322
- server ! . printUrls ( )
323
274
debug ( 'Proxy setup complete' )
324
275
}
325
276
catch ( error ) {
@@ -328,10 +279,10 @@ export function VitePluginLocal(options: VitePluginLocalOptions): Plugin {
328
279
}
329
280
}
330
281
331
- server . httpServer ?. once ( 'listening' , setupProxy )
282
+ // Wait for the server to be ready before starting the proxy
283
+ server . httpServer ?. once ( 'listening' , startProxy )
332
284
if ( server . httpServer ?. listening ) {
333
- debug ( 'Server already listening, setting up proxy immediately' )
334
- await setupProxy ( )
285
+ await startProxy ( )
335
286
}
336
287
} ,
337
288
0 commit comments