-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
183 lines (166 loc) · 6.29 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
var request = require("request");
var ProgressBar = require("progress");
var _ = require("underscore");
var fs = require("fs");
var path = require("path");
var platform = "osx";
var os = require('os');
var tmp = require('tmp');
var rimraf = require("rimraf");
var chalk = require("chalk");
var appc = require("node-appc");
var semver = require("semver");
var _spawn = require('child_process').spawn;
function spawn(cmd,args,props) {
if (process.platform === 'win32') {
args = ['/c',cmd].concat(args);
cmd = process.env.comspec;
}
return _spawn(cmd,args,props);
}
function spawnPromise(cmd, args, props) {
return new Promise((resolve, reject) => {
var s = spawn(cmd, args, props);
s.on('error',function(err) { reject(err); });
s.on('exit', function(){ resolve(); });
})
}
var TITANIUM, platform;
if (os.platform()=="linux"){
TITANIUM = path.join(process.env.HOME, '.titanium');
platform = "linux";
} else if (os.platform() === "win32") {
TITANIUM = path.join(process.cwd().split(path.sep)[0], "ProgramData", "Titanium");
platform = "win32";
} else {
TITANIUM = path.join(process.env.HOME, 'Library', 'Application Support', 'Titanium');
platform = "osx";
}
exports.platform = platform;
exports.getGATags = function getGATags(callback) {
request({
url: "https://api.github.com/repos/appcelerator/titanium_mobile/tags?per_page=50",
headers: {
'User-Agent': 'Awesome-Octocat-App'
}
}, function(err, res) {
var tags = JSON.parse(res.body).filter(function(a) { return a.name.match(/^\d+_\d+_\d+_.*/); });
callback(tags);
});
};
exports.getNightlies = function getNightlies(branch, callback) {
request({
url: "http://builds.appcelerator.com/mobile/" + branch + "/index.json",
headers: {
// 'User-Agent': 'Awesome-Octocat-App'
}
}, function(err, res) {
callback(JSON.parse(res.body));
});
};
exports.download = function(_version, destination, callback) {
var version = _version.replace(/\./g, "_");
exports.getGATags(function(tags) {
var tag = _.find(tags, function(tag) {
return tag.name === version;
});
if (!tag) {
console.error("Invalid GA Version: use the `tisdk list` command");
return;
}
var nightly = tag.name.split("_").splice(0,2).join("_") + "_" + "X";
exports.getNightlies(nightly, function(list) {
var build = _.find(list, function(t) { return t.git_revision === tag.commit.sha;});
if (!build) {
console.error("Build not available. Try the `tisdk build` command to build the sdk from source.");
return;
}
var file = fs.createWriteStream(destination);
var filename = build.filename;
if (platform=="linux"){
filename = filename.replace(/osx/g,"linux");
} else if (platform === "win32") {
filename = filename.replace(/osx/g,"win32");
}
var req = request.get("http://builds.appcelerator.com/mobile/" + nightly + "/" + filename);
req.pipe(file);
req.on('response', function(req) {
var bar = new ProgressBar('Downloading... [:bar] :percent :etas', {
complete: '=',
incomplete: ' ',
width: 40,
total: parseInt(req.headers['content-length'])
});
req.on('data', function(buffer) {
bar.tick(buffer.length);
});
});
file.on('finish', function() {
callback(build);
});
});
});
};
exports.install = function(_version, callback) {
var version = _version.replace("_", ".");
spawnPromise('ti', ['sdk', 'install', `http://builds.appcelerator.com/mobile-releases/${version.replace(".GA","")}/mobilesdk-${version}-${platform}.zip`], {stdio: "inherit"})
.then(callback);
}
exports.manualBuild = function(_version, destination, overwrite, callback) {
var version = _version.replace(/\./g, "_");
exports.getGATags(function(tags) {
var tag = _.find(tags, function(tag) {
return tag.name === version;
});
if (!tag) {
console.error("Invalid GA Version: use the `tisdk list` command");
return;
}
var tmp_dir = tmp.dirSync({prefix:'tisdk_'}).name;
var args = "clone --depth 1 --branch " + version + " https://github.com/appcelerator/titanium_mobile " + tmp_dir;
var git = spawn('git', args.split(" "), {stdio: "inherit"});
git.on('error',function(err) { console.log(err); });
git.on('exit', function(){
if (semver.gte(version.split("_").slice(0,3).join("."), "6.0.0")) {
spawnPromise('npm', ['install'], {stdio: "inherit", cwd: tmp_dir})
.then(() => spawnPromise('npm', ['install', 'fs-extra'], {stdio: "inherit", cwd: tmp_dir}))
.then(() => spawnPromise('node', [path.join('build','scons.js'), 'build'], {stdio: "inherit", cwd: tmp_dir}))
.then(() => spawnPromise('node', [path.join('build','scons.js'), 'package'], {stdio: "inherit", cwd: tmp_dir}))
.then(() => spawnPromise('node', [path.join('build','scons.js'), 'install'], {stdio: "inherit", cwd: tmp_dir}))
.then(callback);
} else {
var scons = spawn('scons', ['-C', tmp_dir], {stdio: "inherit", env: process.env});
scons.on('error',function(err) { console.log(err); });
scons.on('exit', function() {
fs.renameSync(path.join(tmp_dir, 'dist', "mobilesdk-" + _version.split(".").splice(0,3).join(".") + "-" + platform + ".zip"), destination);
exports.unzip(version, tmp_file, version.split(".").splice(0,3).join("."), overwrite, callback());
});
}
});
});
};
exports.installTarget = function(version) {
return path.join(TITANIUM, "mobilesdk", platform, version);
};
exports.unzip = function(version, src, bundled_version_name, overwrite, callback) {
var target = exports.installTarget(version);
console.log("Unzipping...");
appc.zip.unzip(src, TITANIUM, {
visitor: function(entry, i, len) {
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write("Unzipping (" + i + "/" + len + ") : " + chalk.grey(entry.entryName) );
}
}, function() {
var source = path.join(TITANIUM, "mobilesdk", platform, bundled_version_name);
console.log("");
if (overwrite) {
console.log("Removing old " + target);
rimraf.sync(target);
}
console.log("Renaming " + source + " to " + target);
fs.renameSync(source, target);
fs.unlinkSync(src);
callback();
});
};