@@ -5,7 +5,10 @@ import type { RsbuildPluginAPI } from '@rsbuild/core'
55
66import type { ExposedAPI } from '@lynx-js/rspeedy'
77
8- import generateDevUrls from './generateDevUrls.js'
8+ import {
9+ generateExplorerDevUrls ,
10+ generateWebDevUrls ,
11+ } from './generateDevUrls.js'
912
1013import type { CustomizedSchemaFn } from './index.js'
1114
@@ -14,13 +17,16 @@ const gExistingShortcuts = new WeakSet<Options>()
1417interface Options {
1518 api : RsbuildPluginAPI
1619 entries : string [ ]
20+ webEntries : string [ ]
1721 schema : CustomizedSchemaFn
1822 port : number
1923 customShortcuts ?: Record <
2024 string ,
2125 { value : string , label : string , hint ?: string , action ?( ) : Promise < void > }
2226 >
23- onPrint ?: ( ( url : string ) => Promise < void > ) | undefined
27+ onPrint ?:
28+ | ( ( lynx ?: string , web ?: Record < string , string > ) => Promise < void > )
29+ | undefined
2430}
2531
2632export async function registerConsoleShortcuts (
@@ -32,22 +38,35 @@ export async function registerConsoleShortcuts(
3238 import ( './showQRCode.js' ) ,
3339 ] )
3440
35- const currentEntry = options . entries [ 0 ] !
36- const devUrls = generateDevUrls (
41+ // keep the default entry to be lynx explorer entry if exists
42+ const currentEntry = options . entries [ 0 ]
43+ const devUrls = currentEntry
44+ ? generateExplorerDevUrls (
45+ options . api ,
46+ currentEntry ,
47+ options . schema ,
48+ options . port ,
49+ )
50+ : undefined
51+
52+ const value : string | symbol | undefined = devUrls
53+ ? Object . values ( devUrls ) [ 0 ]
54+ : undefined
55+ const webUrls = generateWebDevUrls (
3756 options . api ,
38- currentEntry ,
39- options . schema ,
57+ options . webEntries ,
4058 options . port ,
4159 )
42-
43- const value : string | symbol = Object . values ( devUrls ) [ 0 ] !
44- await options . onPrint ?.( value )
45- showQRCode ( value )
60+ await options . onPrint ?.(
61+ value ,
62+ webUrls ,
63+ )
64+ showQRCode ( value , webUrls )
4665
4766 gExistingShortcuts . add ( options )
4867
4968 // We should not `await` on this since it would block the NodeJS main thread.
50- void loop ( options , value , devUrls )
69+ void loop ( options , value , devUrls , webUrls )
5170
5271 function off ( ) {
5372 gExistingShortcuts . delete ( options )
@@ -57,8 +76,9 @@ export async function registerConsoleShortcuts(
5776
5877async function loop (
5978 options : Options ,
60- value : string | symbol ,
61- devUrls : Record < string , string > ,
79+ value : string | symbol | undefined ,
80+ devUrls : Record < string , string > | undefined ,
81+ webUrls : Record < string , string > ,
6282) {
6383 const [
6484 { autocomplete, select, selectKey, isCancel, cancel } ,
@@ -70,8 +90,8 @@ async function loop(
7090
7191 const selectFn = ( length : number ) => length > 5 ? autocomplete : select
7292
73- let currentEntry = options . entries [ 0 ] !
74- let currentSchema = Object . keys ( devUrls ) [ 0 ] !
93+ let currentEntry = options . entries [ 0 ]
94+ let currentSchema = devUrls ? Object . keys ( devUrls ) [ 0 ] ! : undefined
7595
7696 while ( ! isCancel ( value ) ) {
7797 const name = await selectKey ( {
@@ -94,18 +114,18 @@ async function loop(
94114 ) {
95115 break
96116 }
97- if ( name === 'r' ) {
117+ if ( name === 'r' && currentSchema ) {
98118 const selection = await selectFn ( options . entries . length ) ( {
99119 message : 'Select entry' ,
100120 options : options . entries . map ( entry => ( {
101121 value : entry ,
102122 label : entry ,
103- hint : generateDevUrls (
123+ hint : generateExplorerDevUrls (
104124 options . api ,
105125 entry ,
106126 options . schema ,
107127 options . port ,
108- ) [ currentSchema ] ! ,
128+ ) [ currentSchema ! ] ! ,
109129 } ) ) ,
110130 initialValue : currentEntry ,
111131 } )
@@ -114,8 +134,8 @@ async function loop(
114134 }
115135 currentEntry = selection
116136 value = getCurrentUrl ( )
117- } else if ( name === 'a' ) {
118- const devUrls = generateDevUrls (
137+ } else if ( name === 'a' && currentEntry ) {
138+ const devUrls = generateExplorerDevUrls (
119139 options . api ,
120140 currentEntry ,
121141 options . schema ,
@@ -138,8 +158,8 @@ async function loop(
138158 } else if ( options . customShortcuts ?. [ name ] ) {
139159 await options . customShortcuts [ name ] . action ?.( )
140160 }
141- await options . onPrint ?.( value )
142- showQRCode ( value )
161+ await options . onPrint ?.( value , webUrls )
162+ showQRCode ( value , webUrls )
143163 }
144164
145165 // If the `options` is not deleted from `gExistingShortcuts`, means that this is an explicitly
@@ -152,12 +172,14 @@ async function loop(
152172 return
153173
154174 function getCurrentUrl ( ) : string {
155- return generateDevUrls (
156- options . api ,
157- currentEntry ,
158- options . schema ,
159- options . port ,
160- ) [ currentSchema ] !
175+ return ( currentEntry && currentSchema )
176+ ? generateExplorerDevUrls (
177+ options . api ,
178+ currentEntry ,
179+ options . schema ,
180+ options . port ,
181+ ) [ currentSchema ] !
182+ : ''
161183 }
162184
163185 function exit ( code ?: number ) {
0 commit comments