tauri
的 Command
api
在官网的介绍非常简单。简单到当你碰到问题的时候,你根本不知道该从哪里着手来解决问题。
要想正常使用 Command
,必须要先在src-tauri/tauri.conf.json
中声明你的命令作用域(scope
)。比如官网的例子:
"shell": {
"execute": true,
"sidecar": true,
"open": true,
"scope": [{
"name": "run-git-commit",
"cmd": "git",
"args": ["commit", "-m", { "validator": "\\S+" }]
}
]
}
在页面中使用的时候:
import { Command } from '@tauri-apps/api/shell'
new Command('run-git-commit', ['commit', '-m', 'the commit message'])
这似乎要求我们要对每一个命令都要实现进行定义,非常繁琐。比如git 的命令有:
git add
git commit
git branch
git checkout
git --version
...
所以,我们只需要将scope
中 args
改为true
就可以接受所有参数了,不需要一个一个去定义。
"shell": {
"execute": true,
"sidecar": true,
"open": true,
"scope": [{
"name": "run-git",
"cmd": "git",
"args": true
}
]
}
我一直以为,tauri
已经对不同的平台做了兼容处理,也就是同一个Command
我在不同的平台应该都是可以直接运行的。但是,在macOS
和windows
环境打包程序的时候我发现,我还是太天真了。很多东西,还是需要自己来处理的。
我最开始是在macOS
环境下开的程序,运行npm -v
非常丝滑。scope
配置如下:
"shell": {
"execute": true,
"sidecar": true,
"open": true,
"scope": [{
"name": "npm",
"cmd": "npm",
"args": true
}
]
}
在页面中如下使用:
import { Command } from '@tauri-apps/api/shell'
new Command('npm', ['-v']).spawn();
在macOS
下一切正常,但是到了windows
下完全运行不起来,一直报错program not found
。查看了官网文档包括rust
和api
文档都没有相关介绍。在tauri
群里问大神们,也没有人回复。
后来我突发奇想,如果是nodejs
中的process_child
,它会怎么处理?才发现nodejs
也有类似的问题,就是在macOS
下运行命令正常,但是到了windows
下就跑不了。就这个问题,在网上查了下才最终找到个靠谱的方案。
就是将命令改为xxx.cmd
,测试了下确实解决了问题。
在scope中如下配置:
"shell": {
"execute": true,
"sidecar": true,
"open": true,
"scope": [{
"name": "npm",
"cmd": "npm.cmd",
"args": true
}
]
}
这样就可以了。当然如果为了兼容macOS
和windows
最好将两个都注册到scope
中
"shell": {
"execute": true,
"sidecar": true,
"open": true,
"scope": [{
"name": "npm",
"cmd": "npm",
"args": true
},{
"name": "npm.cmd",
"cmd": "npm.cmd",
"args": true
}
]
}
然后在页面中对不同的platform
进行兼容。
import { Command } from '@tauri-apps/api/shell';
import { platform } from '@tauri-apps/api/os';
function async runCommand(){
const platformName = await platform();
const cmdStr = /^win/i.test(platformName) ? 'npm.cmd' : 'npm';
await new Command(cmdStr, ['-v']).spawn();
}
runCommand();
不过,也并不是所有的命令都适用这个方法,比如git
在两个环境都是一样的直接用git
就好,不需要加.cmd
。