Skip to content

Commit

Permalink
Merge 0434d12 into 63f10cf
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Mujica committed Aug 3, 2017
2 parents 63f10cf + 0434d12 commit 13d2167
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
22 changes: 19 additions & 3 deletions lib/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ module.exports = function(root, options) {
var pkgPath = path.join(root, "package.json");
var optionsMain = options.main;

function updatePackage(pkgSource, pkgPath) {
return asap(fse.writeFile)(pkgPath, pkgSource, "utf8");
}

return Promise.resolve().then(function(){
if(!optionsMain) {
throw new Error("electronOptions.main is required.");
Expand All @@ -15,6 +19,7 @@ module.exports = function(root, options) {
})
.then(function(pkgSource){
var pkg = JSON.parse(pkgSource);
var restoreMainPackage = function() {};
var setPkgMainPromise = Promise.resolve();
var copyElectronMainPromise = Promise.resolve();

Expand All @@ -32,11 +37,22 @@ module.exports = function(root, options) {
var shouldUpdatePackageJSON = pkg.main !== optionsMain;

if(shouldUpdatePackageJSON) {
// callback to restore the original package.json source
restoreMainPackage = function() {
return updatePackage(pkgSource, pkgPath);
};

// update the package.json with the electron main
pkg.main = optionsMain;
pkgSource = JSON.stringify(pkg, null, " ");
setPkgMainPromise = asap(fse.writeFile)(pkgPath, pkgSource, "utf8");
setPkgMainPromise = updatePackage(
JSON.stringify(pkg, null, " "),
pkgPath
);
}

return Promise.all([setPkgMainPromise, copyElectronMainPromise]);
return Promise.all([setPkgMainPromise, copyElectronMainPromise])
.then(function() {
return restoreMainPackage;
});
});
};
21 changes: 16 additions & 5 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,35 @@ exports = module.exports = function(options, buildResult) {
});
var rewritePromise = rewriter.rewrite();

var restoreMainPackage;
var restoreHTML = function(){
return rewriter.restore();
};

process.on("exit", restoreHTML);

return configure(root(), options)
.then(function(){
.then(function(restorePkg) {
restoreMainPackage = restorePkg;
return rewritePromise;
})
.then(function(){
var ElectronPackager = exports.getElectronPackager();
return build(options, buildResult, ElectronPackager);
})
.then(restoreHTML, function(err){
restoreHTML();
return Promise.reject(err);
});
.then(
function() {
return Promise.all([
restoreHTML(),
restoreMainPackage()
]);
},
function(err) {
restoreHTML();
restoreMainPackage();
return Promise.reject(err);
}
);
};

// This exists so we can stub it in tests
Expand Down
15 changes: 9 additions & 6 deletions test/helpers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
var copy = require("copy");
var fse = require("fs-extra");
var exists = fse.existsSync;
var readFile = fse.readFileSync;
var writeFile = fse.writeFileSync;
var path = require("path");
var stealElectron = require("../lib/main");

Expand Down Expand Up @@ -90,11 +88,16 @@ exports.setup = function(projectPath, setup, dontFail){
});
});

after(function(done){
writeFile(path.join(projectPath, "package.json"), this.oldPackageJSON);
after(function(done) {
var oldPackageJson = this.oldPackageJSON.toString();
var packageJson = readFile(path.join(projectPath, "package.json")).toString();

var fin = function() { done(); };
exports.rmdir(path.join(projectPath, "build"))
.then(fin, done);
.then(function() {
if (packageJson !== oldPackageJson) {
throw new Error("Application package.json should not be mutated");
}
})
.then(done, done);
});
};

0 comments on commit 13d2167

Please sign in to comment.