-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
96 lines (81 loc) · 2.77 KB
/
mod.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
88
89
90
91
92
93
94
95
96
export * from "./base.ts";
export type { Daemon, ExecutionTarget } from "./daemon.ts";
import { Action, Command, Mode } from "./base.ts";
import { powar } from "./deps.ts";
import { installDaemonLaunch } from "./daemon.ts";
import { CommonActions } from "./common_actions.ts";
import { CommonBindings } from "./common_bindings.ts";
import { MappingCreate, BindingConfigGen } from "./kb_binding_config_gen.ts";
export interface UltraOpts extends powar.ModuleConfig {
shellWrapper: (cmd: string) => string;
karabinerWrite: {
profileName: string;
configPath: string;
};
karabinerCliPath: string;
yabaiCliPath: string;
daemonLaunch?: { name: string; path: string }[];
setup?: (U: Ultra, p: powar.ModuleApi) => void;
}
export type ActionHook = {
after: "set-mode";
action: (mode: Mode) => Action;
};
export interface Ultra extends powar.Module {
addMappings: (entries: MappingCreate[]) => void;
addStartup: (command: Command) => void;
addActionHook: (hook: ActionHook) => void;
commonActions: CommonActions;
commonBindings: CommonBindings;
opts: UltraOpts;
}
export function ultra(opts: UltraOpts): Ultra {
return new UltraImpl(opts);
}
class UltraImpl implements Ultra {
private startupCommands: string[] = [];
private configGen: BindingConfigGen;
private actionHooks: ActionHook[] = [];
name: string;
path: string;
dependsOn?: string[];
constructor(public opts: UltraOpts) {
this.name = this.opts.name;
this.path = this.opts.path;
this.dependsOn = this.opts.dependsOn;
this.commonBindings = new CommonBindings();
this.commonActions = new CommonActions(opts, this.commonBindings);
this.configGen = new BindingConfigGen(this.opts, this.actionHooks);
}
addActionHook = (hook: ActionHook) => {
this.actionHooks.push(hook);
};
commonActions: CommonActions;
commonBindings: CommonBindings;
addStartup = (command: Command): void => {
this.startupCommands.push(this.opts.shellWrapper(command.command));
};
addMappings = (entries: MappingCreate[]): void => {
this.configGen.addMappings(entries);
};
action = async (p: powar.ModuleApi): Promise<void> => {
if (typeof this.opts.setup !== "undefined") {
await this.opts.setup(this, p);
p.info(`Ran Ultra setup`);
}
await this.configGen.install(p);
p.info(`Installed Ultra configuration`);
if (typeof this.opts.daemonLaunch !== "undefined") {
for (const daemon of this.opts.daemonLaunch) {
await installDaemonLaunch(p, daemon.name, daemon.path);
p.info(`Installed Ultra daemon: ${daemon.name}`);
}
}
for (const command of this.startupCommands) {
await p.exec(command);
}
if (this.startupCommands.length > 0) {
p.info(`Executed ${this.startupCommands.length} startup command(s)`);
}
};
}