This repository has been archived on 2019-04-12. You can view files and clone it, but cannot push or open issues or pull requests.
ttmsh-cli/cli.js

102 lines
3.3 KiB
JavaScript
Executable File

#!/usr/bin/env node
const { prompt } = require('enquirer');
const program = require('commander');
const pkg = require("./package.json");
const fs = require("fs");
const validUrl = require("valid-url");
const ora = require("ora");
const Funnies = require("funnies").Funnies;
const ttmsh = require("./index.js");
const fileBytes = require("file-bytes");
let funnies = new Funnies();
// configure program with version, description and commands
program.version(pkg.version)
.description(pkg.description)
.command("shorten [url]")
.action(shorten);
program
.command("upload [path]")
.action(upload);
// if any command isn't handled, this is run
program.on("command:*", () => {
console.error("Invalid command: %s\nSee --help for a list of available commands.", program.args.join(" "));
process.exit(1);
});
// pass the arguments to the program to be parsed
program.parse(process.argv);
// this function is ran when the upload command is used
// 'path' is passed in from the program and *should* be a valid filename
async function upload(path) {
// if we don't have a path set or the path doesn't exist
if (!path || !fs.existsSync(path)) {
// ask the user to (re)type a path
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)",
// check that the file exists before we allow the user to submit
validate: (input) => fs.existsSync(input) ? true : "Invalid file path"
}
]);
// set the path variable to be the response that we just got
path = response.path;
}
fileBytes(path).then(size => {
var sizeMib = size / 1048576;
var ret = ttmsh.retention(sizeMib);
console.log(`This file is expected to last approximately ${ret} days.`);
// run the action, i.e upload the file
doAction("file", path);
});
}
// this function is ran when the shorten command is used
// 'url' is passed in from the program and *should* be a valid url
async function shorten(url) {
// if we don't have a url set or the url isn't really a url
if (!url || !validUrl.isWebUri(url)) {
// ask the user to (re)type a url
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)",
// check that the url is valid before we allow the user to submit
validate: (input) => validUrl.isWebUri(input) ? true : "Invalid URL"
}
]);
// set the url variable to be the response that we just got
url = response.url;
}
// run the action, i.e shorten the url
doAction("shorten", url);
}
// this is run when we need to contact ttm.sh (via the module index.js)
function doAction(action, data) {
// create an spinner to show that we are trying to upload
// the spinner will have a funny message from 'funnies'
const spinner = ora(funnies.message()).start();
// 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}`);
});
}