-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.ts
211 lines (190 loc) · 4.44 KB
/
api.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import type { io } from "./deps.ts";
/**
* A map of path key-value pairs.
*
* This is used for `{ src1 : [dest1,..destN], ..}` file path mappings.
*/
export type Entries = Record<string, string | string[]>;
/**
* A map of string-key--path-value pairs.
*
* This is used for `{ srcText1 : [dest1,..destN], ..}` content-to-file mappings.
*/
export type Contents = [string, string | string[]][];
/**
* Options for the `exec` function.
*/
export interface ExecOpts {
/**
* The standard input to pass to the command.
*/
stdin?: string | io.Buffer;
/**
* The current working directory to run the command in.
*
* By default it is the directory `path` given to the current module, or
* `rootPath` if this is in the global configuration.
*/
cwd?: string;
}
/**
* The output of the `exec` function.
*
* This is a wrapper around stdout and stderr buffers, and the exit code.
*/
export interface Output {
/**
* The exit code of the command.
*/
code: number;
/**
* The standard output of the command as a string.
*/
stdoutAsString: () => string;
/**
* The standard output of the command as a byte buffer.
*/
stdoutAsBytes: () => Uint8Array;
/**
* The standard error of the command as a string.
*/
stderrAsString: () => string;
/**
* The standard error of the command as a byte buffer.
*/
stderrAsBytes: () => Uint8Array;
}
/**
* An empty output object, used for dry runs.
*/
export const EMPTY_OUTPUT: Output = {
code: 0,
stdoutAsString() {
return "";
},
stdoutAsBytes() {
return new Uint8Array([]);
},
stderrAsString() {
return "";
},
stderrAsBytes() {
return new Uint8Array([]);
},
};
/**
* The main powar-ts API which is available to modules and the global
* configuration.
*/
export interface CommonApi {
/**
* Log a message at the info level.
*/
info: (msg: string) => void;
/**
* Log a message at the warn level.
*/
warn: (msg: string) => void;
/**
* Make a directory at the given path, including any parent directories.
*/
makeDir: (paths: string) => Promise<void>;
/**
* Copy files from the given source paths to the given destination paths.
*/
install: (entries: Entries) => Promise<void>;
/**
* Symbolically link files from the given source paths to the given destination paths.
*/
link: (entries: Entries) => Promise<void>;
/**
* Write the given contents to the given destination paths.
*/
installContents: (contents: Contents) => Promise<void>;
/**
* Execute the given command in the shell.
*/
exec: (command: string, opts?: ExecOpts) => Promise<Output>;
/**
* Read the contents of the given file.
*/
read: (filename: string) => Promise<string>;
}
/**
* The configuration for a powar-ts module.
*/
export interface ModuleConfig {
/**
* The name of the module
*/
name: string;
/**
* The path to the module, usually this should be `__dirname`.
*/
path: string;
/**
* Other modules which this module depends upon.
*
* These will be checked to exist before this module is run.
*/
dependsOn?: string[];
}
/**
* The API available to a powar-ts module.
*/
export interface ModuleApi extends CommonApi {
kind?: "module";
}
/**
* The API available to the global configuration.
*/
export interface GlobalApi extends CommonApi {
kind?: "global";
}
/**
* A powar-ts module.
*/
export interface Module extends ModuleConfig {
/**
* The action to perform for this module.
*
* This is a function which takes the powar-ts module API and performs
* some relevant tasks with it (installing/copying files, running commands, etc.).
*/
action: (p: ModuleApi) => Promise<void>;
}
/**
* The global configuration for powar-ts.
*/
export interface GlobalConfig {
/**
* Perform some tasks before the modules are run.
*/
preAction?: (p: GlobalApi) => Promise<void>;
/**
* The root path for the powar-ts configuration, usually this should be `__dirname`.
*/
rootPath: string;
/**
* The modules to run.
*/
modules: Module[];
/**
* Perform some tasks after the modules are run.
*/
postAction?: (p: GlobalApi) => Promise<void>;
}
/**
* Make a powar-ts module.
*/
export function module(m: Module): Module {
return m;
}
/**
* Produce some object `U` determined by some module variables `T` and the powar-ts module API.
*/
export function produce<T, U>(
m: (vars: T, p: ModuleApi) => U
): (vars: T, p: ModuleApi) => U {
return m;
}