forked from Superalgos/Superalgos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunSetup.js
More file actions
226 lines (203 loc) · 7.14 KB
/
runSetup.js
File metadata and controls
226 lines (203 loc) · 7.14 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
const path = require('path')
const fs = require('fs')
const process = require('process')
const { exec } = require('child_process')
const https = require('https')
const externalScriptsDir = path.join(process.cwd(), 'Platform', 'WebServer', 'externalScripts')
const env = require('../Environment').newEnvironment()
const externalScriptsURLs = env.EXTERNAL_SCRIPTS
const errorResp = (e) => {
console.error(e)
process.exit()
}
// ** export setup piece by piece for more robust tests **
const installExternalScripts = () => {
for (let i = 0; i<externalScriptsURLs.length; i++) {
const url = externalScriptsURLs[i]
const filename = url.split('/').pop()
const dest = path.join(externalScriptsDir, filename)
https.get(url, resp => {
if (resp.statusCode !== 200) {
console.error(
`Error downloading ${url}: HTTP response code ${resp.statusCode}.`
)
return false
} else {
const writeStream = fs.createWriteStream(dest)
resp.pipe(writeStream)
writeStream.on('error', () => console.error('Error writing to ' + path.resolve(dest)))
writeStream.on('finish', () => writeStream.close())
return 5555
}
})
}
return 'External scripts installed'
}
const setUpstreamAndOrigin = async (dir, repo='Superalgos') => {
console.log('Setting up github upstream and origin...')
// initialize simpleGit
const simpleGit = require('simple-git')
const options = {
binary: 'git',
maxConcurrentProcesses: 6,
}
// main app repo should be the working directory
if (repo === 'Superalgos') options.baseDir = dir || process.cwd()
// if repo is not main app repo, assume it is a plugin, in ./Plugins.
else options.baseDir = path.join(process.cwd(), 'Plugins', dir)
const git = simpleGit(options)
// Check to see it main repo has been set as upstream
let remotes = await git.getRemotes(true).catch(errorResp)
let isUpstreamSet
let origin
for (let remote in remotes) {
if (remotes[remote].name === 'upstream') {
isUpstreamSet = true
} else {
isUpstreamSet = false
if (remotes[remote].name === 'origin') {
origin = remotes[remote].refs.fetch
}
}
}
// If upstream has not been set. Set it now
if (isUpstreamSet === false) {
await git.addRemote('upstream', `https://github.com/Superalgos/${repo}.git`).catch(errorResp)
}
let gitUser
let usesSSH
// if in main Superalgos repo, set gitUser from origin
if (repo === 'Superalgos' && origin) {
if (origin.indexOf('@') === -1) {
gitUser = origin.split('/')[3]
usesSSH = false
} else {
gitUser = origin.split(':')[1].split('/')[0]
usesSSH = true
}
}
// Check that branches exist
let branchSumAll = await git.branchLocal().catch(errorResp)
let masterExists = false
let developExists = false
for (let i = 0; i < branchSumAll.all.length; i++) {
if (branchSumAll.all[i] === 'master') masterExists = true
else if (branchSumAll.all[i] === 'develop') developExists = true
}
if (!masterExists) {
await git.checkout(['-B', 'master'])
}
if (!developExists) {
await git.checkout(['-B', 'develop'])
}
// Check that a branch is checked out, otherwise checkout develop
branchSumAll = await git.branchLocal().catch(errorResp)
if (branchSumAll.current === '' || branchSumAll.current === 'master') {
await git.checkout(['-B', 'develop'])
}
if (repo !== 'Superalgos' && origin && gitUser) {
await git.removeRemote('origin').catch(errorResp)
}
if (repo !== 'Superalgos' && gitUser) {
let orURL
if (usesSSH) {
await git.addRemote('origin', orURL).catch(errorResp)
}
else {
orURL = `https://github.com/${gitUser}/${repo}.git`
await git.addRemote('origin', orURL).catch(errorResp)
}
}
console.log('Set upstream and origin for github')
return 'Set upstream and origin for github'
}
const runSetup = async (tfjs=false) => {
// Output the Logo
showLogo()
// Install Node_Modules to Main Superalgos Directory
// install tensorflow if user ran tensorflow setup file
if (tfjs !== false) {
console.log('Including tensorflow.js in your setup...')
path.join(process.cwd(),
"Projects",
"TensorFlow",
"TS",
"Bot-Modules",
"Learning-Bot",
"Low-Frequency-Learning")
}
let dir = process.cwd()
await runInstallCommands()
.then(() => setUpstreamAndOrigin())
.catch(errorResp)
// Donload external scripts
console.log('')
console.log('Setting up your environment …')
console.log('')
installExternalScripts()
return 'Setup complete'
}
async function runInstallCommands() {
try {
await executeCommand('echo Results of install at ' + process.cwd() + ' & npm ci --omit=optional')
}
catch(err) {
if(err.message.indexOf('package.json and package-lock.json') > -1) {
console.error('npm ci failed package.json and package-lock.json are not in sync')
console.log('running npm install --omit=optional')
await executeCommand('npm install --omit=optional')
}
else {
throw err
}
}
}
function executeCommand(command) {
return new Promise((resolve, reject) => {
const child = exec(command, {cwd: process.cwd()}, (err, stdout) => {
if(err) {
reject(err)
return
}
resolve()
})
try {
child.stdout.pipe(process.stdout)
child.on('exit', () => {
console.log('')
console.log('Finished ' + command)
console.log('')
})
}
catch(e) {
reject(e)
}
})
}
function showLogo () {
console.log('\x1b[31m%s\x1b[0m', ' ///////')
console.log('\x1b[31m%s\x1b[0m', ' , ///////')
console.log('\x1b[31m%s\x1b[0m', ' //////')
console.log('\x1b[31m%s\x1b[0m', ' * ,/////')
console.log('\x1b[31m%s\x1b[0m', ' (( /////')
console.log('\x1b[31m%s\x1b[0m', ' /(( /// ////')
console.log('\x1b[31m%s\x1b[0m', ' ((((( ((//// ////')
console.log('\x1b[31m%s\x1b[0m', ' ,((((((((( (((////* ///')
console.log('\x1b[31m%s\x1b[0m', ' (((((((((. (((((///// //')
console.log('\x1b[31m%s\x1b[0m', ' (((((((* ((((((((////// /,')
console.log('\x1b[31m%s\x1b[0m', ' (((((( ((((((((((//////// /')
console.log('\x1b[31m%s\x1b[0m', ' (((( ((((((((((/////////')
console.log('\x1b[31m%s\x1b[0m', ' ((( /////')
console.log('\x1b[31m%s\x1b[0m', ' *(((/.')
console.log('\x1b[31m%s\x1b[0m', ' /(((///// /')
console.log('\x1b[31m%s\x1b[0m', ' ///////// ///')
console.log('\x1b[31m%s\x1b[0m', ' //////////////')
console.log('\x1b[31m%s\x1b[0m', ' ////////')
}
module.exports = {
runSetup,
setUpstreamAndOrigin,
installExternalScripts,
errorResp
}