This repository has been archived by the owner on Jun 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·61 lines (57 loc) · 1.87 KB
/
cli.js
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
#!/usr/bin/env node
var waaai = require('./waaai');
var argv = require('yargs')
.usage('Usage: $0 <url> [options]')
.describe('c', 'Create a custom link.')
.describe('p', 'Create a private link.')
.describe('i', 'Get info from short code')
.example('$0 https://github.com/underr/waaai/', 'Create http://waa.ai/4giF')
.example('$0 -p http://yuruyuri.com/', 'Create: http://waa.ai/4gh7/9cf648')
.example('$0 -c smokeeveryday http://420.moe/', 'Create: http://waa.ai/smokeeveryday')
.example('$0 -i 4iLm', 'Gives back link info')
.alias('h', 'help')
.alias('c', 'custom')
.alias('p', 'private')
.alias('i', 'info')
.help('h')
.epilog('http://waaa.ai · https://github.com/underr/waaai')
.argv;
var validURL = /^(http|ftp|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/;
if (argv.i) {
waaai.info(argv.i)
.then(function(result) {
r = result;
code = r.short_code;
clicks = result.clicks.toString();
created = r.date_created;
long = r.long_url;
last = r.last_visited ? r.last_visited : 'Never';
console.log('Short URL: http://waa.ai/' + code + '\nFull URL: ' + long + '\nClicks: ' +
clicks + '\nLast visited: ' + last + '\nDate created: ' + created);
});
} else {
link = argv._[0];
if (!link) {
console.log('You must give a URL. Use --help for usage.');
process.exit(1);
} else if (!link.match(validURL)) {
console.log('Not a valid URL.')
process.exit(1);
}
if (!argv.c && !argv.p) {
waaai.link({url: link})
.then(function(result) {
console.log(result);
});
} else if (argv.c) {
waaai.link({url: link, custom: argv.c})
.then(function(result) {
console.log(result);
});
} else if (argv.p) {
waaai.link({url: link, private: true})
.then(function(result) {
console.log(result);
});
}
}