Add comments and description
This commit is contained in:
parent
9d4074d6c2
commit
5477afc5b9
22
cli.js
22
cli.js
|
@ -9,7 +9,9 @@ const Funnies = require("funnies").Funnies;
|
|||
|
||||
let funnies = new Funnies();
|
||||
|
||||
// configure program with version, description and commands
|
||||
program.version(pkg.version)
|
||||
.description(pkg.description)
|
||||
.command("shorten [url]")
|
||||
.action(shorten);
|
||||
|
||||
|
@ -17,45 +19,61 @@ 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;
|
||||
}
|
||||
|
||||
doRequest("file", path);
|
||||
// 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;
|
||||
}
|
||||
|
||||
doRequest("shorten", 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)
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
"repository": "https://tildegit.org/jakew/ttmsh-cli.git",
|
||||
"author": "Jake Walker <hi@jakew.me>",
|
||||
"license": "GPL-3.0",
|
||||
"description": "A CLI application for interacting with ttm.sh",
|
||||
"description": "A CLI application for interacting with https://ttm.sh/.",
|
||||
"dependencies": {
|
||||
"commander": "^2.19.0",
|
||||
"enquirer": "^2.3.0",
|
||||
|
|
Reference in New Issue