-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
133 lines (120 loc) · 2.54 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
#!/usr/bin/env node
const fs = require("fs");
const meow = require('meow');
const chalk = require('chalk');
const Listr = require('listr');
const { Observable } = require('rxjs');
const opn = require('better-opn');
const clipboardy = require('clipboardy');
const { Octokit } = require("@octokit/rest");
const settingsHandler = require("./src/settings")
const cli = meow(`
${chalk.bold("Usage")}
$ gistash <single|multiple file>
${chalk.bold("Options")}
-p, --public Set GitHub gist as public (default: false)
-c, --copy Copy GitHub gist url to clipboard (default: false)
-o, --open Open GitHub gist url in browser
-m, --message GitHub gist description
-v, --version gistash CLI version
-h, --help Showing all available commands
`, {
flags: {
public: {
type: 'boolean',
alias: 'p',
default: false
},
copy: {
type: 'boolean',
alias: 'c',
default: false
},
open: {
type: 'boolean',
alias: 'o',
default: false
},
message: {
type: 'string',
alias: 'm',
default: "Upload using 'gistash' 👉🏻 https://github.com/mittalyashu/gistash"
},
help: {
type: 'boolean',
alias: 'h',
default: false
}
}
});
const filesInput = cli.input;
const {
public,
copy,
open,
message,
help
} = cli.flags;
(async () => {
if (!filesInput && help) {
console.log(cli.help);
process.exit(1);
}
const settings = {
...(await settingsHandler.get())
}
const octokit = new Octokit({
auth: settings.token
});
let rawFile = {};
let gist;
const tasks = new Listr([
{
title: "Reading",
task: async () => {
return new Observable(observer => {
for (let i = 0; i < filesInput.length; i++) {
const fileNameArray = filesInput[i].split("/")
const fileName = fileNameArray[fileNameArray.length - 1]
observer.next(filesInput[i]);
file = fs.readFileSync(filesInput[i], 'utf8')
rawFile[`${fileName}`] = {
content: file
}
}
observer.complete();
});
}
},
{
title: "Uploading",
task: async () => {
const data = await octokit.gists.create({
files: rawFile,
description: message,
public
})
try {
gist = data.data
} catch (error) {
throw new Error(`${chalk.red("Something unexpected happened!!!")}`);
}
}
},
{
title: "Opening url in browser",
skip: () => !open,
task: () => {
opn(gist.html_url)
}
},
{
title: "Copy url to clipboard",
skip: () => !copy,
task: () => {
clipboardy.writeSync(gist.html_url);
}
}
]);
tasks.run()
})();