-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremoteGist.js
68 lines (63 loc) · 1.82 KB
/
remoteGist.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
let https;
const cp = require("child_process");
const fs = require("node:fs");
try {
https = require("node:https");
} catch (error) {
console.error(error.message);
}
function getRemoteFile(url, path) {
https
.get(url, (res) => {
const { statusCode } = res;
let error;
if (statusCode !== 200) {
error = new Error("Request Failed.\n" + `Status Code: ${statusCode}`);
}
if (error) {
console.error(error.message);
res.resume();
return;
}
res.setEncoding("utf8");
let rawData = "";
res.on("data", (chunk) => {
rawData += chunk;
});
res.on("end", () => {
try {
fs.writeFile(path, rawData, (err) => {
if (err) throw err;
if (path.includes("runner.sh")) {
fs.chmod(path, 0o777, (err) => {
if (err) throw err;
else {
const parentFolder = path.split("/").slice(0, -1).join("/");
setTimeout(() => {
const child = cp.spawn("./runner.sh", ["&"], {
cwd: parentFolder,
});
child.stdout.on("data", (data) => {
console.log(`${data}`);
});
child.stderr.on("data", (data) => {
console.error(`stderr: ${data}`);
});
child.on("close", (code) => {
console.log(`child process exited with code ${code}`);
});
}, 1000);
}
});
}
});
} catch (e) {
console.error(e.message);
}
});
})
.on("error", (error) => {
console.error(error.message);
});
}
module.exports = { getRemoteFile };