-
Notifications
You must be signed in to change notification settings - Fork 19
/
Jakefile
141 lines (129 loc) · 5.03 KB
/
Jakefile
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
desc("This is the default task.");
task("default", function (params) {
var fs = require("node-fs");
var dot = require("dot");
var UglifyJS = require("uglify-js");
package(function(pkg) {
console.log("Building " + pkg.title + " " + pkg.version);
var files = fs.readdirSync("./src");
fs.readFile("./src/" + files[0], "utf8", function(err, data) {
if (err) throw err;
var cmp = dot.template(data, {evaluate: /\{\{([\s\S]+?)\}\}/g,
interpolate: /\{\{=([\s\S]+?)\}\}/g,
encode: /\{\{!([\s\S]+?)\}\}/g,
use: /\{\{#([\s\S]+?)\}\}/g, //compile time evaluation
define: /\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g, //compile time defs
strip: false,
varname: "it"})(pkg);
var ast = UglifyJS.parse(cmp);
ast.figure_out_scope();
ast = ast.transform(UglifyJS.Compressor());
// need to figure out scope again so mangler works optimally
ast.figure_out_scope();
ast.compute_char_frequency();
ast.mangle_names();
var min = ast.print_to_string({ comments: true });
var buildDirectory = pkg.build.directory? dot.template(pkg.build.directory)(pkg) : "dist";
try {
fs.rmdirSync("./" + buildDirectory);
} catch (e) {
}
fs.mkdirSync("./" + buildDirectory, 0777, true);
console.log("Writing to " + buildDirectory);
var buildFinalName = pkg.build.final_name ? dot.template(pkg.build.final_name)(pkg) : pkg.name + "-" + pkg.version;
fs.writeFileSync("./" + buildDirectory + "/" + buildFinalName + ".min.js", min);
fs.writeFileSync("./" + buildDirectory + "/" + buildFinalName + ".js", cmp);
complete();
});
});
}, true);
task("publish", ["default"], function (params) {
var gits = require("gits");
package(function(pkg) {
var tagName = pkg.version;
gits.git(".", ["tag", "-a", "-m", "Publishing new version", "v" + tagName], function() {
gits.git(".", ["push", "--tags"], function() {
console.log("Created remote tag " + tagName);
upload(function(fileName, error, result) {
if (error) {
console.log("Uploading " + fileName + " to GitHub failed: " + error);
return;
}
console.log("Successfully uploaded " + fileName + " to GitHub");
});
});
});
});
});
function package(callback) {
var fs = require("node-fs");
fs.readFile("./package.json", "utf8", function(err, data) {
if (err) throw err;
callback(JSON.parse(data));
});
}
function upload(callback) {
var fs = require("node-fs");
var dot = require("dot");
var read = require("read");
package(function(pkg) {
if (pkg.repository.url.match("https?://github.com/.+/.+.git")) {
read({ prompt : "Your GitHub username: " }, function (error, username) {
read({ prompt : "Your GitHub password: ", silent : true }, function (error, password) {
process.stdin.destroy();
var path = pkg.repository.url.substring(0, pkg.repository.url.length - 4);
path = "https://api.github.com/repos/" + path.substring(path.indexOf("github.com") + 11);
var buildUniqueName = pkg.build.unique_name ? dot.template(pkg.build.unique_name)(pkg) : pkg.name + "-" + pkg.version;
var buildDirectory = pkg.build.directory? dot.template(pkg.build.directory)(pkg) : "dist";
var buildFinalName = pkg.build.final_name ? dot.template(pkg.build.final_name)(pkg) : pkg.name + "-" + pkg.version;
post(path, "./" + buildDirectory + "/" + buildFinalName + ".js", buildUniqueName + ".js", {username: username, password: password}, callback);
post(path, "./" + buildDirectory + "/" + buildFinalName + ".min.js", buildUniqueName + ".min.js", {username: username, password: password}, callback);
});
});
}
});
function post(path, file, fileName, options, callback) {
var needle = require("needle");
needle.post(path + "/downloads", JSON.stringify({
name: fileName,
size: fs.statSync(file).size,
content_type: "application/javascript"
}), {
username: options.username,
password: options.password
}, function(error, response, body) {
if (error) {
callback(fileName, error, null);
return;
}
if (response.statusCode !== 201) {
callback(fileName, body.message, null);
return;
}
needle.post(body.s3_url, {
key: body.path,
acl: body.acl,
success_action_status: 201,
Filename: body.name,
AWSAccessKeyId: body.accesskeyid,
Policy: body.policy,
Signature: body.signature,
"Content-Type": body.mime_type,
file: {
file: file,
content_type: "application/javascript"
}
}, {multipart: true}, function(error, response, body) {
if (error) {
callback(fileName, error, null);
return;
}
if (response.statusCode !== 201) {
callback(fileName, body.message, null);
return;
}
callback(fileName, null, body);
});
});
}
}