|
| 1 | +# Termux Api |
| 2 | + |
| 3 | +> v7.1.7 新增 |
| 4 | +
|
| 5 | +termux 模块提供了与 [termuxApp](https://f-droid.org/packages/com.termux/) 交互的能力, |
| 6 | + |
| 7 | +首先你需要在`termux`中编辑`~/.termux/termux.properties`, 添加以下一行配置以允许运行命令 |
| 8 | + |
| 9 | +``` |
| 10 | +allow-external-apps = true |
| 11 | +``` |
| 12 | + |
| 13 | +你还需要为该应用授予`com.termux.permission.RUN_COMMAND`权限, |
| 14 | +可以选择在"应用详情"中手动允许或者通过调用该模块相关方法 |
| 15 | + |
| 16 | +> 此模块不是全局可用,需使用以下代码导入 |
| 17 | +
|
| 18 | +```js |
| 19 | +let termux = require("termux"); |
| 20 | +``` |
| 21 | + |
| 22 | +示例 |
| 23 | + |
| 24 | +```js |
| 25 | +let { requestPermission, bash, sh } = require("termux"); |
| 26 | + |
| 27 | +bash('echo "Hello from Termux bash!"', (result) => { |
| 28 | + console.log(result); |
| 29 | +}); |
| 30 | +``` |
| 31 | + |
| 32 | +类型 |
| 33 | + |
| 34 | +```ts |
| 35 | +export interface Result { |
| 36 | + stdout: string; |
| 37 | + stdout_original_length: number; |
| 38 | + stderr: string; |
| 39 | + stderr_original_length: number; |
| 40 | + exitCode: number; |
| 41 | + errCode: number | null; |
| 42 | + errmsg: string | null; |
| 43 | +} |
| 44 | +export type Callback = { |
| 45 | + (result: Result): void; |
| 46 | +}; |
| 47 | + |
| 48 | +export declare function createShortcutCommand( |
| 49 | + options: CommandOptions |
| 50 | +): ShortcutCommandFunction; |
| 51 | +export declare function runCommand( |
| 52 | + options: CommandOptions, |
| 53 | + callback?: Callback |
| 54 | +): void; |
| 55 | +export declare const bash: ShortcutCommandFunction; |
| 56 | +export declare const sh: ShortcutCommandFunction; |
| 57 | +export declare function checkPermission(): boolean; |
| 58 | +export declare function requestPermission( |
| 59 | + callback?: (r: boolean) => void |
| 60 | +): void; |
| 61 | +``` |
| 62 | + |
| 63 | +## runCommand([options](#commandoptions), callback?) |
| 64 | + |
| 65 | +向`termux`发送一个运行命令的请求,命令运行在`termux`中,而不是该应用,可选传入一个回调函数接收结果,如果没有传入`callback`,则视为放弃结果,该函数不会阻塞线程。 |
| 66 | + |
| 67 | +如果没有权限则会直接抛出错误 |
| 68 | + |
| 69 | +## createShortcutCommand([options](#commandoptions)) |
| 70 | + |
| 71 | +创建一个运行命令的快捷函数,传入的选项作为默认值,返回一个[快捷函数](#shortcutcommandfunction) |
| 72 | + |
| 73 | +## checkPermission() |
| 74 | + |
| 75 | +检查是否拥有权限 |
| 76 | + |
| 77 | +## requestPermission() |
| 78 | + |
| 79 | +向用户请求权限,可传入回调函数获得结果 |
| 80 | + |
| 81 | +```js |
| 82 | +let { requestPermission, bash, sh } = require("termux"); |
| 83 | +requestPermission((s) => { |
| 84 | + log(s); //true or false |
| 85 | +}); |
| 86 | +``` |
| 87 | + |
| 88 | +## bash, sh |
| 89 | + |
| 90 | +这两个函数是[快捷函数](#shortcutcommandfunction),但是必须传入参数,没有参数会抛出错误, |
| 91 | + |
| 92 | +传入的参数将附加到`bash -c ...`或`sh -c ...`,传入多个参数将使用空格进行拼接 |
| 93 | + |
| 94 | +## CommandOptions |
| 95 | + |
| 96 | +类型 |
| 97 | + |
| 98 | +```ts |
| 99 | +export interface CommandOptions { |
| 100 | + //可执行文件的路径,必填 |
| 101 | + path: string; |
| 102 | + //参数 |
| 103 | + arguments?: string[]; |
| 104 | + //工作目录 |
| 105 | + workdir?: string; |
| 106 | + //是否在后台运行,默认为false |
| 107 | + background?: boolean; |
| 108 | + //当在前台运行时有效 |
| 109 | + sessionAction?: "0" | "1" | "2" | "3"; |
| 110 | + //前台运行时显示的label |
| 111 | + label?: string; |
| 112 | + //前台运行时的description |
| 113 | + description?: string; |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +## ShortcutCommandFunction |
| 118 | + |
| 119 | +[createShortcutCommand](#createshortcutcommandoptions)所返回的函数, |
| 120 | +可传入额外的选项附加到默认选项 |
| 121 | + |
| 122 | +```ts |
| 123 | +type ShortcutCommandFunction = { |
| 124 | + //args 将作为额外参数添加到默认选项 |
| 125 | + (args?: string | string[], callback?: Callback): void; |
| 126 | + //第二个参数是字符串则作为 workdir 覆盖默认值 |
| 127 | + (args?: string | string[], workdir?: string, callback?: Callback): void; |
| 128 | + //第二个参数是对象则作为新的选项覆盖原有的相关属性 |
| 129 | + ( |
| 130 | + args?: string | string[], |
| 131 | + options?: CommandOptions, |
| 132 | + callback?: Callback |
| 133 | + ): void; |
| 134 | +}; |
| 135 | +``` |
0 commit comments