-
Notifications
You must be signed in to change notification settings - Fork 0
/
_run.ts
66 lines (62 loc) · 1.79 KB
/
_run.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
import { move } from 'https://deno.land/std/fs/mod.ts';
import { join } from 'https://deno.land/std/path/mod.ts';
const decoder = new TextDecoder('utf-8');
/** Deno.run().output() and wrapping code. */
export async function run(params: {
cmd: string[];
cwd?: string;
env?: Record<string, string>;
}) {
const p = Deno.run({
...params,
stdin: 'null',
stdout: 'piped',
stderr: 'piped',
});
const [{ success, code }, out, err] = await Promise.all([
p.status(),
p.output(),
p.stderrOutput(),
]);
p.close();
if (success) {
return decoder.decode(out).trim();
} else {
const msg = decoder.decode(err).trim();
throw new Error(`Exit ${code}: ${msg}`);
}
}
/** https://docs.microsoft.com/en-us/windows/win32/shell/knownfolderid */
export async function pwshKnownFolder(id: 'ApplicationData') {
const value = `[Environment]::GetFolderPath([Environment+SpecialFolder]::${id})`;
return await run({ cmd: ['pwsh', '-noprofile', '-command', value] });
}
/** Use PowerShell Core on Windows, or unzip otherwise. */
export async function unzip(
path: string,
dest: string,
opt?: { xlist: string[] }
) {
if (Deno.build.os === 'windows') {
const tmp = await Deno.makeTempDir();
const c = ['Expand-Archive', '-Path', path, '-DestinationPath', tmp];
await run({ cmd: ['pwsh', '-noprofile', '-command', ...c] });
entries: for await (const dirEntry of Deno.readDir(tmp)) {
for (const exclude of opt?.xlist ?? []) {
if (dirEntry.name.startsWith(exclude)) {
continue entries;
}
}
if (dirEntry.name.startsWith('META-INF')) {
continue entries;
}
await move(join(tmp, dirEntry.name), join(dest, dirEntry.name), {
overwrite: true,
});
}
} else {
const cmd = ['unzip', '-uo', path, '-d', dest];
if (opt?.xlist) cmd.push('-x', ...opt.xlist);
await run({ cmd });
}
}