Skip to content

Commit

Permalink
Merge pull request #144 from mllrsohn/fixing-1.0.x
Browse files Browse the repository at this point in the history
Some fixes related to #142
  • Loading branch information
adam-lynch committed Dec 21, 2014
2 parents 591864e + 7a75a1f commit a16ab31
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 51 deletions.
104 changes: 61 additions & 43 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ function NwBuilder(options) {
// Intercept the platforms and check for the legacy platforms of 'osx' and 'win' and
// replace with 'osx32', 'osx64', and 'win32', 'win64' respectively.
if(typeof options.platforms != 'undefined'){
if(options.platforms.indexOf('osx') >= 0){
options.platforms.splice(options.platforms.indexOf('osx'), 1, 'osx32', 'osx64');
}
if(options.platforms.indexOf('win') >= 0){
options.platforms.splice(options.platforms.indexOf('win'), 1, 'win32', 'win64');
}
if(options.platforms.indexOf('linux') >= 0){
options.platforms.splice(options.platforms.indexOf('linux'), 1, 'linux32', 'linux64');
}
if(options.platforms.indexOf('osx') >= 0){
options.platforms.splice(options.platforms.indexOf('osx'), 1, 'osx32', 'osx64');
}
if(options.platforms.indexOf('win') >= 0){
options.platforms.splice(options.platforms.indexOf('win'), 1, 'win32', 'win64');
}
if(options.platforms.indexOf('linux') >= 0){
options.platforms.splice(options.platforms.indexOf('linux'), 1, 'linux32', 'linux64');
}
}
// Assing options
this.options = _.defaults(options, defaults);
Expand Down Expand Up @@ -283,17 +283,18 @@ NwBuilder.prototype.preparePlatformSpecificManifests = function(){
objectMode: true,
platform: name
}, function(err, result){
if(err) throw(err);
platform.platformSpecificManifest = result;
});
if(err) throw(err);
platform.platformSpecificManifest = result;
});
}
});
};


NwBuilder.prototype.createReleaseFolder = function () {
var self = this,
releasePath;
releasePath,
directoryCreationPromises = [];

if (_.isFunction(self.options.buildType)) {
releasePath = self.options.buildType.call(self.options);
Expand All @@ -314,15 +315,25 @@ NwBuilder.prototype.createReleaseFolder = function () {
}

this._forEachPlatform(function (name, platform) {
platform.releasePath = path.resolve(self.options.buildDir, releasePath, name);
directoryCreationPromises.push(new Promise(function(resolve, reject){
platform.releasePath = path.resolve(self.options.buildDir, releasePath, name);

// Ensure that there is a release Folder, delete and create it.
fs.remove(platform.releasePath, function(err){
if(err) return reject(err);

// 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);
fs.mkdirp(platform.releasePath, function(err){
if(err) return reject(err);

self.emit('log', 'Create release folder in ' + platform.releasePath);
resolve();
});

});
}));
});

return true;
return Promise.all(directoryCreationPromises);
};

NwBuilder.prototype.copyNodeWebkit = function () {
Expand All @@ -339,7 +350,7 @@ NwBuilder.prototype.copyNodeWebkit = function () {
// 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)));
copiedFiles.push(Utils.copyFile(path.resolve(platform.cache, file), path.resolve(platform.releasePath, destFile), self));
});
});

Expand Down Expand Up @@ -387,27 +398,27 @@ NwBuilder.prototype.zipAppFiles = function () {
resolve();
}
})
.then(function(platformAgnosticZip){
var zipPromises = [];
.then(function(platformAgnosticZip){
var zipPromises = [];

_.forEach(self._zips, function(zip, platformName){
_.forEach(self._zips, function(zip, platformName){

if(platformAgnosticZip && !zip.platformSpecific){
zip.file = platformAgnosticZip;
return;
}
if(platformAgnosticZip && !zip.platformSpecific){
zip.file = platformAgnosticZip;
return;
}

zipPromises.push(Utils.generateZipFile(
self._files,
self,
JSON.stringify(self._platforms[platformName].platformSpecificManifest)
).then(function(file){
zip.file = file;
}));
});
zipPromises.push(Utils.generateZipFile(
self._files,
self,
JSON.stringify(self._platforms[platformName].platformSpecificManifest)
).then(function(file){
zip.file = file;
}));
});

Promise.all(zipPromises).then(resolve, reject);
}, reject);
Promise.all(zipPromises).then(resolve, reject);
}, reject);
});
};

Expand All @@ -418,23 +429,25 @@ NwBuilder.prototype.mergeAppFiles = function () {
this._forEachPlatform(function (name, platform) {
// We copy the app files if we are on mac and don't force zip
if(name === 'osx32' || name === 'osx64') {

// 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);
var dest = path.resolve(self.getResourcesDirectoryPath(platform), 'app.nw', file.dest);

if(file.dest === 'package.json' && platform.platformSpecificManifest){
copiedFiles.push(self.writePlatformSpecificManifest(platform, dest));
}
else {
copiedFiles.push(Utils.copyFile(file.src, dest));
copiedFiles.push(Utils.copyFile(file.src, dest, self));
}
});
} else {
// zip just copy the app.nw
copiedFiles.push(Utils.copyFile(
self.getZipFile(name),
path.resolve(platform.releasePath, self.options.appName+'.app', 'Contents', 'Resources', 'app.nw')
path.resolve(self.getResourcesDirectoryPath(platform), 'app.nw'),
self
));
}
} else {
Expand Down Expand Up @@ -475,20 +488,20 @@ NwBuilder.prototype.handleMacApp = function () {

// Let's first handle the mac icon
if(self.options.macIcns) {
allDone.push(Utils.copyFile(self.options.macIcns, path.resolve(platform.releasePath, self.options.appName+'.app', 'Contents', 'Resources', 'nw.icns')));
allDone.push(Utils.copyFile(self.options.macIcns, path.resolve(self.getResourcesDirectoryPath(platform), 'nw.icns'), self));
}

// Handle mac credits
if(self.options.macCredits) {
allDone.push(Utils.copyFile(self.options.macCredits, path.resolve(platform.releasePath, self.options.appName+'.app', 'Contents', 'Resources', 'Credits.html')));
allDone.push(Utils.copyFile(self.options.macCredits, path.resolve(self.getResourcesDirectoryPath(platform), 'Credits.html'), self));
}

// Let's handle the Plist
var PlistPath = path.resolve(platform.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));
allDone.push(Utils.copyFile(self.options.macPlist, PlistPath, self));
} else {
// Setup the Plist
var plistOptions = Utils.getPlistOptions(
Expand Down Expand Up @@ -571,3 +584,8 @@ NwBuilder.prototype._forEachPlatform = function (fn) {
return fn(name, platform)
});
};

// Mac only
NwBuilder.prototype.getResourcesDirectoryPath = function (platform) {
return path.resolve(platform.releasePath, this.options.appName+'.app', 'Contents', 'Resources');
};
42 changes: 34 additions & 8 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,40 @@ module.exports = {
pathDepth: function(absolutePath) {
return absolutePath.split(path.sep).length;
},
copyFile: function (src, dest) {
return new Promise(function(resolve, reject) {
copyFile: function (src, dest, _event) {
var copy = function(resolve, reject, retryCount){
var onFailure = function(err){
if(retryCount > 2){
return reject(err);
}
else {
setTimeout(function(){
copy(resolve, reject, retryCount + 1);
}, 200);
}
};

var stats = fs.lstatSync(src);
fs.copy(src, dest, function (err) {
if(err) return reject(err);
fs.chmodSync(dest, stats.mode);
resolve();

fs.exists(dest, function(exists){
if(exists){
fs.chmod(dest, stats.mode, function(err){
// ignore error
_event.emit('log', 'chmod ' + stats.mode + ' on ' + dest + ' failed after copying, ignoring');
resolve();
});
}
else {
onFailure(new Error("Copied file (" + dest + ") doesn't exist in destination after copying"));
}
});
});
};

return new Promise(function(resolve, reject) {
copy(resolve, reject, 0);
});
},
mergeFiles: function (app, zipfile, chmod) {
Expand Down Expand Up @@ -180,10 +206,10 @@ module.exports = {
'CFBundleShortVersionString',
'NSHumanReadableCopyright'
].forEach(function(prop) {
if(!options.hasOwnProperty(prop)) {
throw new Error('Missing macPlist property \'' + prop + '\'');
}
});
if(!options.hasOwnProperty(prop)) {
throw new Error('Missing macPlist property \'' + prop + '\'');
}
});

// Bundle identifier based on package name
if(options.CFBundleIdentifier === undefined) {
Expand Down

0 comments on commit a16ab31

Please sign in to comment.