-
Notifications
You must be signed in to change notification settings - Fork 41
/
utils.ts
110 lines (100 loc) · 2.59 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2018-2024 the Deno authors. MIT license.
import { expandGlob } from "@std/fs/expand_glob";
import * as path from "@std/path";
/** 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 = 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, ...args] = getCmd();
await Deno.permissions.request({ name: "run", command: cmd });
try {
const process = new Deno.Command(cmd, {
args,
cwd: opts.cwd,
stderr: "inherit",
stdout: "inherit",
stdin: "inherit",
});
const output = await process.output();
if (!output.success) {
throw new Error(
`${opts.cmd.join(" ")} failed with exit code ${output.code}`,
);
}
} 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;
}
}
}
export function standardizePath(fileOrDirPath: string) {
if (fileOrDirPath.startsWith("file:")) {
return path.fromFileUrl(fileOrDirPath);
}
return path.resolve(fileOrDirPath);
}
export function valueToUrl(value: string) {
const lowerCaseValue = value.toLowerCase();
if (
lowerCaseValue.startsWith("http:") ||
lowerCaseValue.startsWith("https:") ||
lowerCaseValue.startsWith("npm:") ||
lowerCaseValue.startsWith("jsr:") ||
lowerCaseValue.startsWith("node:") ||
lowerCaseValue.startsWith("file:")
) {
return value;
} else {
return path.toFileUrl(path.resolve(value)).toString();
}
}
export function getDntVersion(url = import.meta.url) {
return /\/dnt@([0-9]+\.[0-9]+\.[0-9]+)\//.exec(url)?.[1] ?? "dev";
}