forked from ker0olos/fable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start_dev.ts
73 lines (52 loc) · 1.49 KB
/
start_dev.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
// deno-lint-ignore-file explicit-function-return-type
import { green, yellow } from '$std/fmt/colors.ts';
import { MongoMemoryReplSet } from 'mongodb-memory-server';
import { start } from '~/src/interactions.ts';
import { ensureIndexes } from '~/db/_ensureIndexes.ts';
let mongod: MongoMemoryReplSet;
const sleep = (ms: number): Promise<void> => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
if (import.meta.main) {
const isRemote = Deno.args.includes('--remote');
if (!isRemote) {
await startMongod();
await sleep(500);
await ensureIndexes();
} else {
console.log(yellow('Using the production database, be careful!'));
}
await start();
await tunnel();
}
async function startMongod() {
mongod = await MongoMemoryReplSet.create({
replSet: { storageEngine: 'ephemeralForTest' },
});
const uri = mongod.getUri();
console.log(green(uri));
Deno.env.set('MONGO_URI', uri);
}
async function tunnel() {
try {
const maxAttempts = 5;
let ngrok;
let attempts = 0;
while (attempts < maxAttempts) {
try {
ngrok = await fetch('http://localhost:4040/api/tunnels');
const { tunnels } = await ngrok.json();
console.log(green(tunnels[0].public_url));
break;
} catch {
attempts++;
await sleep(1000);
}
}
if (attempts >= maxAttempts) {
console.error('Failed to fetch ngrok tunnels after multiple attempts.');
}
} catch (err) {
console.error(err);
}
}