-
Notifications
You must be signed in to change notification settings - Fork 39
/
utils.ts
86 lines (79 loc) · 1.88 KB
/
utils.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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import * as deps from "./mod.deps.ts";
/** Gets the files found in the provided root dir path based on the glob. */
export async function glob(options: {
pattern: string;
rootDir: string;
excludeDirs: string[];
}) {
const paths: string[] = [];
const entries = deps.glob.expandGlob(options.pattern, {
root: options.rootDir,
extended: true,
globstar: true,
exclude: options.excludeDirs,
});
for await (const entry of entries) {
if (entry.isFile) {
paths.push(entry.path);
}
}
return paths;
}
export function runNpmCommand({ bin, args, cwd }: {
bin: string;
args: string[];
cwd: string;
}) {
return runCommand({
cmd: [bin, ...args],
cwd,
});
}
export async function runCommand(opts: {
cmd: string[];
cwd: string;
}) {
const cmd = getCmd();
await Deno.permissions.request({ name: "run", command: cmd[0] });
try {
const process = Deno.run({
cmd,
cwd: opts.cwd,
stderr: "inherit",
stdout: "inherit",
stdin: "inherit",
});
try {
const status = await process.status();
if (!status.success) {
throw new Error(
`${opts.cmd.join(" ")} failed with exit code ${status.code}`,
);
}
} finally {
process.close();
}
} catch (err) {
// won't happen on Windows, but that's ok because cmd outputs
// a message saying that the command doesn't exist
if (err instanceof Deno.errors.NotFound) {
throw new Error(
`Could not find command '${
opts.cmd[0]
}'. Ensure it is available on the path.`,
{ cause: err },
);
} else {
throw err;
}
}
function getCmd() {
const cmd = [...opts.cmd];
if (Deno.build.os === "windows") {
return ["cmd", "/c", ...opts.cmd];
} else {
return cmd;
}
}
}