/
index.js
executable file
·164 lines (137 loc) · 3.9 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
#!/usr/bin/env node
var program = require('commander')
var Github = require('github')
var Git = require('gift')
var Promise = require("bluebird")
var updateMarkdown = require('./lib/utils').updateMarkdown
var fs = require('fs')
var readlineSync = require('readline-sync');
var mkpath = require('mkpath');
var path = require('path')
var package = require('./package.json')
var configPath = process.env['HOME']
var authPath
var templatePath
program
.version(package.version)
.usage('[options] <name>')
.option('-p, --private', 'Create a private gist')
.option('-d, --desc', 'Set a description')
.parse(process.argv)
var template = [
"#{{name}}",
"{{description}}",
"",
"---",
"{{html_url}} ",
"Created on `{{created_at}}`"
].join('\n');
if(!fs.existsSync(process.env['HOME'] + '/.config/engist')) {
var answer = readlineSync.question('Would you like to setup engist now [Y/n]?');
console.log("")
if(answer != 'n') {
var path = process.env['HOME'] + '/.config/engist';
console.log(" - Creating directory '%'", path);
mkpath.sync(path);
console.log(" - Creating auth.json");
fs.writeFileSync(path + '/auth.json', JSON.stringify({
type: 'oauth',
token: ''
}, null, ' '))
console.log(" - Creating template.md");
fs.writeFileSync(path + '/template.md', template)
console.log("")
console.log("Before you can use engist you must setup your auth.");
console.log("")
console.log("Engist supports the following auth schemas, just update")
console.log("%s to which ever you choose", path + '/auth.json');
console.log("You can setup oauth2 from github: https://github.com/settings/applications");
var basic = {
type: "basic",
username: "octocat",
password: "kittens"
}
var oauth = {
type: "oauth",
token: "token",
}
var oauth_secret = {
type: "oauth",
key: "clientID",
secret: "clientSecret"
}
console.log("")
console.log("Basic")
console.log(JSON.stringify(basic, null, ' '))
console.log("")
console.log("OAuth2")
console.log(JSON.stringify(oauth, null, ' '))
console.log("")
console.log("OAuth2 Key/Secret")
console.log(JSON.stringify(oauth_secret, null, ' '))
console.log("")
console.log("")
console.log("You can also configure the default template for the README.md, which ")
console.log("is located at '%s'", path + '/template.md')
}
return;
}
if(program.args.length == 0) {
console.error('No name given!')
process.exit(1);
}
program.name = program.args[0];
if (fs.existsSync(program.name)) {
var contents = fs.readdirSync(program.name)
if(contents.length > 0) {
console.error("fatal: destination path '%s' already exists and is not an empty directory.", program.name);
return 1;
}
}
var github = new Github({
version: "3.0.0",
})
github.authenticate(
require(process.env['HOME'] + '/.config/engist/auth.json')
)
var readFile = Promise.promisify(fs.readFile)
var gistCreate = Promise.promisify(github.gists.create)
var gistEdit = Promise.promisify(github.gists.edit)
var gitClone = Promise.promisify(Git.clone)
var gistConfig = {
description: program.desc || "",
public: !program.private,
files: {
"README.md": {
"content": "..."
}
}
}
var template = readFile(process.env['HOME'] + '/.config/engist/template.md')
Promise.all([
gistCreate(gistConfig),
template
]).spread(function(gist, markdown) {
markdown = markdown.toString('utf-8');
if(gist.description.length == 0) {
// strip the description if it's empty
markdown = markdown.replace('{{description}}\n', '');
}
markdown = markdown.replace('{{name}}', program.name);
markdown = updateMarkdown(markdown, gist)
return gistEdit({
id: gist.id,
files: {
"README.md": {
"content": markdown
}
}
})
}).then(function(gist) {
console.log("Your gist is located at: %s", gist.html_url)
return gitClone("git@gist.github.com:/" + gist.id + ".git", process.cwd() + "/" + program.name)
}).then(function(repo) {
})
.catch(function(err) {
console.log(err)
})