-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.mjs
91 lines (81 loc) · 2.39 KB
/
run.mjs
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
import ora from 'ora'
import { spawn } from "child_process"
import { EventEmitter } from 'events'
import { writeComposite } from './composites.mjs';
const events = new EventEmitter()
const spinner = ora();
const credentials = async () => {
spinner.info("[Ceramic] creating admin credentials");
const admin = spawn('npm', ['run', 'generate'])
spinner.succeed("Ceramic] admin credentials created");
admin.stdout.on('data', (buffer) => {
console.log('[Ceramic]', buffer.toString())
})
}
const ceramic = spawn("npm", ["run", "ceramic"]);
ceramic.stdout.on("data", (buffer) => {
console.log('[Ceramic]', buffer.toString())
if (buffer.toString().includes("IMPORTANT: Ceramic API running")) {
events.emit("ceramic", true);
spinner.succeed("ceramic node started");
}
})
ceramic.stderr.on('data', (err) => {
console.log(err.toString())
})
const bootstrap = async () => {
// TODO: convert to event driven to ensure functions run in correct orders after releasing the bytestream.
// TODO: check if .grapql files match their .json counterparts
// & do not create the model if it already exists & has not been updated
try {
spinner.info("[Composites] bootstrapping composites");
await writeComposite(spinner)
spinner.succeed("Composites] composites bootstrapped");
} catch (err) {
spinner.fail(err.message)
ceramic.kill()
throw err
}
}
const graphiql = async () => {
spinner.info("[GraphiQL] starting graphiql");
const graphiql = spawn('node', ['./scripts/graphiql.mjs'])
spinner.succeed("[GraphiQL] graphiql started");
graphiql.stdout.on('data', (buffer) => {
console.log('[GraphiqQL]',buffer.toString())
})
}
const next = async () => {
const next = spawn('npm', ['run', 'nextDev'])
spinner.info("[NextJS] starting nextjs app");
next.stdout.on('data', (buffer) => {
console.log('[NextJS]', buffer.toString())
})
}
const start = async () => {
try {
await credentials()
spinner.start('[Ceramic] Starting Ceramic node\n')
events.on('ceramic', async (isRunning) => {
if (isRunning) {
await bootstrap()
await graphiql()
await next()
}
if(isRunning === false) {
ceramic.kill()
process.exit()
}
})
} catch (err) {
ceramic.kill()
spinner.fail(err)
}
}
start()
process.on("SIGTERM", () => {
ceramic.kill();
});
process.on("beforeExit", () => {
ceramic.kill();
});