-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
executable file
·176 lines (147 loc) · 4.23 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
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
175
176
#!/usr/bin/env node
const Gists = require('gists');
const fs = require('fs');
//const ProgressBar = require('progress');
const chalk = require('chalk');
const request = require('superagent');
const co = require('co');
//const prompt = require('co-prompt');
const program = require('commander');
const terminalLink = require('terminal-link');
const exit = require('./success.js');
const prompt = require('prompt-sync')(); // Sync cli input
var CLI = require('clui'),
Spinner = CLI.Spinner;
var username = '', password = '', details = [], privacy = [],
upload_complete = false;
/*
// Using promise (async)
function input_data(fileList) {
return new Promise(function(resolve, reject) {
// Do async job
co(function *() {
username = yield prompt(chalk.yellow('username: '));
password = yield prompt.password(chalk.yellow('password: '));
for(file of fileList)
{
console.log('for file: ' + chalk.cyan(file));
var detail_text = yield prompt(chalk.yellow('description: '));
var privacy_value = yield prompt(chalk.yellow('access [Public(t)/Private(f)]: '));
details.push(detail_text);
privacy.push(privacy_value);
}
});
resolve(true);
})
}
*/
// Using sync command line input
function input_data(fileList) {
username = prompt(chalk.yellow('username: '));
password = prompt.hide(chalk.yellow('password: '));
for(file of fileList)
{
console.log('for file: ' + chalk.cyan(file));
var detail_text = prompt(chalk.yellow('description: '));
var privacy_value = prompt(chalk.yellow('access [Public(t)/Private(f)]: '));
details.push(detail_text);
privacy.push(privacy_value);
}
return true;
}
function loading() {
var countdown = new Spinner('Uploading... ', ['⣾','⣽','⣻','⢿','⡿','⣟','⣯','⣷']);
console.log('Uploading... ');
countdown.start()
// var number = 10;
setInterval(function () {
// number--;
// countdown.message('Uploading... ');
if (upload_complete) {
countdown.stop();
process.stdout.write('\n');
process.exit(0);
}
}, 1000);
}
function main() {
program
.arguments('<file1> [file2]')
.option('-u, --username <username>', 'The user to authenticate as')
.option('-p, --password <password>', 'The user\'s password')
.option('-d, --description <description>', 'Description of the file')
.option('-a, --access <access>', 'Public(t)/Private(f) access for the file. Default is true.')
//.option('-c, --collect [value]', 'A repeatable value', collect, [])
//.option('-f, --multipleFiles <multipleFiles>', 't: multiple files | default: single file')
.action(function() {
//console.log(' collect: %j', program.collect);
var count_upload = 0;
co(function *() {
//console.log(program.args);
var fileList = [], links = [];
fileList = program.args[2]['rawArgs'].filter( function(val, index) {
if(index > 1)
return true;
});
// Taking inputs
var success = false;
success = input_data(fileList);
// After sync input is complete
if(success == true)
{
loading(); // Loading animation
var fileNumber = 0;
for(var file of fileList)
{
var description = details[fileNumber];
var access = privacy[fileNumber];
if(access == 't' || access == 'true')
access = true;
else
if(access == 'f' || access == 'false')
access = false;
else
access = true;
const gists = new Gists({
username: username,
password: password
});
var fileData = fs.readFileSync(file, 'utf8');
++fileNumber;
gists.create(
{
"description": description,
"public": access,
"files": {
[file]: {
"content": fileData
}
}
}
)
.then(function(res, err) {
if(res)
{
const url = terminalLink(res.body.html_url, res.body.html_url);
links.push(url);
++count_upload;
// Checking end of all file uploads
if(count_upload == fileList.length)
{
upload_complete = true;
exit(true, links, fileList);
}
}
})
.catch(function(err) {
console.log(chalk.red(err));
process.exit(1);
});
} // end for
} // end if
});
})
.parse(process.argv);
}
main();
module.exports = main;