-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
executable file
·340 lines (322 loc) · 9.98 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env node
var program = require('commander');
var emoji = require('node-emoji');
var B = require('br4nch');
var _ = require('underscore');
var C = require('c0mm1t');
var I = require('1n1t');
var m4g1c = require('m4g1c');
var async = require('async');
var colors = require('colors');
var E = require('3x3c');
var exist = require('3x1st');
var updateNotifier = require('update-notifier');
var pkg = require('./package.json');
var CR = require('cr34t3');
var inquirer = require('inquirer');
var S = require('./lib/git-heads');
var opn = require('opn');
var isGitUrl = require('is-git-url');
var notify = require('./lib/Notify');
const assets = require('./lib/Assets.js');
var globalModulesDir = require('global-modules');
const os = require('os');
updateNotifier({pkg}).notify();
program
.option('-m, --message <message>', 'Commit message')
.option('-b, --new_branch <branch>', 'Git push origin as a new branch')
.option('-p, --publish', 'After process publish repo as a npm package')
.option('-i, --init', 'Git init')
.option('-s, --status', 'Git status')
.option('-c, --create', 'Create github repository')
.option('--clone', 'Clone github repository')
.option('-u, --update', 'Self update')
.option('-e, --enable', 'Enable Auto Commit')
.option('-d, --disable', 'Disable Auto Commit')
.parse(process.argv);
function sleep(ms) {
return new Promise(function(resolve, reject) {
setTimeout(function () {
resolve();
}, ms * 1000);
});
}
function log(message) {
console.log(emoji.emojify(':zap:'), colors.underline.white('Running') ,colors.cyan(message));
}
if (process.argv.length === 2) {
opn('https://github.com/svtek/g3l#usage');
process.exit()
}
if (program.enable) {
exist(`${process.cwd()}/.gitignore`)
.then(() => {
console.log(colors.green('g3l auto committer enabled in', process.cwd(), 'successfully. Happy coding..', emoji.emojify(':relaxed:')));
var pm2 = require('pm2');
var globalModulesDir = require('global-modules');
pm2.connect(function(err) {
if (err) {
console.log(err);
process.exit(2);
}
pm2.start({
name: process.cwd(),
script : `${globalModulesDir}/g3l/lib/AutoCommit.js`,
exec_mode : 'fork',
max_memory_restart : '100M'
}, function(err, apps) {
pm2.disconnect();
if (err) console.log(err);
});
});
})
.catch(() => {
console.log(colors.red(`Please create .gitignore`));
})
} else if (program.disable) {
console.log(colors.white('g3l auto committer disabled in', process.cwd(), 'successfully. '));
var pm2 = require('pm2');
pm2.delete(process.cwd(), function(err) {})
pm2.disconnect();
sleep(2)
.then((value) => {
console.log('Bye bye..', emoji.emojify(':white_frowning_face:'));
process.exit();
})
}
var commands = [
{
name: "new_branch",
command: "git checkout -b ${branch}",
description: "Git checkout new branch easily",
priority: 99,
boolean: false,
containRequiredParam: true,
function: "branch",
params: [
{
name: "branch",
}
]
},
{
name: "init",
command: "git init",
description: "Git checkout new branch easily",
priority: 100,
boolean: true,
containRequiredParam: false,
function: "init",
params: []
},
{
name: "message",
command: "git add . && git commit -m ${message} && git push origin ${branch}",
description: "Git commit easily",
priority: 95,
boolean: false,
containRequiredParam: true,
function: "message",
params: [
{
name: "message",
}
],
},
{
name: "publish",
command: "npm version patch && npm publish",
description: "Npm publish easily",
boolean: true,
function: "publish",
priority: 80,
containRequiredParam: false,
params: [],
},
{
name: "status",
command: "git status",
description: "Git status",
boolean: true,
function: "status",
priority: 90,
containRequiredParam: false,
params: [],
},
{
name: "create",
command: "git create new repo",
description: "Create new github repository",
boolean: true,
function: "create",
priority: 90,
containRequiredParam: false,
params: [],
},
{
name: "clone",
command: "Git clone existing repository",
description: "Git clone existing repository",
boolean: true,
function: "clone",
priority: 89,
containRequiredParam: false,
params: [],
},
{
name: "update",
command: "Self update",
description: "Self update",
boolean: true,
function: "update",
priority: 1,
containRequiredParam: false,
params: [],
}
];
function run(array) {
return new Promise(function(resolve, reject) {
array.forEach(function(piece) {
if (eval('program.'+piece.name)) {
if (piece.containRequiredParam || piece.boolean || eval('program.' + piece.name).length > 2) { /* If is has contain required param? */
if (piece.boolean) {
eval(piece.function + '(' + JSON.stringify(piece) + `).then((value) => {console.log(colors.grey(value));}).catch((err) => {console.log(colors.red(err));})`);
} else {
piece.params.forEach(function(param) {
eval(piece.function + '(' + JSON.stringify(piece) + ').then((value) => {console.log(colors.grey(value));}).catch((err) => {console.log(colors.red(err));})');
});
}
} else {
process.exit();
}
}
});
resolve('')
});
}
/* Sort by priority */
commands = _.sortBy(commands, 'priority').reverse();
/* Run commands */
run(commands)
.then((value) => {console.log(value);})
.catch((err) => {console.log(err);})
function init(command) {return new Promise(function(resolve, reject) {I().then((value) => {resolve(value)}).catch((err) => {reject(err)});});}
function branch(command) {
return new Promise(function(resolve, reject) {
log('Checkout new branch');
E(`git checkout -b ${program.new_branch}`)
.then((value) => {
notify({title: 'g3l', 'message': `New branch created ${program.new_branch.trim()}`, 'status':'resolve'})
resolve(`New branch created: ${program.new_branch}`);
})
.catch((err) => {
notify({title: 'g3l', 'message': `New branch doesn\'t created as: ${program.new_branch.trim()}`, 'status':'reject'})
reject(err)
});
});
}
function message(command) {
return new Promise(function(resolve, reject) {
log('Git commit and push proccess');
var obj = {
message: program.message,
new: program.init,
}
C(obj)
.then(function(value) {
notify({title: 'g3l', 'message': 'Git committed successfully', 'status':'resolve'})
resolve('Git committed successfully.');
})
.catch(function(err) {
notify({title: 'g3l', 'message': `Git doesn\'t committed successfully.`, 'status':'reject'})
reject(err)
});
});
}
function publish(command) {
return new Promise(function(resolve, reject) {
log('Version patch and publish as npm package');
var isNew = program.init ? '' : 'npm version patch &&';
E(`${isNew} npm publish`)
.then((value) => {
notify({title: 'g3l', 'message': 'Npm publish successfully', 'status':'resolve'})
resolve(value);
})
.catch((err) => {
notify({title: 'g3l', 'message': `Npm doesn\'t published successfully.`, 'status':'reject'})
reject(err);
})
});
}
function status(command) {
return new Promise(function(resolve, reject) {
notify({title: 'g3l', 'message': 'Git status fetched successfully', 'status':'resolve'})
S();
resolve('Git status fetched successfully');
});
}
function update(command) {
return new Promise(function(resolve, reject) {
log('g3l update process started.')
E('npm i -g g3l')
.then((value) => {
notify({title: 'g3l', 'message': 'g3l updated successfully.', 'status':'resolve'})
resolve('g3l updated successfully.')
})
.catch((err) => {
notify({title: 'g3l', 'message': `g3l doesn\'t updated.`, 'status':'reject'})
reject('g3l doesn\'t updated.')
})
});
}
function create(command) {
return new Promise(function(resolve, reject) {
CR()
.then((value) => {
log('Your repository url: ', value.clone.url);
opn(value.clone_url);
notify({title: 'g3l', 'message': 'g3l created github repository successfully', 'status':'resolve'})
resolve(emoji.emojify(`:sunglasses: Horarayy! You can init your repository easily with this command: g3l -i`));
process.exit();
}).catch((err) => {
notify({title: 'g3l', 'message': `Repository doesn\'t created successfully.`, 'status':'reject'})
reject(err);
});
});
}
function clone(command) {
return new Promise(function(resolve, reject) {
var questions = [
{
type: 'input',
name: 'url',
message: 'What\'s your git repository url?',
validate: function (value) {
if(value.indexOf('.git') > -1 && value.trim().length > 1) {
return true;
}
return 'Please write correct git repository.'
}
},
{
type: 'input',
name: 'name',
message: 'Which name you prefer for cloned repository directory name? (Optional)'
},
];
inquirer.prompt(questions).then(function (answers) {
if (answers.name.trim().length < 1) {
answers.name = answers.url.split('/').pop(-1).split('.git')[0];
}
E(`git clone ${answers.url} ${answers.name} && cd ${answers.name}`)
.then((value) => {
notify({title: 'g3l', 'message': 'Clone', 'subtitle': `Git clone successfully`, 'status':'resolve'})
resolve('Clone done!')
})
.catch((err) => {
notify({title: 'g3l', 'message': `Repository doesn\'t cloned successfully.`, 'status':'reject'})
reject(err)
});
});
});
}