Skip to content

Commit 19bd3ec

Browse files
authored
Merge pull request #1 from Cloudiverse-Github/beta
Beta
2 parents 0df18e3 + 5370c2f commit 19bd3ec

File tree

1 file changed

+53
-29
lines changed

1 file changed

+53
-29
lines changed

index.js

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ var child
1313
// put npm i commands in try catch stuff
1414
var log = console.log
1515
var error = console.error
16-
console.log = function(){
17-
log(arguments)
16+
console.log = function () {
17+
log.apply(null, arguments);
1818
var text = "";
1919
Array.prototype.slice.call(arguments).forEach(arg => {
2020
if (typeof arg === "object") arg = JSON.stringify(arg);
@@ -30,9 +30,8 @@ console.log = function(){
3030
}).catch(err => err)
3131
}
3232

33-
console.error = function(){
34-
error(arguments)
35-
log("This was an error")
33+
console.error = function () {
34+
error.apply(null, arguments);
3635
var text = "";
3736
Array.prototype.slice.call(arguments).forEach(arg => {
3837
if (typeof arg === "object") arg = JSON.stringify(arg);
@@ -70,7 +69,7 @@ async function main() {
7069
try {
7170
child_process.execSync("cd code; rm -rf node_modules")
7271
child_process.execSync("cd code; npm install --force")
73-
}catch(err){
72+
} catch (err) {
7473
console.error(err.toString())
7574
stop = true
7675
}
@@ -84,12 +83,37 @@ async function main() {
8483
time: Date.now()
8584
})
8685
}).catch(err => err)
87-
}else{
86+
} else {
8887
if (!environmentalVariables.query.environmentalVariables) {
8988
environmentalVariables.query.environmentalVariables = {}
9089
}
9190
environmentalVariables.query.environmentalVariables.PORT = 8888
92-
91+
try {
92+
let jsonfile = fs.readFileSync("./code/package.json").toString()
93+
jsonfile = JSON.parse(jsonfile);
94+
var nonCustom = ["node", "cd", "ls", "npx", "npm"]
95+
Object.entries(jsonfile.scripts).forEach(([key, value]) => {
96+
if (value !== undefined) {
97+
let allCmds = value.split(";");
98+
for (let i = 0; i < allCmds.length; i++) {
99+
var add = true;
100+
for (let p = 0; p < nonCustom.length; p++) {
101+
if (allCmds[i].trimStart().startsWith(nonCustom[p])) {
102+
add = false;
103+
}
104+
}
105+
if (add === true) {
106+
allCmds[i] = "npx --no-install " + allCmds[i].trimStart();
107+
}
108+
}
109+
allCmds = allCmds.join("; ");
110+
jsonfile.scripts[key] = allCmds;
111+
}
112+
})
113+
fs.writeFileSync("./code/package.json", JSON.stringify(jsonfile))
114+
} catch (err) {
115+
//console.log(err)
116+
}
93117
child = child_process.exec(`cd code; ${environmentalVariables.query.runCmd || "npm run start"}`, {
94118
env: environmentalVariables.query.environmentalVariables
95119
});
@@ -110,7 +134,7 @@ async function main() {
110134
})
111135
}).catch(err => err)
112136
console.log(colors.bold.red(`Child process exited with code ${code}`));
113-
});
137+
});
114138
}
115139
postData();
116140
async function postData() {
@@ -123,8 +147,8 @@ async function main() {
123147
if (running === true && child.pid) {
124148
try {
125149
currentResources = await pidusage(child.pid);
126-
}catch(err) {
127-
currentResources = {cpu: 0, memory: 0}
150+
} catch (err) {
151+
currentResources = { cpu: 0, memory: 0 }
128152
}
129153
}
130154
let dataRes = await fetch(postUrl, {
@@ -147,7 +171,7 @@ async function main() {
147171
})
148172
})
149173
try {
150-
runChild = child_process.exec("cd code; "+json.query.runCmd, {
174+
runChild = child_process.exec("cd code; " + json.query.runCmd, {
151175
env: environmentalVariables.query.environmentalVariables
152176
});
153177
runChild.stdout.on('data', (data) => {
@@ -156,7 +180,7 @@ async function main() {
156180
runChild.stderr.on('data', (data) => {
157181
console.log(data);
158182
});
159-
}catch(err){
183+
} catch (err) {
160184
console.log(err.toString())
161185
}
162186
}
@@ -185,22 +209,22 @@ async function main() {
185209
console.log("Extracting files...")
186210
await extract("./temp.zip", { dir: __dirname + "/temp-extracted" });
187211
let directory = fs.readdirSync("./temp-extracted/");
188-
fs.moveSync("./temp-extracted/"+directory[0], "./code", function (err) {
189-
if (err) {
190-
console.error(err);
212+
fs.moveSync("./temp-extracted/" + directory[0], "./code", function (err) {
213+
if (err) {
214+
console.error(err);
191215
} else {
192-
console.log("Moved folders successfully!");
216+
console.log("Moved folders successfully!");
193217
}
194-
});
218+
});
195219
if (running === true) {
196220
child.kill("SIGKILL");
197221
}
198222
return restartProcess();
199223
} else {
200224
await sleep(60000)
201225
}
202-
}catch(err){
203-
console.log("We had trouble posting to the cloudiverse servers! Please check your internet connection. Processes will continue running. \n Full error string: "+err.toString())
226+
} catch (err) {
227+
console.log("We had trouble posting to the cloudiverse servers! Please check your internet connection. Processes will continue running. \n Full error string: " + err.toString())
204228
}
205229
}
206230
}
@@ -212,10 +236,10 @@ async function restartProcess() {
212236
if (running === true) {
213237
child.kill("SIGKILL");
214238
}
215-
try{
239+
try {
216240
child_process.execSync("lsof -i :8888 -sTCP:LISTEN |awk 'NR > 1 {print $2}' |xargs kill -15")
217-
}catch(err){
218-
241+
} catch (err) {
242+
219243
}
220244

221245
console.log("Restarting process due to code changes...")
@@ -232,12 +256,12 @@ const downloadFile = (async (url, path) => {
232256
});
233257
});
234258
process()
235-
async function process(){
236-
237-
initialize()
238-
main();
259+
async function process() {
260+
261+
initialize()
262+
main();
239263
}
240-
async function initialize(){
264+
async function initialize() {
241265
console.log("Initializing...")
242-
fetch(postUrl.replace("/api/v1/dump-status/", "/api/v1/user/deploy/")+"?source=server")
266+
fetch(postUrl.replace("/api/v1/dump-status/", "/api/v1/user/deploy/") + "?source=server")
243267
}

0 commit comments

Comments
 (0)