wxa-runner 0.6.0 → 0.8.0
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/wxa-runner.js +45 -7
- package/package.json +4 -3
package/bin/wxa-runner.js
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
// 先于加载依赖检查:npm / npx 对 engines 只警告,低版本照样能启动,到用 fetch 等时才报看不懂的错
|
|
4
|
+
const requiredNode = Number(/\d+/.exec(require('../package.json').engines.node)[0]);
|
|
5
|
+
if (Number(process.versions.node.split('.')[0]) < requiredNode) {
|
|
6
|
+
console.error(`wxa-runner 需要 Node.js ${requiredNode} 及以上,当前是 ${process.versions.node}。请先切换 Node.js 版本,如 nvm use 22`);
|
|
7
|
+
process.exit(1);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const { Command, Option } = require('commander');
|
|
4
11
|
const chalk = require('chalk');
|
|
5
12
|
const path = require('path');
|
|
6
13
|
const { spawn } = require('child_process');
|
|
@@ -15,32 +22,45 @@ program
|
|
|
15
22
|
program
|
|
16
23
|
.command('dev <project-path>')
|
|
17
24
|
.description('启动开发服务器')
|
|
18
|
-
.option('-p, --port <port>', '
|
|
19
|
-
.option('--host
|
|
25
|
+
.option('-p, --port <port>', '端口号(默认 3000,被占用时换用后面空闲的端口)')
|
|
26
|
+
.option('--host [host]', '监听地址,不带值时监听所有地址,局域网也能访问', 'localhost')
|
|
20
27
|
.option('-b, --baselib <version>', '基础库版本(默认使用本机已安装的最新兼容版本)')
|
|
21
28
|
.option('--setup <file>', '小程序启动前执行的 setup 文件(默认使用项目目录下的 wxa.setup.ts / .js / .mjs)')
|
|
22
29
|
.option('--no-setup', '不执行 setup 文件')
|
|
23
30
|
.option('--no-watch', '不监听文件变化')
|
|
24
31
|
.option('-o, --open', '启动后打开浏览器')
|
|
25
|
-
.option('--
|
|
32
|
+
.option('--forward-console', '浏览器里的 console.warn / console.error 也转发到终端(检测到编程助手时默认开启)')
|
|
33
|
+
.option('--no-forward-console', '浏览器里的报错不转发到终端')
|
|
34
|
+
.option('--devtools-login', '借本机微信开发者工具的登录态访问微信后台:真实的 code 和手机号、服务市场、插件')
|
|
35
|
+
// 旧名,开发服务器会提示改用 --devtools-login
|
|
36
|
+
.addOption(new Option('--real-login').hideHelp())
|
|
26
37
|
.action(async (projectPath, options) => {
|
|
27
38
|
const { DevServer } = require('@open-wxa/wxa-runner-core');
|
|
28
39
|
|
|
29
40
|
try {
|
|
41
|
+
const forward = await resolveForwardConsole(options.forwardConsole);
|
|
30
42
|
const server = new DevServer({
|
|
31
43
|
projectPath: path.resolve(process.cwd(), projectPath),
|
|
32
|
-
port: parseInt(options.port, 10),
|
|
33
|
-
host: options.host,
|
|
44
|
+
port: options.port === undefined ? undefined : parseInt(options.port, 10),
|
|
45
|
+
host: options.host === true ? '0.0.0.0' : options.host,
|
|
34
46
|
baseLibVersion: options.baselib,
|
|
35
47
|
setup: options.setup,
|
|
36
48
|
watch: options.watch,
|
|
37
|
-
|
|
49
|
+
devtoolsLogin: options.devtoolsLogin,
|
|
50
|
+
realLogin: options.realLogin,
|
|
51
|
+
forwardConsole: forward.mode
|
|
38
52
|
});
|
|
39
53
|
const url = await server.start();
|
|
40
54
|
|
|
41
55
|
console.log('');
|
|
42
56
|
console.log(chalk.green(' wxa-runner 已启动'));
|
|
43
57
|
console.log(` 地址 ${chalk.cyan(url)}`);
|
|
58
|
+
const networkUrls = server.networkUrls;
|
|
59
|
+
if (networkUrls.length === 0) {
|
|
60
|
+
console.log(` 局域网 ${chalk.gray('加 --host 开放局域网访问')}`);
|
|
61
|
+
}
|
|
62
|
+
networkUrls.forEach((networkUrl, i) => console.log(` ${i === 0 ? '局域网' : ' '} ${chalk.cyan(networkUrl)}`));
|
|
63
|
+
console.log(` 控制台 ${describeForwardConsole(forward)}`);
|
|
44
64
|
console.log(` 项目 ${server.project.root}`);
|
|
45
65
|
console.log(` 基础库 ${server.baseLib.version}${server.baseLib.source === 'bundled' ? '(内置)' : ''}`);
|
|
46
66
|
if (server.setupFile) console.log(` setup ${server.setupFile}`);
|
|
@@ -91,6 +111,24 @@ baselib
|
|
|
91
111
|
}
|
|
92
112
|
});
|
|
93
113
|
|
|
114
|
+
/**
|
|
115
|
+
* 没指定 --forward-console / --no-forward-console 时:编程助手自己启动开发服务器时只看得到终端,
|
|
116
|
+
* console.warn / console.error 也转发;人在自己的终端里启动时用浏览器的开发者工具看,只转发报错
|
|
117
|
+
*/
|
|
118
|
+
async function resolveForwardConsole(option) {
|
|
119
|
+
if (option === false) return { mode: false };
|
|
120
|
+
if (option === true) return { mode: 'all' };
|
|
121
|
+
const { determineAgent } = require('@vercel/detect-agent');
|
|
122
|
+
const { isAgent, agent } = await determineAgent();
|
|
123
|
+
return isAgent ? { mode: 'all', agent: agent.name } : { mode: 'errors' };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function describeForwardConsole({ mode, agent }) {
|
|
127
|
+
if (!mode) return chalk.gray('不转发浏览器里的报错');
|
|
128
|
+
if (mode === 'errors') return `转发报错到终端${chalk.gray(',加 --forward-console 再转发 console.warn / console.error')}`;
|
|
129
|
+
return `转发报错和 console.warn / console.error 到终端${agent ? chalk.gray(`(检测到编程助手 ${agent})`) : ''}`;
|
|
130
|
+
}
|
|
131
|
+
|
|
94
132
|
function openBrowser(url) {
|
|
95
133
|
const command = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'cmd' : 'xdg-open';
|
|
96
134
|
const args = process.platform === 'win32' ? ['/c', 'start', '', url] : [url];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wxa-runner",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Run WeChat Mini Programs in the browser on the official base library, for debugging and E2E testing",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"wechat",
|
|
@@ -33,10 +33,11 @@
|
|
|
33
33
|
"bin"
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
|
+
"@vercel/detect-agent": "^1.2.7",
|
|
36
37
|
"chalk": "^4.1.2",
|
|
37
38
|
"commander": "^11.1.0",
|
|
38
|
-
"@open-wxa/wxa-runner-core": "0.
|
|
39
|
-
"@open-wxa/wxa-runner-extractor": "0.
|
|
39
|
+
"@open-wxa/wxa-runner-core": "0.8.0",
|
|
40
|
+
"@open-wxa/wxa-runner-extractor": "0.8.0"
|
|
40
41
|
},
|
|
41
42
|
"engines": {
|
|
42
43
|
"node": ">=18"
|