-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
198 lines (185 loc) · 6.26 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
"use strict";
var fs = require("fs"),
path = require("path"),
when = require("when"),
sequence = require("sequence"),
commander = require("commander"),
request = require("superagent"),
url = require("url"),
shell = require("shelljs");
var rcPath = findRc();
module.exports.config = {
};
function findRc(){
var home = path.resolve(path.relative("~/.salsarc"));
if(fs.existsSync(home + "/.salsarc")){
return home + "/.salsarc";
}
if(fs.existsSync(__dirname + "/.salsarc")){
return __dirname + "/.salsarc";
}
if(fs.existsSync(__dirname + "/../.salsarc")){
return __dirname + "/../.salsarc";
}
return null;
}
module.exports.loadConfig = function(){
if(rcPath){
try{
module.exports.config = JSON.parse(
fs.readFileSync(path.normalize(rcPath), "utf-8"));
}
catch(err){
console.log("Unable to parse .salsarc: " + err);
}
if (module.exports.config.auth_token) {
// this is the minimum defined config
return true;
}
return false;
}
return false;
};
module.exports.getBowlList = function(){
var d = when.defer(),
templates = {};
sequence().then(function(next){
fs.readdir(__dirname + "/bowls/", next);
}).then(function(next, err, dirs){
dirs.forEach(function(dir){
if(dir.indexOf(".DS_Store") === -1){
templates[dir] = require(__dirname + "/bowls/" + dir + "/salsa_init");
}
});
d.resolve(templates);
});
return d.promise;
};
module.exports.getBowl = function(name){
return require(__dirname + "/bowls/" + name + "/salsa_init");
};
module.exports.copyWebhooks = function(sourceRepo, destRepo){
var p = when.defer();
var webHooks;
sequence().then(function(next){
sourceRepo = url.parse(sourceRepo).pathname.replace(/\/$/, "");
destRepo = url.parse(destRepo).pathname.replace(/\/$/, "");
next();
}).then(function(next){
console.log("Attempting to retrieve webhooks from " + sourceRepo);
request
.get("https://api.github.com/repos" + sourceRepo +
"/hooks?access_token=" + module.exports.config.auth_token)
.end(function(res){
if (res.ok) {
console.log(
"Sucessfully retrieved existing webhooks from " +
sourceRepo + ".");
next(res.body);
}
else {
console.log("Failed to retrieve existing webhooks from " +
sourceRepo + ".");
process.exit(1);
}
});
}).then(function(next, webHooks){
console.log("Copying " + webHooks.length + " webhooks from " +
sourceRepo + " to " + destRepo + "...");
when.all(webHooks.map(function(item, index) {
var d = when.defer();
var hook = {
'name': item.name,
'active': item.active,
'config': item.config
};
var hookString = JSON.stringify(hook);
request
.post("https://api.github.com/repos" + destRepo +
"/hooks?access_token=" + module.exports.config.auth_token)
.send(hookString)
.set("Content-Type", "application/x-www-form-urlencoded")
.set("Content-Length", hookString.length)
.end(function (res){
if(res.ok){
console.log("Successfully added hook " + hook.name +
".");
d.resolve(true);
}
else {
console.log("Failed to add hook " + hook.name + ".");
d.reject();
process.exit(1);
}
});
return d.promise;
}), next);
}).then(function(next){
console.log("Copy complete.");
p.resolve();
});
return p.promise;
};
module.exports.initializeRepo = function(localPath, name, makePrivate, repoUrl,
listOfFiles){
var d = when.defer();
var userName;
console.log("Initializing repository...");
var postRequest = {
'name': name,
'private': makePrivate
};
if (module.exports.config.team_id) {
postRequest.team_id = module.exports.config.team_id;
}
var postRequestString = JSON.stringify(postRequest);
sequence().then(function(next){
repoUrl = url.parse(repoUrl).pathname.replace(/^\/|\/$/g, "");
userName = repoUrl.split("/")[0];
next();
}).then(function(next){
shell.cd(localPath);
next();
}).then(function(next){
shell.exec("npm install");
next();
}).then(function(next){
shell.exec("git init");
next();
}).then(function(next){
shell.exec("git add " + listOfFiles.join(" "));
shell.exec("git status");
next();
}).then(function(next){
shell.exec("git commit -m 'Intial commit'");
next();
}).then(function(next){
request
.post("https://api.github.com/orgs/" + userName +
"/repos?access_token=" + module.exports.config.auth_token)
.send(postRequestString)
.set("Content-Type", "application/x-www-form-urlencoded")
.set("Content-Length", postRequestString.length)
.end(function (res){
if (res.ok){
console.log("Successfully created repo " + name + ".");
next();
}
else {
console.log("Failed to create repo " + name + ".");
console.log("Response:\n" + JSON.stringify(res.body));
process.exit(1);
}
});
}).then(function(next){
shell.exec("git remote add origin git@github.com:" + repoUrl + ".git");
next();
}).then(function(next){
shell.exec("git push origin master");
next();
}).then(function(next){
console.log("Done initializing repository.");
d.resolve();
});
return d.promise;
};