forked from nwutils/nw-builder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
454 lines (378 loc) · 14.7 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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
var Promise = require('bluebird');
var _ = require('lodash');
var inherits = require('inherits');
var EventEmitter = require('events').EventEmitter;
var fs = require('fs-extra');
var path = require('path');
var url = require('url');
var winresourcer = Promise.promisify(require('winresourcer'));
var spawn = require('child_process').spawn;
var semver = require('semver');
var detectCurrentPlatform = require('./detectCurrentPlatform.js')
var NwVersions = require('./versions');
var Utils = require('./utils');
var Downloader = require('./downloader');
var platforms = require('./platforms');
// We inherit from EventEmitter for logging
inherits(NwBuilder, EventEmitter);
module.exports = NwBuilder;
function NwBuilder(options) {
var self = this;
var defaults = {
files: null,
appName: false,
appVersion: false,
platforms: ['win','osx'],
currentPlatform: detectCurrentPlatform(),
version: 'latest',
buildDir: './build',
cacheDir: './cache',
downloadUrl: 'http://dl.node-webkit.org/',
buildType: 'default',
forceDownload: false,
macCredits: false,
macIcns: false,
macZip: false,
macPlist: false,
winIco: null,
argv: process.argv.slice(2)
};
// Assing options
this.options = _.defaults(options, defaults);
this._platforms = platforms;
// Some Option checking
if(!this.options.files) {
throw new Error('Please specify some files');
}
if (this.options.platforms.length === 0)
throw new Error('No platform to build!');
// verify all the platforms specifed by the user are supported
// this + previous check assures as we have only buildable platforms specified
this.options.platforms.forEach(function(platform) {
if (!(platform in platforms))
throw new Error('unknown platform ' + platform)
})
this._platforms = _.cloneDeep(platforms)
// clear all unused platforms
for (var name in this._platforms) {
if (this.options.platforms.indexOf(name) === -1)
delete this._platforms[name]
}
};
NwBuilder.prototype.build = function (callback) {
var hasCallback = (typeof callback === 'function'),
done = Promise.defer();
// Let's create a node Webkit app
this.checkFiles().bind(this)
.then(this.resolveLatestVersion)
.then(this.checkVersion)
.then(this.platformFilesForVersion)
.then(this.downloadNodeWebkit)
.then(this.createReleaseFolder)
.then(this.copyNodeWebkit)
.then(this.handleMacApp)
.then(this.handleWinApp)
.then(this.zipAppFiles)
.then(this.mergeAppFiles)
.then(function (info) {
if(hasCallback) {
callback(false, info);
} else {
done.resolve(info);
}
})
.catch(function (error) {
if(hasCallback) {
callback(error);
} else {
done.reject(error);
}
});
return hasCallback ? true : done.promise;
};
NwBuilder.prototype.run = function (callback) {
var hasCallback = (typeof callback === 'function'),
done = Promise.defer();
// Let's run this node Webkit app
this.checkFiles().bind(this)
.then(this.resolveLatestVersion)
.then(this.checkVersion)
.then(this.platformFilesForVersion)
.then(this.downloadNodeWebkit)
.then(this.runApp)
.then(function (info) {
if(hasCallback) {
callback(false, info);
} else {
done.resolve(info);
}
})
.catch(function (error) {
if(hasCallback) {
callback(true, error);
} else {
done.reject(error);
}
});
return hasCallback ? true : done.promise;
};
NwBuilder.prototype.checkFiles = function () {
var self = this;
return Utils.getFileList(this.options.files).then(function (data) {
self._appPkg = data.json;
self._files = data.files;
if(!self.options.appName || self.options.appVersion) {
return Utils.getPackageInfo(self._appPkg).then(function (appPkg) {
self._appPkg = appPkg;
self.options.appName = (self.options.appName ? self.options.appName : appPkg.name);
self.options.appVersion = (self.options.appVersion ? self.options.appVersion : appPkg.version);
});
}
});
};
NwBuilder.prototype.resolveLatestVersion = function () {
var self = this;
if(self.options.version !== 'latest') return Promise.resolve();
return NwVersions.getLatestVersion(self.options.downloadUrl).then(function(latestVersion){
self.emit('log', 'Latest Version: v' + latestVersion);
self.options.version = latestVersion;
});
};
NwBuilder.prototype.checkVersion = function () {
var version = this.options.version.replace('v', '');
if(!semver.valid(this.options.version)){
return Promise.reject('The version ' + this.options.version + ' is not valid.');
} else {
this._version = NwVersions.getVersionNames( version );
this.emit('log', 'Using v' + this._version.version);
}
return Promise.resolve();
};
NwBuilder.prototype.platformFilesForVersion = function () {
var self = this;
this._forEachPlatform(function (name, platform) {
var satisfied = !Object.keys(platform.files).every(function(range) {
if (semver.satisfies(self._version.version, range)) {
platform.files = platform.files[range];
return false;
}
return true;
});
if (!satisfied) {
throw new Error("Unsupported node-webkit version '" + self._version.version + "' for platform '" + platform.type + "'");
}
});
return true;
};
NwBuilder.prototype.downloadNodeWebkit = function () {
var self = this,
downloads = [];
this._forEachPlatform(function (name, platform) {
platform.cache = path.resolve(self.options.cacheDir, self._version.version, name);
platform.url = url.resolve(self.options.downloadUrl, self._version.platforms[name]);
// Ensure that there is a cache folder
if(self.options.forceDownload) {
fs.removeSync(platform.cache);
}
fs.mkdirpSync(platform.cache);
self.emit('log', 'Create cache folder in ' + path.resolve(self.options.cacheDir, self._version.version));
if(!Downloader.checkCache(platform.cache, platform.files)) {
downloads.push(
Downloader.downloadAndUnpack(platform.cache, platform.url)
.catch(function(err){
if(err.statusCode === 404){
self.emit('log', 'ERROR: The version '+self._version.version+' does not have a corresponding build posted at http://dl.node-webkit.org/. Please choose a version from that list.')
} else {
self.emit('log', err.msg)
}
return Promise.reject('Unable to download nodewebkit.');
})
);
self.emit('log', 'Downloading: ' + platform.url);
} else {
self.emit('log', 'Using cache for: ' + name);
}
});
return Promise.all(downloads)
.then(function(data) {
Downloader.clearProgressbar();
return data;
});
};
NwBuilder.prototype.buildGypModules = function () {
// @todo
// If we trigger a rebuild we have to copy
// the node_modules to a tmp location because
// we don't want to change the source files
};
NwBuilder.prototype.createReleaseFolder = function () {
var self = this,
releasePath;
if (_.isFunction(self.options.buildType)) {
releasePath = self.options.buildType.call(self.options);
} else {
// buildTypes
switch(self.options.buildType) {
case 'timestamped':
releasePath = self.options.appName + ' - ' + Math.round(Date.now() / 1000).toString();
break;
case 'versioned':
releasePath = self.options.appName + ' - v' + self.options.appVersion;
break;
default:
releasePath = self.options.appName;
}
}
this._forEachPlatform(function (name, platform) {
platform.releasePath = path.resolve(self.options.buildDir, releasePath, name);
// Ensure that there is a release Folder, delete and create it.
fs.removeSync(platform.releasePath);
fs.mkdirpSync(platform.releasePath);
self.emit('log', 'Create release folder in ' + platform.releasePath);
});
return true;
};
NwBuilder.prototype.copyNodeWebkit = function () {
var self = this,
copiedFiles = [];
this._forEachPlatform(function (name, platform) {
var executable = platform.runable.split('/')[0];
platform.files.forEach(function (file, i) {
var destFile = file;
if(i===0) {
// rename executable to app name
destFile = self.options.appName + path.extname(file);
// save new filename back to files list
platform.files[0] = destFile;
}
copiedFiles.push(Utils.copyFile(path.resolve(platform.cache, file), path.resolve(platform.releasePath, destFile)));
});
});
return Promise.all(copiedFiles);
};
NwBuilder.prototype.zipAppFiles = function () {
var self = this;
// Check if zip is needed
var _needsZip = self.options.macZip
// this can be improved
this._forEachPlatform(function(name, platform) {
if (platform.needsZip)
_needsZip = true
})
this._needsZip = _needsZip;
return new Promise(function(resolve, reject) {
if(self._needsZip) {
Utils.generateZipFile(self._files, self).then(function (nwFile) {
self._nwFile = nwFile;
resolve();
}, reject);
} else {
self._nwFile = null;
resolve();
}
});
};
NwBuilder.prototype.mergeAppFiles = function () {
var self = this,
copiedFiles = [];
this._forEachPlatform(function (name, platform) {
// We copy the app files if we are on mac and don't force zip
if(name === 'osx') {
// no zip, copy the files
if(!self.options.macZip) {
self._files.forEach(function (file) {
var dest = path.resolve(platform.releasePath, self.options.appName+'.app', 'Contents', 'Resources', 'app.nw', file.dest);
copiedFiles.push(Utils.copyFile(file.src, dest));
});
} else {
// zip just copy the app.nw
copiedFiles.push(Utils.copyFile(self._nwFile, path.resolve(platform.releasePath, self.options.appName+'.app', 'Contents', 'Resources', 'nw.icns')));
}
} else {
// We cat the app.nw file into the .exe / nw
copiedFiles.push(Utils.mergeFiles(path.resolve(platform.releasePath, _.first(platform.files)), self._nwFile), platform.chmod);
}
});
return Promise.all(copiedFiles);
};
NwBuilder.prototype.handleMacApp = function () {
var self = this, allDone = [];
var macPlatform = this._platforms.osx;
if(!macPlatform) return Promise.resolve();
// Let's first handle the mac icon
if(self.options.macIcns) {
allDone.push(Utils.copyFile(self.options.macIcns, path.resolve(macPlatform.releasePath, self.options.appName+'.app', 'Contents', 'Resources', 'nw.icns')));
}
// Handle mac credits
if(self.options.macCredits) {
allDone.push(Utils.copyFile(self.options.macCredits, path.resolve(macPlatform.releasePath, self.options.appName+'.app', 'Contents', 'Resources', 'Credits.html')));
}
// Let handle the Plist
var PlistPath = path.resolve(macPlatform.releasePath, self.options.appName+'.app', 'Contents', 'Info.plist');
// If the macPlist is a string we just copy the file
if(typeof self.options.macPlist === 'string') {
allDone.push(Utils.copyFile(self.options.macPlist, PlistPath));
} else {
// Setup the Plst
var defaultPlist = {
appName: self.options.appName,
appVersion: self.options.appVersion,
copyright: self._appPkg.copyright || false
};
var plistOptions = (self.options.macPlist ? _.defaults(self.options.macPlist, defaultPlist) : defaultPlist);
allDone.push(Utils.editPlist(PlistPath, PlistPath, plistOptions));
}
return Promise.all(allDone);
};
NwBuilder.prototype.handleWinApp = function () {
var self = this,
done = Promise.defer();
var winPlatform = this._platforms.win;
if(!winPlatform || !self.options.winIco) return Promise.resolve();
// Set icon
if (self.options.winIco) {
self.emit('log', 'Update executable icon');
winresourcer({
operation: "Update",
exeFile: path.resolve(winPlatform.releasePath, _.first(winPlatform.files)),
resourceType: "Icongroup",
resourceName: "IDR_MAINFRAME",
lang: 1033, // Required, except when updating or deleting
resourceFile: path.resolve(self.options.winIco)
}, function(err) {
if(err) {
done.reject('Error while updating the Windows icon.' +
(process.platform !== "win32" ? ' Wine (winehq.org) must be installed to add custom icons from Mac and Linux.' : '')
);
}
else {
done.resolve();
}
});
}
return done.promise;
};
NwBuilder.prototype.runApp = function () {
var self = this,
platform = this._platforms[this.options.currentPlatform],
executable = path.resolve(platform.cache, platform.runable);
self.emit('log', 'Launching App');
return new Promise(function(resolve, reject) {
var p = spawn(executable, ['--enable-logging', self.options.files.replace(/\*[\/\*]*/,"")].concat(self.options.argv));
p.stdout.on('data', function(data) {
self.emit('stdout', data);
});
p.stderr.on('data', function(data) {
self.emit('stderr', data);
});
p.on('close', function(code) {
self.emit('log', 'App exited with code ' + code);
resolve();
});
});
};
NwBuilder.prototype._forEachPlatform = function (fn) {
_.forEach(this._platforms, function(platform, name) {
return fn(name, platform)
});
};