-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathupload.js
44 lines (34 loc) · 1.2 KB
/
upload.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
const { exec } = require('child_process');
module.exports = (username, blockName, filePath) => new Promise((resolve, reject) => {
let isResolved = false
// NOW WE CAN LAUNCH BASH SCRIPT TO UPLOAD SINGLE FILE
const script = exec(`bash add_file.sh ${username} ${blockName} "${filePath}"`);
script.stdout.on('data', (data) => {
console.log('sh:', data)
// resolve with a github link
if (data.includes('raw.githubusercontent.com')) {
data = data.replace(/\n/g, '');
// Check if need to remove garbage before link
link = data.substr(data.indexOf('http'));
resolve(link);
isResolved = true;
};
if (data.includes('ERROR: Upload failed.')) {
// check error type here
// reject({ error: 'unknown error' })
// for now, let's always think the problem is free space
// BUT
// TODO: check if that's a connection error (e.g. when no internet access)
reject({ error: 'no free space' })
isResolved = true;
}
});
script.stderr.on('data', (data) => {
console.log('sh err:', data)
})
script.on('exit', (code, signal) => {
if (!isResolved) {
reject({ code, signal, error: 'unknown error' })
}
})
});