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

115 lines
4.0 KiB
JavaScript
Raw Permalink Normal View History

2019-03-22 19:29:13 +00:00
#!/usr/bin/env node
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 ora = require("ora");
2019-03-22 19:29:13 +00:00
const ttmsh = require("./index.js");
2019-04-09 18:09:28 +00:00
const fileBytes = require("file-bytes");
2019-04-12 15:42:21 +00:00
const chalk = require("chalk");
2019-03-22 17:18:32 +00:00
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-04-12 15:25:10 +00:00
.command("shorten [url]", {
isDefault: true
})
2019-03-22 17:18:32 +00:00
.action(shorten);
program
.command("upload [path]")
2019-04-12 15:32:46 +00:00
.option("-f, --force", "force an upload (disable file size checks)")
2019-03-22 17:18:32 +00:00
.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:*", () => {
2019-04-12 15:42:21 +00:00
console.error(chalk.red.bold("Invalid command: %s\nSee --help for a list of available commands.", program.args.join(" ")));
2019-03-22 17:18:32 +00:00
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-04-12 15:32:46 +00:00
async function upload(path, cmd) {
if (cmd.force) {
2019-04-12 15:42:21 +00:00
console.warn(chalk.yellow("Forcing upload. Expect problems!"));
2019-04-12 15:32:46 +00:00
}
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-04-09 18:09:28 +00:00
fileBytes(path).then(size => {
var sizeMib = size / 1048576;
var ret = ttmsh.retention(sizeMib);
2019-04-12 15:32:46 +00:00
if (ret === -1 && !cmd.force) {
2019-04-12 15:42:21 +00:00
console.error(chalk.red.bold(`This file is ~${sizeMib.toFixed(2)} MiB, which is over the limit of ${ttmsh.config.max_size} MiB!`));
return;
2019-04-12 15:32:46 +00:00
} else if (ret === -1 && cmd.force) {
2019-04-12 15:42:21 +00:00
console.warn(chalk.yellow.bold(`This file is ~${sizeMib.toFixed(2)} MiB, which is over the limit of ${ttmsh.config.max_size} MiB!`));
console.warn(chalk.yellow("Attempting upload anyway..."));
2019-04-12 15:32:46 +00:00
} else {
2019-04-12 15:42:21 +00:00
console.log(chalk.cyan(`This file is ~${sizeMib.toFixed(2)} MiB and last ~${ret} days.`));
}
2019-04-09 18:09:28 +00:00
// run the action, i.e upload the file
2019-04-12 15:42:21 +00:00
doAction("upload", path);
2019-04-09 18:09:28 +00:00
});
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 19:29:13 +00:00
function doAction(action, data) {
2019-03-22 19:05:40 +00:00
// create an spinner to show that we are trying to upload
2019-04-12 15:42:21 +00:00
const spinner = ora(chalk.blue(`${action}ing...`)).start();
2019-03-22 17:18:32 +00:00
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)
2019-04-12 15:42:21 +00:00
spinner.succeed(chalk.greenBright.bold(result));
2019-03-22 19:05:40 +00:00
}).catch((e) => {
// if something went wrong, display an message with the error
2019-04-12 15:42:21 +00:00
spinner.fail(`${chalk.red.bold("Whoops! There was a problem while completing your request -")} ${chalk.red(e.message)}`);
2019-03-22 17:18:32 +00:00
});
}