forked from clerk/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.ts
More file actions
78 lines (66 loc) · 1.75 KB
/
Copy pathhelpers.ts
File metadata and controls
78 lines (66 loc) · 1.75 KB
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
import { randomBytes } from 'node:crypto';
import type { Readable } from 'node:stream';
// @ts-nocheck
const raw = (val: any) => dedent(String.raw(val));
const js = raw;
const jsx = raw;
const ts = raw;
const tsx = raw;
const mdx = raw;
const css = raw;
function json(value: Record<string, unknown>) {
return JSON.stringify(value, null, 2);
}
export const helpers = {
js,
jsx,
ts,
tsx,
mdx,
css,
json,
} as const;
export type Helpers = typeof helpers;
/**
* Format a String.raw template string with indentation stripped.
* https://github.com/dmnd/dedent/blob/master/dedent.js
*/
const dedent = (strings: string | Array<string>, ...values: Array<string>) => {
// @ts-ignore
const raw = typeof strings === 'string' ? [strings] : strings.raw;
let result = '';
for (let i = 0; i < raw.length; i++) {
result += raw[i].replace(/\\\n[ \t]*/g, '').replace(/\\`/g, '`');
if (i < values.length) {
result += values[i];
}
}
const lines = result.split('\n');
let mindent: number | null = null;
lines.forEach(l => {
const m = l.match(/^(\s+)\S+/);
if (m) {
const indent = m[1].length;
if (!mindent) {
mindent = indent;
} else {
mindent = Math.min(mindent, indent);
}
}
});
if (mindent !== null) {
const m = mindent; // appease Flow
result = lines.map(l => (l[0] === ' ' || l[0] === '\t' ? l.slice(m) : l)).join('\n');
}
return result.trim().replace(/\\n/g, '\n');
};
export const hash = () => randomBytes(5).toString('hex');
export const waitUntilMessage = async (stream: Readable, message: string) => {
return new Promise<void>(resolve => {
stream.on('data', chunk => {
if (chunk.toString().includes(message)) {
resolve();
}
});
});
};