-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·147 lines (138 loc) · 3.77 KB
/
index.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 yargs = require("yargs/yargs");
const { hideBin } = require("yargs/helpers");
const axios = require("axios");
const fileDownload = require("js-file-download");
const SYTRAN_URL = "https://sytran-backend.herokuapp.com/";
function isFileExist(path) {
try {
if (fs.existsSync(path)) {
return true;
} else {
return false;
}
} catch (err) {
console.error(err);
}
}
function getFileName(url) {
return url.substring(url.lastIndexOf("/") + 1);
}
async function downloadFile(fileUrl) {
if(process.env.PWD) {
outputLocationPath = process.env.PWD;
} else {
outputLocationPath = process.cwd();
}
const writer = fs.createWriteStream(
outputLocationPath + "/" + getFileName(fileUrl)
);
return axios({
method: "get",
url: fileUrl,
responseType: "stream",
}).then((res) => {
//ensure that the user can call `then()` only when the file has
//been downloaded entirely.
return new Promise((resolve, reject) => {
res.data.pipe(writer);
let error = null;
writer.on("error", (err) => {
error = err;
writer.close();
reject(err);
});
writer.on("close", () => {
if (!error) {
resolve(true);
}
//no need to call the reject here, as it will have been called in the
//'error' stream;
});
});
});
}
yargs(hideBin(process.argv))
.command(
"push [file] [short]",
"push a file to the server with a shorthand",
(yargs) => {
yargs
.positional("file", {
describe: "path to file",
})
.positional("short", {
describe: "short phrase to use as an identifier on the server",
});
},
(argv) => {
if (argv.file) {
if (isFileExist(argv.file)) {
if (argv.short) {
const FormData = require("form-data");
const form = new FormData();
form.append("item[name]", argv.short);
form.append("item[file]", fs.createReadStream(argv.file));
axios
.post(SYTRAN_URL + "items/", form, { headers: form.getHeaders() })
.then(
(res) => {
console.log("sent!");
},
(err) => {
if (err.response.status == 422) {
console.log(
"Error",
err.response.status,
"-",
err.response.statusText,
"(usually need to pick a unique alphanumeric shorthand)"
);
} else {
console.log(err);
}
}
);
} else {
console.log("missing shorthand");
}
} else {
console.log("File could not be found");
}
} else {
console.log("missing path to file");
}
}
)
.command(
"pull [short]",
"pull from the server using a shorthand",
(yargs) => {
yargs.positional("short", {
describe: "short phrase to use as an identifier on the server",
});
},
(argv) => {
if (argv.short) {
axios.get(SYTRAN_URL + "items/" + argv.short).then(
(res) => {
downloadFile(SYTRAN_URL + res.request.socket._httpMessage.path);
},
(err) => {
console.log(err);
if (err.response.status == 404) {
console.log(
"file either doesn't exist or has been removed! (all files get removed once every 24 hours)"
);
} else {
console.log(err);
}
}
);
} else {
console.log("need short");
}
}
)
.argv;