forked from bitovi/jquerypp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-github-download.js
More file actions
273 lines (216 loc) · 6.08 KB
/
update-github-download.js
File metadata and controls
273 lines (216 loc) · 6.08 KB
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
var path = require("path"),
fs = require("fs"),
spawn = require("child_process").spawn,
// Third party modules
program = require("commander"),
GitHubApi = require("github"),
_ = require("underscore"),
s3p = require("./s3-post.js"),
mime = require('mime'),
// Get the current version
version = fs.readFileSync(path.join(__dirname, "../version")).toString("utf8").trim(),
// Describe all the files we'll be uploading to Github
descriptions = {
"jquerypp.js" : "jQuery++ #{VERSION} - All plugins",
},
// Figure out some paths
rhinoPath = path.join(__dirname, "../../.."),
distPathShort = "jquery/dist/",
distPath = path.join(__dirname, "../../dist"),
// Github client
github = new GitHubApi({
version : "3.0.0"
}),
remote = "git@github.com:jupiterjs/jquerypp.git",
// Timeouts
stealTimeout,
// For Github credentials
username,
password,
// For steal build process
pluginify;
// Get deferreds
_.mixin(require("underscore.deferred"));
function updateDist() {
console.log("Copying built files to gh-pages.");
var clone = spawn("git", [ "clone", remote ], {
cwd : __dirname
}), dfd = new _.Deferred();
clone.on("exit", function () {
var clonePath = path.join(__dirname, "jquerypp"),
checkout = spawn("git", [ "checkout", "gh-pages"], {
cwd : clonePath
});
checkout.on("exit", function () {
var cloneReleasePath = path.join(clonePath, "release"),
latestPath = path.join(cloneReleasePath, "latest"),
versionPath = path.join(cloneReleasePath, version);
// Make sure directories exist
[ cloneReleasePath, versionPath, latestPath ].forEach(function (dir) {
if (!path.existsSync(dir)) {
fs.mkdirSync(dir);
}
});
fs.readdir(distPath, function (err, files) {
var dfds = files.map(function (file) {
var dfd = new _.Deferred(),
inPath, outPath, latestOutPath,
inStream;
if(file.match(/.*\.js/)) {
inPath = path.join(distPath, file),
outPath = path.join(versionPath, file),
latestOutPath = path.join(latestPath, file),
inStream = fs.createReadStream(inPath);
inStream.pipe(fs.createWriteStream(outPath));
inStream.pipe(fs.createWriteStream(latestOutPath));
inStream.on("end", function () {
dfd.resolve();
});
} else {
dfd.resolve();
}
return dfd.promise();
});
_.when.apply(_, dfds).done(function () {
console.log("Finished copying files. Cleaning up...")
var add = spawn("git", ["add", "release/*"], {
cwd : clonePath
});
add.on("exit", function () {
var commit = spawn("git", ["commit", "-m", "Generated release files for " + version ], {
cwd : clonePath
});
commit.on("exit", function () {
var push = spawn("git", ["push", "origin", "gh-pages"], {
cwd : clonePath
});
push.on("exit", function () {
var remove = spawn("rm", ["-rf", "jquerypp"], {
cwd : __dirname
});
console.log("Done!")
});
});
});
});
});
});
});
return dfd.promise();
}
// Upload files to the Github downloads page
function uploadFiles() {
console.log('Uploading files');
var dfd = new _.Deferred(),
dfds = _.map(descriptions, function (desc, filename) {
var dfd = new _.Deferred();
desc = desc.replace("#{VERSION}", version);
fs.readFile(path.join(distPath, filename), function (err, buf) {
console.log('Uploading ' + filename + ' MIME type ' + mime.lookup(filename));
github.httpSend({
"user" : "jupiterjs",
"repo" : "jquerypp",
"name" : filename,
"size" : buf.length,
"description" : desc,
"content_type" : mime.lookup(filename)
}, {
"url" : "/repos/:user/:repo/downloads",
"method" : "POST",
"params" : {
"$user" : null,
"$repo" : null,
"$name" : null,
"$size" : null,
"description" : null,
"$content_type" : null
}
}, function (err, socket) {
console.log(err);
var data = JSON.parse(socket.data);
s3p.postToS3({
key : data.path,
acl : data.acl,
success_action_status : "201",
Filename : data.name,
AWSAccessKeyId : data.accesskeyid,
policy64 : data.policy,
signature64 : data.signature,
contentType : data.mime_type,
data : buf,
bucket : "github"
}, function (e) {
if (e) {
console.log(e);
}
dfd.resolve();
})
});
});
return dfd.promise();
});
_.when.apply(_, dfds).done(dfd.resolve.bind(dfd));
return dfd.promise();
}
function stealBuild() {
var dfd = new _.Deferred(),
zipFile;
// Run Steal build script
pluginify = spawn(rhinoPath + "/js", ["jquery/build/make.js"], {
cwd : rhinoPath
});
pluginify.on("exit", function (code) {
// Create a zip file
// tar -cvzf jquery/dist/jquerypp.tar.gz jquery/dist
zipFile = 'jquerypp-' + version + '.zip';
descriptions[zipFile] = "jQuery++ #{VERSION} - Full download";
spawn("zip", ["-r", zipFile, '.'], {
cwd : distPath
}).on("exit", function () {
console.log("Done creating ZIP file " + zipFile);
dfd.resolve(code);
});
});
return dfd.promise();
}
// Clean up on process exit
process.on("exit", function () {
process.stdout.write("\n")
})
// Get Github credentials
function getCredentials() {
var dfd = new _.Deferred();
program.prompt("Github Username: ", function (name) {
username = name;
program.password("Github Password: ", "*", function (pass) {
var timeout;
password = pass;
process.stdin.pause();
github.authenticate({
type : "basic",
username : username,
password : password
});
process.stdout.write("Building jQuery++...")
stealTimeout = setInterval(function () {
process.stdout.write(".")
}, 1000)
dfd.resolve();
})
});
return dfd.promise();
}
_.when(stealBuild(), getCredentials()).done(function (code) {
console.log('Built and got credentials');
if (stealTimeout) {
clearTimeout(stealTimeout);
process.stdout.write(" Done!\n")
}
console.log(code);
if (code != 0) {
console.log("Steal build process failed.")
} else {
_.when(uploadFiles(), updateDist()).done(function () {
});
}
});