-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.ts
250 lines (217 loc) · 6.78 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
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
import {path, readLines} from './deps.ts';
export function exitWithMessage (code: number, message: string): never {
console[code === 1 ? 'error' : 'log'](message);
Deno.exit(code);
}
// eslint-disable-next-line max-len
export async function findMetablockPath (dir: string): Promise<string | undefined> {
let metablockPath: string | undefined;
const metablockPaths = ['.json', '.js', '.yaml', '.yml']
.map(ext => path.join(dir, `metablock${ext}`));
for (const filePath of metablockPaths) {
const stats = await statsIfExists(filePath);
if (stats.exists && stats.isFile) {
metablockPath = filePath;
break;
}
}
return metablockPath;
}
/** hh:mm:ss.ssss */
export function getLocalPreciseTime (): string {
const locale = 'en-US';
/** hh:mm:ss.sss */
const timeFormatOptions: Intl.DateTimeFormatOptions = {
fractionalSecondDigits: 3,
hour: '2-digit',
hour12: false,
minute: '2-digit',
second: '2-digit',
};
// const formatter = new Intl.DateTimeFormat(locale, timeFormatOptions);
// return formatter.format(new Date());
return new Date().toLocaleString(locale, timeFormatOptions);
}
/** Read lines, invoking a callback with each line. Call `close()` on reader. */
async function handleLines (
reader: Deno.Reader & {close?: () => void},
// eslint-disable-next-line no-unused-vars
callback: (line: string) => unknown,
): Promise<void> {
for await (const line of readLines(reader)) callback(line);
reader.close?.();
}
export async function getProcessOutput (
cmd: string[],
options: {
/* eslint-disable no-unused-vars */
onStdErrLine?: (line: string) => unknown;
onStdOutLine?: (line: string) => unknown;
/* eslint-enable no-unused-vars */
} = {},
): Promise<{
status: Deno.ProcessStatus;
stderr: string;
stdout: string;
}> {
const process = Deno.run({cmd, stderr: 'piped', stdout: 'piped'});
const stdErrLines: string[] = [];
const stdOutLines: string[] = [];
const [status] = await Promise.all([
process.status(),
handleLines(process.stderr, line => {
stdErrLines.push(line);
options.onStdErrLine?.(line);
}),
handleLines(process.stdout, line => {
stdOutLines.push(line);
options.onStdOutLine?.(line);
}),
]);
process.close();
return {
status,
stderr: stdErrLines.join('\n'),
stdout: stdOutLines.join('\n'),
};
}
export function handleCommand (
command: string,
args: string[],
// eslint-disable-next-line no-unused-vars
branches: Record<string, (args: string[]) => void | Promise<void>>,
// eslint-disable-next-line no-unused-vars
defaultCase?: (command: string) => void | Promise<void>,
): void | Promise<void> {
for (const [commandName, callback] of Object.entries(branches)) {
if (commandName === command) return callback(args);
}
if (defaultCase) return defaultCase(command);
}
let isDockerMemo: boolean;
/** Adapted from https://unpkg.com/[email protected]/index.js */
export async function isDocker ({promptForPermissions}: {
promptForPermissions?: boolean;
} = {}): Promise<boolean> {
async function hasDockerEnv () {
try {
if (promptForPermissions) {
await requestPermission(
{name: 'read', path: '/.dockerenv'},
'Determine if environment is Docker',
);
}
await Deno.stat('/.dockerenv');
return true;
}
catch (ex) {
if (ex instanceof Deno.errors.NotFound) return false;
throw ex;
}
}
async function hasDockerCGroup () {
try {
if (promptForPermissions) {
await requestPermission(
{name: 'read', path: '/proc/self/cgroup'},
'Determine if environment is Docker',
);
}
return (await Deno.readTextFile('/proc/self/cgroup')).includes('docker');
}
catch (ex) {
if (ex instanceof Deno.errors.NotFound) return false;
throw ex;
}
}
if (typeof isDockerMemo !== 'boolean') {
isDockerMemo = await hasDockerEnv() || await hasDockerCGroup();
}
return isDockerMemo;
}
let isWslMemo: boolean;
/** Adapted from https://unpkg.com/[email protected]/index.js */
export async function isWsl ({promptForPermissions}: {
promptForPermissions?: boolean;
} = {}): Promise<boolean> {
async function determine () {
if (Deno.build.os !== 'linux') return false;
// This substituted condition is not high-effort
let match = Deno.build.vendor.toLowerCase().includes('microsoft');
if (match) {
if (await isDocker({promptForPermissions})) return false;
return true;
}
try {
if (promptForPermissions) {
await requestPermission(
{name: 'read', path: '/proc/version'},
'Determine if environment is WSL',
);
}
match = (await Deno.readTextFile('/proc/version')).toLowerCase().includes('microsoft');
return match
? !(await isDocker({promptForPermissions}))
: false;
}
catch (ex) {
if (ex instanceof Deno.errors.NotFound) return false;
throw ex;
}
}
if (typeof isWslMemo !== 'boolean') isWslMemo = await determine();
return isWslMemo;
}
export async function openInVsCode (path: string): Promise<void> {
console.log(`Opening "${path}" in VS Code`);
const p = Deno.run({cmd: ['code', path]});
const {success} = await p.status();
if (!success) {
throw new Error(`There was a problem opening "${path}" in VS Code. See console output for more details.`);
}
}
export async function requestPermission (
descriptor: Deno.PermissionDescriptor,
purpose?: string,
): Promise<boolean> {
const status = await Deno.permissions.query(descriptor);
if (status.state === 'prompt') {
if (purpose) console.log(`ℹ️ ${purpose}`);
await Deno.permissions.request(descriptor);
}
return status.state === 'granted';
}
export async function resolveRealPath (
path: string,
{promptForPermissions}: {promptForPermissions?: boolean} = {},
): Promise<string> {
if (promptForPermissions) {
await requestPermission(
{name: 'read', path: '.'},
'Create an absolute path',
);
}
const absolutePath = await Deno.realPath(path);
if (!(await isWsl({promptForPermissions}))) return absolutePath;
if (promptForPermissions) {
await requestPermission(
{command: 'wslpath', name: 'run'},
'Resolve the Windows path',
);
}
const cmd = ['wslpath', '-w', absolutePath];
const {status: {success}, stderr, stdout} = await getProcessOutput(cmd);
if (!success) throw new Error(stderr);
return stdout.trim();
}
type MaybeStats = {exists: false} | (Deno.FileInfo & {exists: true});
export async function statsIfExists (path: string): Promise<MaybeStats> {
try {
const info = await Deno.lstat(path);
return Object.assign(info, {exists: true});
}
catch (ex) {
if (ex instanceof Deno.errors.NotFound) return {exists: false};
throw ex;
}
}