1- import { existsSync } from 'node:fs'
21import { resolve } from 'node:path'
32import { execa } from 'execa'
43import type { GeneratorTransform } from 'execa/types/transform/normalize.js'
5- import ora from 'ora'
64import { resolveCommand } from 'package-manager-detector/commands'
75import type { BuildOptions } from '../build.js'
86import type { RunOptions } from '../run.js'
97import { App } from '../utils/app.js'
10- import { errorDebugLog } from '../utils/error.js'
118import { stripAnsiColors } from '../utils/exec.js'
9+ import { exists } from '../utils/file.js'
1210import Log from '../utils/log.js'
13- import { isWindows , uniRunSuccess } from '../utils/util.js'
11+ import { isDarwin , isWindows , uniRunSuccess } from '../utils/util.js'
1412import { PlatformModule } from './index.js'
1513
16- function getWeixinDevToolCliPath ( ) {
14+ async function getWeixinDevToolCliPath ( ) {
1715 if ( process . env . WEIXIN_DEV_TOOL ) {
18- if ( existsSync ( process . env . WEIXIN_DEV_TOOL ) ) return process . env . WEIXIN_DEV_TOOL
16+ if ( await exists ( process . env . WEIXIN_DEV_TOOL ) ) return process . env . WEIXIN_DEV_TOOL
1917 }
2018 const defaultPath = isWindows ( )
2119 ? 'C:\\Program Files (x86)\\Tencent\\微信web开发者工具\\cli.bat'
2220 : '/Applications/wechatwebdevtools.app/Contents/MacOS/cli'
23- if ( existsSync ( defaultPath ) ) return defaultPath
21+ if ( await exists ( defaultPath ) ) return defaultPath
2422}
2523
26- function openWeixinDevTool ( projectPath : string ) {
27- const cliPath = getWeixinDevToolCliPath ( )
24+ async function openWeixinDevTool ( projectPath : string ) {
25+ if ( ! isWindows ( ) && ! isDarwin ( ) ) {
26+ Log . error ( `微信开发者工具不支持系统: ${ process . platform } ` )
27+ return
28+ }
29+ const cliPath = await getWeixinDevToolCliPath ( )
2830 if ( ! cliPath ) {
29- Log . error ( '微信开发工具没有找到,请运行 `uniapp requirement mp-weixin` 查看详细信息 ' )
31+ Log . error ( '未找到微信开发工具,请确认已安装 ' )
3032 return
3133 }
32- const spinner = ora ( '正在打开微信开发者工具' ) . start ( )
33- execa `${ cliPath } ${ [ 'open' , '--project' , resolve ( App . projectRoot , projectPath ) ] } `
34- . then ( ( { stderr } ) => {
35- if ( stderr ) {
36- spinner . fail ( '微信开发者工具打开出错了' )
37- } else {
38- spinner . succeed ( '微信开发者工具已打开' )
39- }
40- } )
41- . catch ( ( err ) => {
42- spinner . fail ( '微信开发者工具打开出错了' )
43- errorDebugLog ( err )
44- } )
34+ await execa ( {
35+ stdio : 'inherit' ,
36+ env : { FORCE_COLOR : 'true' } ,
37+ reject : false ,
38+ } ) `${ cliPath } ${ [ 'open' , '--project' , resolve ( App . projectRoot , projectPath ) ] } `
4539}
4640
4741export default class PlatformMPWeixin extends PlatformModule {
@@ -50,12 +44,12 @@ export default class PlatformMPWeixin extends PlatformModule {
5044 modules = [ '@dcloudio/uni-mp-weixin' ]
5145
5246 async requirement ( ) {
53- if ( process . platform !== 'win32' && process . platform !== 'darwin' ) {
47+ if ( ! isWindows ( ) && ! isDarwin ( ) ) {
5448 Log . error ( `微信开发者工具不支持系统: ${ process . platform } ` )
5549 return
5650 }
5751
58- const cliPath = getWeixinDevToolCliPath ( )
52+ const cliPath = await getWeixinDevToolCliPath ( )
5953
6054 if ( cliPath ) {
6155 Log . success ( `微信开发者工具已安装 (${ cliPath } )` )
@@ -89,7 +83,7 @@ export default class PlatformMPWeixin extends PlatformModule {
8983 if ( ! uniRunSuccess ( text ) ) return
9084
9185 over = true
92- openWeixinDevTool ( 'dist/dev/mp-weixin' )
86+ void openWeixinDevTool ( 'dist/dev/mp-weixin' )
9387 } as GeneratorTransform < false >
9488
9589 await execa ( {
@@ -121,7 +115,7 @@ export default class PlatformMPWeixin extends PlatformModule {
121115
122116 const text = stripAnsiColors ( stdout as unknown as string )
123117 if ( / D O N E { 2 } B u i l d c o m p l e t e \. / . test ( text ) ) {
124- openWeixinDevTool ( 'dist/build/mp-weixin' )
118+ await openWeixinDevTool ( 'dist/build/mp-weixin' )
125119 }
126120 }
127121}
0 commit comments