-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathipa.js
219 lines (198 loc) · 6.21 KB
/
ipa.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
var path = require('path');
var fs = require('fs');
var pngdefry = require('pngdefry');
var AdmZip = require("adm-zip");
var uuidV4 = require('uuid/v4');
var util = require('util')
var extract = require('ipa-extract-info');
var Manifest = require('./manifest.js');
var pretty = require('prettysize');
var tmp_dir = require('os').homedir() + "/.playappstore/tmp_file"
var DB = require('./realmDB.js');
var db = new DB();
var FileHelper = require('./file-helper.js');
var fl = new FileHelper();
module.exports = {
publish : function (file, info) {
return publishIpa(file, info);
},
getRecords: function () {
return allRecords();
},
getAllInfos: function(page, count) {
return allInfos(page, count);
},
getAllVersions: function (bundleId, page, count) {
return allVersions(bundleId, page, count);
},
getAppDetail: function(bundleId, objectId) {
return appDetail(bundleId, objectId);
},
renderManifist: function(guid, basePath) {
var input = path.join(fl.manifestDir, util.format('%s.plist', guid));
return Manifest.render(input, basePath);
},
registerDevice: function (device) {
return db.updateDevice(device);
},
updateDevice: function(device) {
return db.updateDevice(device);
},
updateFollowedRecords: function(deviceID, bundleId, action) {
var device = {uuid: deviceID, platform: 'ios'};
return db.updateDevice(device, bundleId, action);
},
getAllFolloweds: function(deviceID) {
return allFolloweds(deviceID);
}
};
function allFolloweds(deviceID) {
return new Promise(function(resolve, reject) {
var records = db.getFolloweds(deviceID, 'ios');
resolve(records);
});
}
function allRecords() {
// usaully, a company may not have more than 20 apps.
var page = 1;
var count = 100;
return new Promise(function(resolve, reject) {
var records = db.getRecords('ios');
resolve(records);
});
}
function allInfos(page, count) {
var page = 1;
var count = 20;
return new Promise(function(resolve, reject) {
var infos = db.getAppInfos('ios', page, count);
resolve(infos);
})
}
function allVersions(bundleId, page, count) {
return new Promise(function(resolve, reject) {
var records = db.getAppVersions('ios', bundleId, page, count);
resolve(records);
});
}
function appDetail(bundleId, objectId) {
return new Promise(function(resolve, reject) {
var records = db.getAppDetail('ios', bundleId, objectId);
resolve(records);
});
}
// map icon, package, manifest property.
function mapIpas(apps) {
var mapedIpas = apps.map(function(ipa) {
ipa.set('icon', ipa.get('icon').url());
ipa.set('package', ipa.get('package').url());
var manifest = util.format('itms-services://?action=download-manifest&url=%s', ipa.get('manifest').url());
ipa.set('manifest', manifest);
return ipa;
})
return mapedIpas;
}
function publishIpa(file, extraInfo) {
// 1. save icon 2. parse info.plist 3. save ipa
var filepath = file.path;
var size = pretty(file.size);
var info = {};
return Promise.all([extractIpaIcon(filepath), parseIpa(filepath)])
.then(values => {
var tmpIconPath = values[0];
info = values[1];
var iconPath = db.findAppIcon(info);
if (iconPath == '') {
iconPath = fl.iconDir + util.format('%s.png', uuidV4());
}
return fl.rename(tmpIconPath, iconPath);
}, reason => {
console.log(reason);
return Promise.reject(reason);
})
.then(function(iconPath) {
info['icon'] = path.basename(iconPath);
info['primaryKey'] = util.format('[B:%s][P:%s][V:%s]', info['bundleId'], info['platform'], info['version']);
return db.updateAppIcon(info);
})
.then(function(appIcon) {
info['primaryKey'] = util.format('[B:%s][P:%s]', info['bundleId'], info['platform']);
return db.updateAppRecord(info);
})
.then(function(appRecord) {
var appPath = path.join(fl.appDir, util.format('%s.ipa', uuidV4()));
return fl.rename(filepath, appPath);
})
.then(function(ipaPath) {
info['package'] = path.basename(ipaPath);
var manifestPath = path.join(fl.manifestDir, util.format('%s.plist', uuidV4()));
return Manifest.generate(info, manifestPath);
})
.then(function(manifestPath) {
info['objectId'] = uuidV4();
info['manifest'] = path.basename(manifestPath);
info['size'] = size;
var appInfo = Object.assign(info, extraInfo);
return db.updateAppInfo(appInfo);
})
//.then(push(info))
}
function push(info) {
console.log('begin push');
new Promise(function(resolve, reject) {
var tokens = db.getDeviceTokens(info);
console.log(tokens);
});
console.log('check if async');
return info;
}
function parseIpa(filename) {
return new Promise(function(resolve,reject){
var fd = fs.openSync(filename, 'r');
extract(fd, function(err, info, raw){
if (err) reject(err);
var data = info[0];
var info = {}
info["platform"] = "ios"
info["build"] = data.CFBundleVersion,
info["bundleId"] = data.CFBundleIdentifier,
info["version"] = data.CFBundleShortVersionString,
info["name"] = data.CFBundleDisplayName
resolve(info)
});
});
}
function extractIpaIcon(filename) {
return new Promise(function(resolve,reject){
var zip = new AdmZip(filename);
var ipaEntries = zip.getEntries();
var found = false;
ipaEntries.forEach(function(ipaEntry) {
if (ipaEntry.entryName.indexOf('AppIcon60x60@2x.png') != -1) {
found = true;
var buffer = new Buffer(ipaEntry.getData());
if (buffer.length) {
var guid = uuidV4();
var input = util.format('%s/%s.png', tmp_dir, guid)
var output = util.format('%s/%s.new.png', tmp_dir, guid)
fs.writeFile(input, buffer,function(err){
if(err){
reject(err)
}
pngdefry(input, output, function(err) {
if (err) {
// may not the correct format, so we just return the input file instand.
resolve(input);
//reject(err)
}
resolve(output);
});
});
}
}
})
if (!found) {
reject("can not find icon ")
}
})
}