@quicktvui/web-cli 1.0.0-beta.14 → 1.0.0-beta.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/qt-web-cli.js CHANGED
@@ -23,7 +23,7 @@ const args = minimist(process.argv.slice(2), {
23
23
  },
24
24
  default: {
25
25
  port: 39001,
26
- open: false,
26
+ open: true,
27
27
  },
28
28
  })
29
29
 
@@ -122,13 +122,22 @@ async function main() {
122
122
 
123
123
  // 构建 webpack 命令
124
124
  const webpackArgs = ['serve', `--config "${webpackConfigPath}"`, `--port ${args.port}`, '--color']
125
- if (args.open) webpackArgs.push('--open')
126
125
 
127
126
  const envPrefix = nodeOptions ? `NODE_OPTIONS="${nodeOptions}" ` : ''
128
127
  const webpackCmd = `${envPrefix}npx webpack ${webpackArgs.join(' ')}`
129
128
 
130
129
  signale.pending(`执行: ${webpackCmd}`)
131
130
 
131
+ // 如果需要打开浏览器,等待服务器就绪后打开
132
+ if (args.open) {
133
+ const url = `http://localhost:${args.port}`
134
+ waitForServer(url, 30000).then(() => {
135
+ openOrRefreshBrowser(url)
136
+ }).catch((err) => {
137
+ signale.warn(`自动打开浏览器失败: ${err.message}`)
138
+ })
139
+ }
140
+
132
141
  const result = exec(webpackCmd, { stdio: 'inherit' })
133
142
  if (result.code !== 0) {
134
143
  signale.error('启动开发服务器失败')
@@ -136,6 +145,80 @@ async function main() {
136
145
  }
137
146
  }
138
147
 
148
+ /**
149
+ * 等待服务器就绪
150
+ */
151
+ function waitForServer(url, timeout = 30000) {
152
+ const http = require('http')
153
+ const startTime = Date.now()
154
+
155
+ return new Promise((resolve, reject) => {
156
+ function check() {
157
+ if (Date.now() - startTime > timeout) {
158
+ reject(new Error('等待服务器超时'))
159
+ return
160
+ }
161
+
162
+ const req = http.get(url, (res) => {
163
+ if (res.statusCode === 200 || res.statusCode === 304) {
164
+ resolve()
165
+ } else {
166
+ setTimeout(check, 500)
167
+ }
168
+ })
169
+
170
+ req.on('error', () => {
171
+ setTimeout(check, 500)
172
+ })
173
+
174
+ req.setTimeout(1000, () => {
175
+ req.destroy()
176
+ setTimeout(check, 500)
177
+ })
178
+ }
179
+
180
+ check()
181
+ })
182
+ }
183
+
184
+ /**
185
+ * 打开或刷新浏览器(支持 macOS)
186
+ */
187
+ function openOrRefreshBrowser(url) {
188
+ const platform = process.platform
189
+
190
+ if (platform === 'darwin') {
191
+ // macOS: 使用 AppleScript 检查并刷新已存在的标签页
192
+ const appleScript = `
193
+ tell application "Safari"
194
+ activate
195
+ set found to false
196
+ repeat with w in windows
197
+ repeat with t in tabs of w
198
+ if URL of t starts with "http://localhost:" then
199
+ set URL of t to "${url}"
200
+ set found to true
201
+ exit repeat
202
+ end if
203
+ end repeat
204
+ if found then exit repeat
205
+ end repeat
206
+ if not found then
207
+ open location "${url}"
208
+ end if
209
+ end tell
210
+ `
211
+
212
+ exec(`osascript -e '${appleScript.replace(/\n/g, ' ')}'`, { silent: true })
213
+ signale.success('已打开/刷新浏览器')
214
+ } else {
215
+ // 其他平台:直接打开 URL
216
+ const openCmd = platform === 'win32' ? 'start' : 'xdg-open'
217
+ exec(`${openCmd} "${url}"`, { silent: true })
218
+ signale.success('已打开浏览器')
219
+ }
220
+ }
221
+
139
222
  function findProjectRoot(startDir = process.cwd()) {
140
223
  let currentDir = startDir
141
224
  while (currentDir !== path.dirname(currentDir)) {
@@ -66,6 +66,20 @@ function detectModules() {
66
66
  if (fs.existsSync(cwdNodeModules) && !modules.includes(cwdNodeModules)) {
67
67
  modules.push(cwdNodeModules)
68
68
  }
69
+ // 添加 web-cli 自身的 node_modules 路径(支持全局安装)
70
+ const cliNodeModules = path.resolve(__dirname, '../node_modules')
71
+ if (fs.existsSync(cliNodeModules) && !modules.includes(cliNodeModules)) {
72
+ modules.push(cliNodeModules)
73
+ }
74
+ // 添加全局 node_modules 路径
75
+ try {
76
+ const globalNodeModules = require('child_process')
77
+ .execSync('npm root -g', { encoding: 'utf-8' })
78
+ .trim()
79
+ if (fs.existsSync(globalNodeModules) && !modules.includes(globalNodeModules)) {
80
+ modules.push(globalNodeModules)
81
+ }
82
+ } catch (e) {}
69
83
  return modules
70
84
  }
71
85
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quicktvui/web-cli",
3
- "version": "1.0.0-beta.14",
3
+ "version": "1.0.0-beta.16",
4
4
  "description": "CLI tool for QuickTVUI web development - zero configuration",
5
5
  "author": "QuickTVUI Team",
6
6
  "license": "Apache-2.0",