-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·202 lines (181 loc) · 6.84 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env node
var program = require('commander'),
chalk = require('chalk'),
figlet = require('figlet'),
inquirer = require('inquirer'),
clear = require('clear'),
colors = require('colors'),
Client = require('node-rest-client').Client,
path = require('path'),
fs = require('fs'),
storage = require('node-persist');
storage.initSync({
dir: 'config',
stringify: JSON.stringify,
parse: JSON.parse,
encoding: 'utf8',
continuous: true,
forgiveParseErrors: true
})
//setting up the program via commander
program
.version('0.0.1')
.option('-a, --amount <amount>', 'Amount in USD ($)', parseFloat)
.option('-m, --phone_number <phone number>', 'Telephone Number')
.option('-d, --delivery_phone <delivery phone>', 'Mobile Number to which the token will be sent.')
.option('-E, --delivery_email <email>', 'Email Address to which the token will be sent(optional).')
.option('-p, --payment_type <payment type>', 'The preferred type of payment.')
.option('-P, --proxy <proxy:port>', "If for some reason you're behind a proxy.")
.option('-M, --mobile_number <mobile number>', 'Paying Mobile Number (via Ecocash or Telecash).')
.option('-i, --init', 'Setting up default values.')
.parse(process.argv)
var client;
//if user specifies a proxy
if (program.proxy) {
const URL = require('url').URL;
var proxy = new URL(program.proxy);
var options_proxy = (proxy) ? {
proxy: {
host: proxy.hostname,
port: proxy.port,
user: proxy.username,
password: proxy.password,
tunnel: true
}
} : {};
client = new Client(options_proxy);
} else {
client = new Client();
}
//for the extra what what,
const ora = require('ora');
var repos = [];
var Repository;
var repoName;
function runConfig(callback) {
showHeader();
var projectName = program.init;
var directoryName = process.cwd().split('/').splice(-1)[0];
var projectConfig;
const spinner = ora('Please Wait...').start();
spinner.color = 'blue';
var restClient = new Client();
restClient.get('https://api.github.com/search/repositories?q=topic:kwikar-template', {
"headers": {
"User-Agent": "bzmp125"
}
}, function (response) {
if (response.items.length > 0) {
repos = response.items;
var choices = response.items.map(choice => {
return choice.name + ' by ' + choice.owner.login
})
spinner.stop();
inquirer.prompt([{
name: 'ProjectName',
type: 'input',
message: 'Project Name:',
default: directoryName,
validate: function (value) {
if (!value.length) {
console.log(chalk.cyan('\n You will have to provide your Meter Number everytime you use BuyMagetsi.'))
}
return true;
}
},
{
name: 'TemplateName',
type: 'rawlist',
message: 'Choose a template:',
default: choices[0],
choices,
validate: function (value) {
console.log('chosen', value)
if (!value.length) {
console.log(chalk.cyan('\n You will have to provide your Payment Type everytime you use BuyMagetsi.'))
}
return true;
}
}
]).then(callback)
} else {
console.error(chalk.red("Failed to connect to Github, please check internet connection."));
process.exit(1);
}
})
}
if (program.init) {
runConfig((answers) => {
repoName = answers.TemplateName.split(' ')[0]
Repository = repos.map(repo => {
return (repo.name == repoName) ? repo : null;
})[0]
if (!Repository) {
console.error(chalk.red('Kwikar has failed to get the repository for the project.'))
process.exit(0);
}
//confirm config
inquirer.prompt([{
name: 'ConfirmChecksout',
type: 'confirm',
message: 'Is this correct? Project Name: ' + answers.ProjectName + ' & Project Template: ' + answers.TemplateName,
validate: function (checksOut) {
if (checksOut) {
return true;
} else {
runConfig(this)
return '';
}
}
}]).then((checksOutAnswers) => {
if (checksOutAnswers.ConfirmChecksout) {
let ProjectName = answers.ProjectName;
let TemplateName = answers.TemplateName;
let path = process.cwd() + "/" + ProjectName + "/kwikar.json";
// clone repo and cd into project
const spinner = ora('Please Wait...Cloning ' + repoName + ' into ' + ProjectName).start();
spinner.color = 'cyan';
var cmd = require('node-cmd');
cmd.get(
`
git clone ` + Repository.clone_url + ` ` + ProjectName + `
cd ` + ProjectName + `
`,
function (err, data, stderr) {
if (!err) {
spinner.stop();
fs.writeFile(path, JSON.stringify({
'ProjectName': answers.ProjectName,
'TemplateName': answers.TemplateName,
Repository
}), function (e) {
if (e) {
console.error('Failed to create project directory and config. Please try again or upgrade file permissions for this user.')
process.exit(1);
}
console.log(chalk.cyan('Done!'))
})
} else {
console.error('Failed to clone repository. Please try again.')
process.exit(1);
}
}
);
} else {
runConfig(this)
}
})
})
} else {
showHeader();
console.log('More functionality is coming soon! For now check out kwikar --help for more options, or visit https://github.com/bzmp125/kwikar for more information.')
}
function showHeader() {
clear();
console.log('\n \n \n', chalk.cyan(
figlet.textSync('Kwikar', {
horizontalLayout: 'full',
size: 9
})
))
}