usr bin env node const argv require yargs argv const readFile require

 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
#!/usr/bin/env node
const argv = require('yargs').argv;
const {
readFile
} = require("mz/fs");
const {
join
} = require("path");
const pkg = require("./package.json");
const codeup = require("./lib");
const transportRegistry = require("./lib/transportRegistry");
const errors = [];
;
(async () => {
try {
const transports = await transportRegistry.loadTransports();
const transportExists = transportRegistry.exists(argv.transport);
const file = argv._[0]; // first only
const transport = argv.transport ? argv.transport : "hasteb.in";
if (argv.version) {
console.log(pkg.version);
process.exit(0);
}
if (argv.list) {
console.log("Available transports: " + Object.keys(transports).join(", "));
process.exit(0);
}
if (!argv.transport) {
if (argv.v) console.warn("No transport defined, using hasteb.in");
} else if (!transportExists) {
errors.push(`Need a defined transport (got ${argv.transport}): --transport <transportname>`);
}
if (!file) {
errors.push("No file specified.");
}
if (errors.length) {
errors.forEach(error => console.error("Error: " + error));
process.exit(1);
}
const filepath = join(file);
const code = await readFile(filepath, "utf8");
const options = {
filename: file,
filetype: file.split(".").reverse()[0]
};
const finalisedUrl = await codeup.run(transports[transport], code, options);
console.log(finalisedUrl);
} catch (err) {
console.error(err);
}
})();