-
Notifications
You must be signed in to change notification settings - Fork 3
/
owo.js
executable file
·110 lines (97 loc) · 2.67 KB
/
owo.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
#!/usr/bin/env node
const OwO = require('../');
const snekparse = require('snekparse');
const fs = require('fs');
const { LocalStorage } = require('node-localstorage');
( function main() {
const argv = snekparse(process.argv.slice(2));
if (argv.help || argv.h) return help();
if (argv.init) return init();
const ls = getLS();
const owo = new OwO(ls.getItem('key'));
const command = argv['🐍'].shift();
const args = argv['🐍'];
if (command === 'init') return init();
switch (command) {
case 'shorten': {
owo.shorten(args[0])
.then(code => {
const url = buildOwOURL(ls.getItem('shorten'), code);
process.stdout.write(url);
});
break;
}
case 'upload': {
owo.upload(args[0])
.then(response => {
const path = response.files[0].url;
const url = buildOwOURL(ls.getItem('upload'), path);
process.stdout.write(url);
});
break;
}
default:
break;
}
}());
function init() {
function q(question) {
return new Promise((resolve, reject) => {
prompt.get(question, (err, res) => {
if (err) reject(err);
else resolve(res[question]);
});
});
}
const prompt = require('prompt');
prompt.start();
prompt.message = '\u001b[32mOwO CLI\u001b[39m';
const ls = getLS();
q('OwO API Key').then(key => ls.setItem('key', key))
.then(() => q('Shortening Domain'))
.then(short => ls.setItem('shorten', short))
.then(() => q('File uploading domain'))
.then(file => ls.setItem('upload', file))
.then(() => {;
console.log('Success!');
prompt.stop();
});
}
function help() {
process.stdout.write(`
Usage: owo <command> [args...]
Options:
--init Initialize the client
Commands:
init Initialize the client
shorten Shorten a url
upload Upload a file
`.trimLeft());
}
function getLS() {
function getOSStoragePath() {
switch (process.platform) {
case 'darwin':
return `${process.env.HOME}/Library/Application Support/owo-cli`;
case 'win32':
return `${process.env.APPDATA}\\owo-cli`;
case 'linux':
return `${process.env.HOME}/.owo-cli`;
default:
return '.';
}
}
const OS_STORAGE_PATH = getOSStoragePath();
if (!fs.existsSync(OS_STORAGE_PATH)) fs.mkdirSync(OS_STORAGE_PATH);
const LOCALSTORAGE_PATH = require('path').join(OS_STORAGE_PATH, 'cache');
if (!fs.existsSync(LOCALSTORAGE_PATH)) fs.mkdirSync(LOCALSTORAGE_PATH);
return new LocalStorage(LOCALSTORAGE_PATH);
}
function buildOwOURL(domain, code) {
return require('url').format({
protocol: 'https:',
slashes: true,
host: domain,
pathname: `/${code}`,
});
}