generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 147
/
runner.ts
87 lines (70 loc) · 2.01 KB
/
runner.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { TauriConfig } from './config';
import {
execCommand,
getTauriDir,
hasDependency,
usesBun,
usesPnpm,
usesYarn,
} from './utils';
class Runner {
// Could be "npm", "yarn", "pnpm", "bun", "cargo", "path/to/tauri-cli/binary" or "tauri"
bin: string;
// could be ["tauri"], ["run", "tauri"], ["some package.json script"], ["run", "some package.json script"] or []
tauriScript: string[];
constructor(bin: string, tauriScript?: string[]) {
this.bin = bin;
this.tauriScript = tauriScript || [];
}
async execTauriCommand(
command: string[],
commandOptions: string[],
cwd?: string,
): Promise<void> {
const args: string[] = [];
if (this.bin === 'npm' && this.tauriScript[0] !== 'run') {
args.push('run');
}
args.push(...this.tauriScript);
args.push(...command);
if (this.bin === 'npm' && commandOptions.length) {
args.push('--');
}
args.push(...commandOptions);
return execCommand(this.bin, args, { cwd });
}
}
async function getRunner(
root: string,
tauriScript: string | null,
): Promise<Runner> {
if (tauriScript) {
// FIXME: This will also split file paths with spaces.
const [runnerCommand, ...runnerArgs] = tauriScript.split(' ');
return new Runner(runnerCommand, runnerArgs);
}
if (hasDependency('@tauri-apps/cli', root)) {
if (usesYarn(root)) return new Runner('yarn', ['tauri']);
if (usesPnpm(root)) return new Runner('pnpm', ['tauri']);
if (usesBun(root)) return new Runner('bun', ['tauri']);
return new Runner('npm', ['run', 'tauri']);
}
// TODO: Change to v2 after a while.
let tag = 'v1';
try {
const tauriDir = getTauriDir(root);
if (tauriDir) {
const baseConf = TauriConfig.fromBaseConfig(tauriDir);
if (baseConf && baseConf.isV2()) {
tag = 'v2';
}
}
} catch {
// ignore
}
await execCommand('npm', ['install', '-g', `@tauri-apps/cli@${tag}`], {
cwd: undefined,
});
return new Runner('tauri');
}
export { Runner, getRunner };