-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.js
executable file
·147 lines (123 loc) · 3.85 KB
/
cli.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
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
#!/usr/bin/env node
const fs = require('fs')
const { resolve } = require('path')
const { execSync } = require('child_process')
const { program } = require('commander')
const colors = require('colors')
const logo = require('./logo')
const moreDetails = 'Supported parse-server versions are 5.2.4 - 5.4.0'
program.command('check')
.description('Checks that necessary patches can be applied')
.action(() => {
prepareCheck()
})
program.command('prepare')
.description('Executes patches necessary to run moralis-sync webhooks')
.action(() => {
prepareParse()
})
program.command('eject')
.description('Undos the patches applied in the prepare script')
.action(() => {
prepareEject()
})
program.addHelpText('beforeAll', logo)
program.parse()
function prepareCheck () {
try {
const pkg = loadPackageJson()
verifyPkg(pkg)
checkIfAlreadyPrepared()
// VERIFY PATCHES ARE VALID
applyPatch('patches/bulk-operations.patch', '--check --verbose')
applyPatch('patches/initialize.patch', '--check --verbose')
applyPatch('patches/number-decimal.patch', '--check --verbose')
applyPatch('patches/definitions.patch', '--check --verbose')
applyPatch('patches/options.patch', '--check --verbose')
printLogo()
printSuccess('Server valid. Execute prepare command to apply patches.')
} catch (err) {
printLogo()
printError(err)
}
}
function prepareParse () {
try {
const pkg = loadPackageJson()
verifyPkg(pkg)
checkIfAlreadyPrepared()
// EXECUTE PATCHES
applyPatch('patches/bulk-operations.patch')
applyPatch('patches/definitions.patch')
applyPatch('patches/initialize.patch')
applyPatch('patches/number-decimal.patch')
applyPatch('patches/options.patch')
printLogo()
printSuccess('Server is prepared for moralis-sync webhook')
} catch (err) {
printLogo()
printError(err)
}
}
function prepareEject () {
try {
const pkg = loadPackageJson()
verifyPkg(pkg)
// REVERSE
applyPatch('patches/bulk-operations.patch', '--reverse')
applyPatch('patches/definitions.patch', '--reverse')
applyPatch('patches/initialize.patch', '--reverse')
applyPatch('patches/number-decimal.patch', '--reverse')
applyPatch('patches/options.patch', '--reverse')
printLogo()
printSuccess('Server ejected moralis-sync webhook')
} catch (err) {
printLogo()
printError(err)
}
}
function loadPackageJson () {
const path = resolve(process.cwd(), 'package.json')
let content = ''
try {
content = fs.readFileSync(path)
} catch (err) {
throw new Error(`package.json not found\n\n${moreDetails}`)
}
try {
return JSON.parse(content)
} catch (err) {
throw new Error(`package.json is not valid json\n\n${moreDetails}`)
}
}
function verifyPkg (pkg) {
if (!pkg.version) {
throw new Error(`package.json does not contain a version\n\n${moreDetails}`)
}
}
function checkIfAlreadyPrepared () {
const path = resolve(process.cwd(), 'src/ParseServer.js')
let content = ''
try {
content = fs.readFileSync(path)
} catch (err) {
throw new Error(`src/ParseServer.js not found.\n\n${moreDetails}`)
}
if (content.includes('parse-server-moralis-streams')) {
throw new Error('Prepare script already applied. Undo using the eject command.')
}
}
function applyPatch (patch, opts = '') {
execSync(`git apply ${opts} ${resolve(__dirname, patch)}`, { stdio: [null, null, null] })
}
function printLogo () {
console.log(logo)
}
function printSuccess (text) {
console.log(colors.green('Success'))
console.log(text)
}
function printError (err) {
console.log(colors.red('Error'))
console.log(err.message)
}