-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutils.ts
130 lines (115 loc) · 3.08 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0.
// SPDX-License-Identifier: MPL-2.0
import { copySync, dirname, fromFileUrl, join, resolve } from "./deps.ts";
export const projectDir = resolve(dirname(fromFileUrl(import.meta.url)), "..");
export async function runOrExit(
cmd: string[],
cwd?: string,
env: Record<string, string> = {},
) {
console.error("shell: ", cmd.join(" "), { env, cwd });
const p = new Deno.Command(cmd[0], {
args: cmd.slice(1),
cwd,
// stdout: "piped",
// stderr: "piped",
env: { ...Deno.env.toObject(), ...env },
}).spawn();
// await p.stdout.pipeTo(Deno.stdout.writable, { preventClose: true });
// await p.stderr.pipeTo(Deno.stderr.writable, { preventClose: true });
const { code, success } = await p.status;
if (!success) {
Deno.exit(code);
}
}
interface Lockfile {
[channel: string]: {
files: Record<string, string[]>;
lines: Record<string, Record<string, string>>;
lock: Record<string, string>;
};
}
export type Cursor = {
start: number;
end: number;
length: number;
match: string;
};
export function upperFirst(str: string) {
return str.charAt(0).toUpperCase() + str.substring(1);
}
export function camelCase(str: string) {
return str
.split(/_+/g)
.map((chunk, idx) => (idx > 0 ? upperFirst(chunk) : chunk))
.join("");
}
/**
* Enhanced `indexOf` with regex support and position information
*/
export function nextMatch(
text: string,
word: string | RegExp,
pos = 0,
): Cursor | null {
if (word instanceof RegExp) {
const searchPos = Math.min(text.length, pos);
const nextText = text.substring(searchPos);
const res = word.exec(nextText);
word.lastIndex = 0; // always reset (js!)
return res
? {
match: res[0],
start: searchPos + res.index,
end: searchPos + res.index + res[0].length - 1,
length: res[0].length,
}
: null;
}
const start = text.indexOf(word, pos);
return start >= 0
? {
start,
end: start + word.length - 1,
match: word,
length: word.length,
}
: null;
}
/**
* Determine all indexOf with position information
*/
export function findCursors(
text: string,
word: string | RegExp,
): Array<Cursor> {
const matches = [] as Array<Cursor>;
let cursor = 0;
while (true) {
const res = nextMatch(text, word, cursor);
if (res != null) {
cursor = res.end;
matches.push(res);
} else {
break;
}
}
return matches;
}
/** Remove extension, will treat `.d.ts` as a whole */
export function removeExtension(path: string) {
const known = /(\.d\.m?ts)$/;
const match = path.trim().match(known);
return match
? path.replace(match[0], "")
: path.substring(0, path.lastIndexOf("."));
}
export function copyFilesAt(
{ destDir, overwrite }: { destDir: string; overwrite?: boolean },
fileNames: Record<string, string>,
) {
for (const [orig, destName] of Object.entries(fileNames)) {
console.log("from", orig, "to", join(destDir, destName));
copySync(orig, join(destDir, destName), { overwrite });
}
}