2019-03-22 17:18:32 +00:00
|
|
|
const { prompt } = require('enquirer');
|
|
|
|
const program = require('commander');
|
|
|
|
const pkg = require("./package.json");
|
|
|
|
const fs = require("fs");
|
|
|
|
const validUrl = require("valid-url");
|
|
|
|
const request = require("request");
|
|
|
|
const ora = require("ora");
|
|
|
|
const Funnies = require("funnies").Funnies;
|
|
|
|
|
|
|
|
let funnies = new Funnies();
|
|
|
|
|
2019-03-22 19:08:48 +00:00
|
|
|
// configure program with version, description and commands
|
2019-03-22 17:18:32 +00:00
|
|
|
program.version(pkg.version)
|
2019-03-22 19:08:48 +00:00
|
|
|
.description(pkg.description)
|
2019-03-22 17:18:32 +00:00
|
|
|
.command("shorten [url]")
|
|
|
|
.action(shorten);
|
|
|
|
|
|
|
|
program
|
|
|
|
.command("upload [path]")
|
|
|
|
.action(upload);
|
|
|
|
|
2019-03-22 19:08:48 +00:00
|
|
|
// if any command isn't handled, this is run
|
2019-03-22 17:18:32 +00:00
|
|
|
program.on("command:*", () => {
|
|
|
|
console.error("Invalid command: %s\nSee --help for a list of available commands.", program.args.join(" "));
|
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
|
2019-03-22 19:08:48 +00:00
|
|
|
// pass the arguments to the program to be parsed
|
2019-03-22 17:18:32 +00:00
|
|
|
program.parse(process.argv);
|
|
|
|
|
2019-03-22 19:08:48 +00:00
|
|
|
// this function is ran when the upload command is used
|
|
|
|
// 'path' is passed in from the program and *should* be a valid filename
|
2019-03-22 17:18:32 +00:00
|
|
|
async function upload(path) {
|
2019-03-22 19:08:48 +00:00
|
|
|
// if we don't have a path set or the path doesn't exist
|
2019-03-22 17:18:32 +00:00
|
|
|
if (!path || !fs.existsSync(path)) {
|
2019-03-22 19:08:48 +00:00
|
|
|
// ask the user to (re)type a path
|
2019-03-22 17:18:32 +00:00
|
|
|
const response = await prompt([
|
|
|
|
{
|
|
|
|
type: 'input',
|
|
|
|
name: 'path',
|
|
|
|
message: 'Which file would you like to upload?',
|
|
|
|
hint: "Type a file path (e.g file.txt, ../file.txt)",
|
2019-03-22 19:08:48 +00:00
|
|
|
// check that the file exists before we allow the user to submit
|
2019-03-22 17:18:32 +00:00
|
|
|
validate: (input) => fs.existsSync(input) ? true : "Invalid file path"
|
|
|
|
}
|
|
|
|
]);
|
2019-03-22 19:08:48 +00:00
|
|
|
// set the path variable to be the response that we just got
|
2019-03-22 17:18:32 +00:00
|
|
|
path = response.path;
|
|
|
|
}
|
|
|
|
|
2019-03-22 19:08:48 +00:00
|
|
|
// run the action, i.e upload the file
|
|
|
|
doAction("file", path);
|
2019-03-22 17:18:32 +00:00
|
|
|
}
|
|
|
|
|
2019-03-22 19:08:48 +00:00
|
|
|
// this function is ran when the shorten command is used
|
|
|
|
// 'url' is passed in from the program and *should* be a valid url
|
2019-03-22 17:18:32 +00:00
|
|
|
async function shorten(url) {
|
2019-03-22 19:08:48 +00:00
|
|
|
// if we don't have a url set or the url isn't really a url
|
2019-03-22 17:18:32 +00:00
|
|
|
if (!url || !validUrl.isWebUri(url)) {
|
2019-03-22 19:08:48 +00:00
|
|
|
// ask the user to (re)type a url
|
2019-03-22 17:18:32 +00:00
|
|
|
const response = await prompt([
|
|
|
|
{
|
|
|
|
type: 'input',
|
|
|
|
name: 'url',
|
|
|
|
message: 'Which URL would you like to shorten?',
|
|
|
|
hint: "Type a URL (e.g http://tilde.team)",
|
2019-03-22 19:08:48 +00:00
|
|
|
// check that the url is valid before we allow the user to submit
|
2019-03-22 17:18:32 +00:00
|
|
|
validate: (input) => validUrl.isWebUri(input) ? true : "Invalid URL"
|
|
|
|
}
|
|
|
|
]);
|
2019-03-22 19:08:48 +00:00
|
|
|
// set the url variable to be the response that we just got
|
2019-03-22 17:18:32 +00:00
|
|
|
url = response.url;
|
|
|
|
}
|
|
|
|
|
2019-03-22 19:08:48 +00:00
|
|
|
// run the action, i.e shorten the url
|
|
|
|
doAction("shorten", url);
|
2019-03-22 17:18:32 +00:00
|
|
|
}
|
|
|
|
|
2019-03-22 19:05:40 +00:00
|
|
|
// this is run when we need to contact ttm.sh (via the module index.js)
|
2019-03-22 17:18:32 +00:00
|
|
|
function doRequest(action, data) {
|
2019-03-22 19:05:40 +00:00
|
|
|
// create an spinner to show that we are trying to upload
|
|
|
|
// the spinner will have a funny message from 'funnies'
|
2019-03-22 17:18:32 +00:00
|
|
|
const spinner = ora(funnies.message()).start();
|
|
|
|
|
2019-03-22 19:05:40 +00:00
|
|
|
// use the ttmsh module to run the action with the data
|
|
|
|
ttmsh.do(action, data).then((result) => {
|
|
|
|
// if we were successful, display our result (i.e the url)
|
|
|
|
spinner.succeed(result);
|
|
|
|
}).catch((e) => {
|
|
|
|
// if something went wrong, display an message with the error
|
|
|
|
spinner.fail(`Whoops! There was a problem while completing your request - ${err.message}`);
|
2019-03-22 17:18:32 +00:00
|
|
|
});
|
|
|
|
}
|