-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdevelop.js
99 lines (89 loc) · 2.37 KB
/
develop.js
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
const {
ensureDatabaseExists,
} = require('@prisma/lift/dist/utils/ensureDatabaseExists')
const execa = require('execa')
const chokidar = require('chokidar')
const runCmd = cmd => {
const [proc, ...args] = cmd.split(' ')
return execa(proc, args, {
stdio: 'inherit',
cwd: process.cwd(),
env: process.env,
})
}
let graphqlServerProcess
const killGraphQLServerProcess = () =>
new Promise(resolve => {
if (graphqlServerProcess == null) {
resolve()
}
graphqlServerProcess.kill('SIGTERM', {
forceKillAfterTimeout: 1000,
})
graphqlServerProcess.on('exit', () => resolve())
})
async function generatePhoton() {
try {
await runCmd('node_modules/.bin/prisma2 generate')
} catch (err) {
console.log(err.message)
}
}
async function migrateDatabase() {
try {
await runCmd('node_modules/.bin/rimraf prisma/migrations')
await runCmd('node_modules/.bin/rimraf prisma/dev.db')
await ensureDatabaseExists('dev', true, true)
await runCmd('node_modules/.bin/prisma2 lift save -n migration')
await runCmd('node_modules/.bin/prisma2 lift up --auto-approve')
} catch (err) {
console.log(err.message)
}
}
async function transpileNexus() {
try {
await runCmd('node_modules/.bin/ts-node --transpile-only src/schema')
} catch (err) {
console.log(err.message)
}
}
const createWatcher = (paths, onChange) => {
const watcher = chokidar.watch(paths, {
ignored: 'src/generated',
ignoreInitial: true,
})
watcher
.on('add', onChange)
.on('change', onChange)
.on('unlink', onChange)
.on('unlinkDir', onChange)
return watcher
}
let prismaSchemaWatcher
let nexusSchemaWatcher
migrateDatabase()
.then(() => generatePhoton())
.then(() => transpileNexus())
.then(() => {
graphqlServerProcess = runCmd(
'node_modules/.bin/ts-node-dev --transpileOnly --respawn src/server',
)
graphqlServerProcess.catch(async err => {
console.log(err.message)
process.exit(1)
})
prismaSchemaWatcher = createWatcher(['prisma/schema.prisma'], () =>
migrateDatabase().then(() => generatePhoton()),
)
nexusSchemaWatcher = createWatcher(
['src/schema.ts', 'src/types/**/*'],
transpileNexus,
)
})
process.on('SIGTERM', () => {
prismaSchemaWatcher.unwatch()
nexusSchemaWatcher.unwatch()
killGraphQLServerProcess.then(() => {
process.exit(0)
})
})