-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
108 lines (89 loc) · 3.38 KB
/
index.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
#!/usr/bin/env node
const DroplrApi = require('droplr-api');
const program = require('commander');
const moment = require('moment');
const chalk = require('chalk');
const Auth = require('./Auth');
const auth = new Auth();
const droplr = new DroplrApi.Client({
auth: new DroplrApi.BasicAuth(auth.username, auth.password)
});
program.version('1.0.5');
program.command('set-auth <username> <password>')
.description('set your droplr authentication')
.action((username, password) => {
auth.setAuth(username, password);
console.log(`Updated credentials.`);
});
program.command('get <link|id>')
.description('get a drop by link or id')
.action(id => {
id = getIdFromLink(id);
droplr.drops.get(id).then(result => {
const data = {
'Title': result.title,
'Link': result.shortlink,
'Type': result.type,
'Active': result.active,
'Boards': result.boards,
'Date Created': new Date(result.createdAt).toLocaleString(),
'Deleted': result.deleted,
'Expires': result.selfDestructType === 'TIME' ? new Date(result.selfDestructValue).toLocaleString() : 'N/A',
};
console.table(data);
}).catch(err => {
console.error(`Encountered error getting drop: ${chalk.white(err.message)}`);
});
});
program.command('expire <link|id> <when>')
.description('set expiration for a drop. can specify "m" for minutes, "h" for hours, or "d" for days. ie: 20m, 1hr, 30d)')
.action((id, when) => {
let num, durationType;
let dt;
id = getIdFromLink(id);
try {
[, num, durationType] = when.match(/^(\d+?)([s|m|h|d|y])$/i);
dt = moment().add(num, durationType);
} catch (err) {
return console.error(`Unable to parse <when> value: ${chalk.white(when)}.`);
}
droplr.drops.update(id, {
selfDestructType: 'TIME',
selfDestructValue: dt.valueOf(),
}).then(result => {
console.log(`Drop [${chalk.cyanBright(id)}] is set to expire ${chalk.cyanBright(dt.calendar().toLowerCase())}.`);
}).catch(err => {
console.error(`Encountered error updating drop: ${chalk.white(err)}`);
});
});
program.command('delete [link|id]')
.description('delete a drop')
.action(id => {
id = getIdFromLink(id);
droplr.drops.delete(id).then(() => {
console.log(`Drop [${chalk.cyanBright(id)}] has been deleted.`);
}).catch(err => {
console.log(`Encountered error deleting drop: ${chalk.white(err.message)}`);
});
});
program.on('command:*', () => {
console.error(`Invalid command: ${chalk.white(program.args.join(' '))}\nSee --help for a list of available commands.`);
});
program.parse(process.argv);
if (!process.argv.slice(2).length) {
program.outputHelp();
}
/**
* Expects link like: https://d.pr/i/FOOBAR
* @param {string} link
* @return {string}
*/
function getIdFromLink(link) {
let url;
try {
url = new URL(link);
} catch (error) {
return link;
}
return url.pathname.split('/').slice(-1).toString();
}