-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcli.js
executable file
·174 lines (151 loc) · 3.99 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env node
'use strict';
const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');
const meow = require('meow');
const prompts = require('prompts');
const chalk = require('chalk');
const clipboardy = require('clipboardy');
const normalizeUrl = require('normalize-url');
const isUrl = require('is-url-superb');
const Conf = require('conf');
const firstRun = require('first-run');
const qrcode = require('qrcode-terminal');
const {BitlyClient} = require('bitly');
const config = new Conf();
const adapter = new FileSync('db.json');
const db = low(adapter);
// Bitly Client
const bitly = new BitlyClient(config.get('token'), {});
// Set database defaults
db.defaults({urls: []}).write();
// CLI configuration
const cli = meow(`
Usage
$ bitly <options>
Options
--url -u Shorten a link
--qr -q Generate a QR Code
--list -l List all shortened links
--purge -p Purge the list of saved URLs
--reset -r Reset Generic Access Token
Examples
$ bitly --url --qr kepinski.me
$ bitly -l
$ bitly --reset
`, {
flags: {
url: {
type: 'string',
alias: 'u'
},
qr: {
type: 'boolean',
alias: 'q',
default: false
},
list: {
type: 'boolean',
alias: 'l'
},
purge: {
type: 'boolean',
alias: 'p'
},
reset: {
type: 'boolean',
alias: 'r'
}
}
});
if (firstRun() === true) {
(async () => {
console.log('Welcome to Bitly CLI! Please enter your Generic Access Token below.');
console.log('Follow this guide: https://bit.ly/2CcRl7C\n');
const response = await prompts({
type: 'text',
name: 'token',
message: 'Paste your token here:'
});
config.set('token', response.token);
console.log(chalk.green('Token has been set successfully! Type `bitly --help` for usage instructions :)'));
console.log(chalk.green('If you would like to reset your token, type `bitly --reset`'));
})();
}
if (cli.flags.url) {
(async () => {
// Check if the token exists
if (config.get('token') === undefined) {
console.log(`\n
${chalk.red.bold('Bitly Generic Access Token not found!')}
${chalk.cyan('Try running `bitly --reset`')}
\n`);
}
// Normalize URL
const link = normalizeUrl(cli.flags.url);
// If a string is a valid URL, make it shorter!
if (isUrl(link) === true) {
try {
const result = await bitly.shorten(link);
const long = result.long_url;
const short = result.url;
// Save long & short link to database
db.get('urls')
.push({long, short})
.write();
// Copy to clipboard
clipboardy.write(short);
console.log(`${chalk.bold.green('Success!')} Here is your Bitly URL: ${chalk.cyan(short)} ${chalk.dim.gray('[copied to clipboard]')}`);
// Generate QR Code
if (cli.flags.qr === true) {
qrcode.generate(short, {small: true});
}
} catch (error) {
console.log(chalk.red(error));
process.exit(1);
}
} else {
console.log(chalk.red('Please provide a valid URL!'));
process.exit(1);
}
})();
}
if (cli.flags.list) {
// List all saved URLs
const data = db.get('urls').value();
const list = data.map(v => chalk.yellow(v.long) + ' => ' + chalk.green(v.short) + '\n').toString().replace(/,/g, '');
if (list === '') {
console.log(chalk.red('No URLs found!'));
process.exit(1);
} else {
console.log('Listing saved urls:\n');
console.log(list);
}
}
if (cli.flags.purge) {
(async () => {
// Confirm prompt
const response = await prompts({
type: 'confirm',
name: 'delete',
message: 'Are you sure you want to delete all saved URLs?'
});
if (response.delete === true) {
try {
// Delete all saved URLs
db.unset('urls').write();
console.log(chalk.green('Successfully deleted all saved URLs!'));
} catch (error) {
console.log(chalk.red('Error deleting saved URLs! Are you sure there are any?'));
process.exit(1);
}
} else {
process.exit(1);
}
})();
}
if (cli.flags.reset) {
firstRun.clear();
config.clear();
console.log(chalk.green('Token deleted! Type `bitly` to configure a new one :)'));
}