forked from dsherret/dax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
372 lines (357 loc) · 10.6 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import { CommandBuilder, CommandResult, escapeArg } from "./src/command.ts";
import { Delay, DelayIterator, delayToIterator, delayToMs, formatMillis } from "./src/common.ts";
import { colors, fs, path, which, whichSync } from "./src/deps.ts";
import { RequestBuilder } from "./src/request.ts";
export { CommandBuilder, CommandResult } from "./src/command.ts";
export type { CommandContext, CommandHandler, CommandPipeWriter } from "./src/command_handler.ts";
export { RequestBuilder, RequestResult } from "./src/request.ts";
export type {
CdChange,
ContinueExecuteResult,
EnvChange,
ExecuteResult,
ExitExecuteResult,
SetEnvVarChange,
SetShellVarChange,
} from "./src/result.ts";
/**
* Cross platform shell tools for Deno inspired by [zx](https://github.com/google/zx).
*
* Differences:
*
* 1. No globals or global configuration.
* 1. No custom CLI.
* 1. Cross platform shell to help the code work on Windows.
* - Uses [deno_task_shell](https://github.com/denoland/deno_task_shell)'s parser.
* - Allows exporting the shell's environment to the current process.
* 1. Good for application code in addition to use as a shell script replacement
* 1. Named after my cat.
*
* ## Example
*
* ```ts
* import $ from "https://deno.land/x/dax@VERSION_GOES_HERE/mod.ts";
*
* await $`echo hello`;
* ```
*
* @module
*/
/** Options for using `$.retry({ ... })` */
export interface RetryOptions<TReturn> {
/** Number of times to retry. */
count: number;
/** Delay in milliseconds. */
delay: Delay | DelayIterator;
/** Action to retry if it throws. */
action: () => Promise<TReturn>;
/** Do not log. */
quiet?: boolean;
}
export interface $Type {
(strings: TemplateStringsArray, ...exprs: any[]): CommandBuilder;
/**
* Makes a request to the provided URL throwing by default if the
* response is not successful.
*
* ```ts
* const data = await $.request("https://plugins.dprint.dev/info.json")
* .json();
* ```
*
* @see {@link RequestBuilder}
*/
request(url: string | URL): RequestBuilder;
/** Changes the directory of the current process. */
cd(path: string | URL): void;
/**
* Escapes an argument for the shell when not using the template
* literal.
*
* This is done by default in the template literal, so you most likely
* don't need this, but it may be useful when using the command builder.
*
* For example:
*
* ```ts
* const builder = new CommandBuilder()
* .command(`echo ${$.escapeArg("some text with spaces")}`);
*
* // equivalent to this:
* const builder = new CommandBuilder()
* .command(`echo 'some text with spaces'`);
*
* // you may just want to do this though:
* const builder = new CommandBuilder()
* .command(["echo", "some text with spaces"]);
* ```
*/
escapeArg(arg: string): string;
/**
* Gets if the provided path exists asynchronously.
*
* Although there is a potential for a race condition between the
* time this check is made and the time some code is used, it may
* not be a big deal to use this in some scenarios and simplify
* the code a lot.
*/
exists(path: string): Promise<boolean>;
/** Gets if the provided path exists synchronously. */
existsSync(path: string): boolean;
/** Re-export of deno_std's `fs` module. */
fs: typeof fs;
/** Re-export of deno_std's `path` module. */
path: typeof path;
/**
* Logs with potential indentation (`$.logIndent`)
* and output of commands or request responses.
*
* Note: Everything is logged over stderr.
*/
log(...data: any[]): void;
/**
* Similar to `$.log`, but logs out the text lighter than usual. This
* might be useful for logging out something that's unimportant.
*/
logLight(...data: any[]): void;
/**
* Similar to `$.log`, but will bold red the first word if one argument or
* first argument if multiple arguments.
*/
logStep(firstArg: string, ...data: any[]): void;
/**
* Similar to `$.logStep`, but will use bold red.
*/
logError(firstArg: string, ...data: any[]): void;
/**
* Similar to `$.logStep`, but will use bold yellow.
*/
logWarn(firstArg: string, ...data: any[]): void;
/**
* Causes all `$.log` and like functions to be logged with indentation.
*
* ```ts
* await $.logIndent(async () => {
* $.log("This will be indented.");
* await $.logIndent(async () => {
* $.log("This will indented even more.");
* });
* });
* ```
*/
logIndent<TResult>(action: () => TResult): TResult;
/**
* Sleep for the provided delay.
*
* ```ts
* await $.sleep(1000); // ms
* await $.sleep("1.5s");
* await $.sleep("100ms");
* ```
*/
sleep(delay: Delay): Promise<void>;
/**
* Does the provided action until it succeeds (does not throw)
* or the specified number of retries (`count`) is hit.
*/
withRetries<TReturn>(params: RetryOptions<TReturn>): Promise<TReturn>;
/** Re-export of `deno_which` for getting the path to an executable. */
which: typeof which;
/** Similar to `which`, but synchronously. */
whichSync: typeof whichSync;
}
function sleep(delay: Delay) {
const ms = delayToMs(delay);
return new Promise<void>(resolve => setTimeout(resolve, ms));
}
async function withRetries<TReturn>(opts: RetryOptions<TReturn>) {
const delayIterator = delayToIterator(opts.delay);
for (let i = 0; i < opts.count; i++) {
if (i > 0) {
const nextDelay = delayIterator.next();
if (!opts.quiet) {
$.logWarn(`Failed trying again in ${formatMillis(nextDelay)}...`);
}
await sleep(nextDelay);
if (!opts.quiet) {
$.logStep(`Retrying attempt ${i + 1}/${opts.count}...`);
}
}
try {
return await opts.action();
} catch (err) {
// don't bother with indentation here
console.error(err);
}
}
throw new Error(`Failed after ${opts.count} attempts.`);
}
function cd(path: string | URL) {
Deno.chdir(path);
}
// this is global because it then allows functions to easily call
// other functions and those should be indented underneath
let indentLevel = 0;
function getLogText(data: any[]) {
// this should be smarter to better emulate how console.log/error work
const combinedText = data.join(" ");
if (indentLevel === 0) {
return combinedText;
} else {
const indentText = " ".repeat(indentLevel);
return combinedText
.split(/\n/) // keep \r on line
.map(l => `${indentText}${l}`)
.join("\n");
}
}
function logStep(firstArg: string, data: any[], colourize: (text: string) => string) {
if (data.length === 0) {
let i = 0;
// skip over any leading whitespace
while (i < firstArg.length && firstArg[i] === " ") {
i++;
}
// skip over any non whitespace
while (i < firstArg.length && firstArg[i] !== " ") {
i++;
}
// emphasize the first word only
firstArg = colourize(firstArg.substring(0, i)) + firstArg.substring(i);
} else {
firstArg = colourize(firstArg);
}
console.error(getLogText([firstArg, ...data]));
}
const helperObject = {
fs,
path,
cd,
escapeArg,
existsSync(path: string) {
return fs.existsSync(path);
},
exists(path: string) {
return fs.exists(path);
},
log(...data: any[]) {
// all logging is done over stderr
console.error(getLogText(data));
},
logLight(...data: any[]) {
console.error(colors.gray(getLogText(data)));
},
logStep(firstArg: string, ...data: any[]) {
logStep(firstArg, data, (t) => colors.bold(colors.green(t)));
},
logError(firstArg: string, ...data: any[]) {
logStep(firstArg, data, (t) => colors.bold(colors.red(t)));
},
logWarn(firstArg: string, ...data: any[]) {
logStep(firstArg, data, (t) => colors.bold(colors.yellow(t)));
},
logIndent<TResult>(action: () => TResult): TResult {
indentLevel++;
let wasPromise = false;
try {
const result = action();
if (result instanceof Promise) {
wasPromise = true;
return result.finally(() => {
indentLevel--;
}) as any;
} else {
return result;
}
} finally {
if (!wasPromise) {
indentLevel--;
}
}
},
sleep,
withRetries,
which(commandName: string) {
if (commandName.toUpperCase() === "DENO") {
return Promise.resolve(Deno.execPath());
} else {
return which(commandName);
}
},
whichSync(commandName: string) {
if (commandName.toUpperCase() === "DENO") {
return Deno.execPath();
} else {
return whichSync(commandName);
}
},
};
/** Options for creating a custom `$`. */
export interface Create$Options {
/** Uses the state of this command builder as a starting point. */
commandBuilder?: CommandBuilder;
/** Uses the state of this request builder as a starting point. */
requestBuilder?: RequestBuilder;
}
/**
* Builds a new `$` which will use the state of the provided
* builders as the default.
*
* This can be useful if you want different default settings.
*
* Example:
*
* ```ts
* import { build$ } from "https://deno.land/x/dax/mod.ts";
*
* const commandBuilder = new CommandBuilder()
* .cwd("./subDir")
* .env("HTTPS_PROXY", "some_value");
* const requestBuilder = new RequestBuilder()
* .header("SOME_VALUE", "value");
*
* const $ = build$({ commandBuilder, requestBuilder });
*
* // this command will use the env described above, but the main
* // process and default `$` won't have its environment changed
* await $`deno run my_script.ts`;
*
* // similarly, this will have the headers that were set in the request builder
* const data = await $.request("https://plugins.dprint.dev/info.json").json();
* ```
*/
export function build$(options: Create$Options) {
const commandBuilder = options.commandBuilder ?? new CommandBuilder();
const requestBuilder = options.requestBuilder ?? new RequestBuilder();
return Object.assign(
(strings: TemplateStringsArray, ...exprs: any[]) => {
let result = "";
for (let i = 0; i < Math.max(strings.length, exprs.length); i++) {
if (strings.length > i) {
result += strings[i];
}
if (exprs.length > i) {
const expr = exprs[i];
if (expr instanceof CommandResult) {
// remove last newline
result += escapeArg(expr.stdout.replace(/\r?\n$/, ""));
} else {
result += escapeArg(`${exprs[i]}`);
}
}
}
return commandBuilder.command(result);
},
helperObject,
{
request(url: string | URL) {
return requestBuilder.url(url);
},
},
);
}
/**
* Default `$` where commands may be executed.
*/
export const $: $Type = build$({});
export default $;