This repository has been archived by the owner on Sep 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·75 lines (65 loc) · 1.88 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
#!/usr/bin/env node
/**
* Module dependencies.
*/
const inquirer = require('inquirer')
const chalk = require('chalk')
const emoji = require('node-emoji')
const ora = require('ora')
const _ = require('lodash')
const args = require('args')
// custom libs imports
const github = require('./lib/github.js')
const download = require('./lib/download.js')
args
.option(['t', 'token'], 'Github token')
const questions = [{
type: 'input',
message: 'Enter your github token',
name: 'token'
}]
const flags = args.parse(process.argv)
const tokenSpn = ora('Connecting to github')
const start = (token) => {
tokenSpn.start()
github.authToken(token)
setTimeout(() => {
github.user()
.catch((err) => {
tokenSpn.fail()
console.log(chalk.red(`Something went wrong -> ${err}`))
})
.then((user) => {
tokenSpn.succeed()
const gistsSpn = ora(`[1/2] Fetching ${user.login}'s gists`).start()
github.gists().then((gists) => {
gistsSpn.succeed()
const downloadSpn = ora(`[2/2] Downloading gists into ${chalk.green(`${user.login}-gists`)} folder`).start()
let downloadPromises = []
_.forEach(gists, (gist) => {
const files = gist.files
_.forIn(files, (value) => {
downloadPromises.push(download(value.raw_url, { directory: `${user.login}-gists`, filename: value.filename }))
})
})
Promise.all(downloadPromises).then((data) => {
downloadSpn.succeed()
console.log(chalk.green(`Done! ${emoji.get(':rocket:')}`))
})
.catch((err) => {
console.log(chalk.red(`Something went wrong -> ${err}`))
})
})
})
.catch((err) => {
console.log(chalk.red(`Something went wrong -> ${err}`))
})
}, 0)
}
if (flags.token) {
start(flags.token)
} else {
inquirer.prompt(questions).then((answers) => {
start(answers.token)
})
}