Skip to content

Commit

Permalink
v1.2.2 - properly fix dependency resolution using promises to defer a…
Browse files Browse the repository at this point in the history
…sync progression of hook scripts. Fixes #23 and fixes #29.
  • Loading branch information
dpa99c committed Feb 8, 2016
1 parent 41d7fe2 commit 4d6ce89
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 118 deletions.
2 changes: 1 addition & 1 deletion hooks/applyCustomConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,5 +580,5 @@ module.exports = function(ctx) {
hooksPath = path.resolve(ctx.opts.projectRoot, "plugins", ctx.opts.plugin.id, "hooks");
logger = require(path.resolve(hooksPath, "logger.js"))(ctx);
logger.debug("Running applyCustomConfig.js");
require(path.resolve(hooksPath, "resolveDependencies.js"))(ctx, applyCustomConfig.init.bind(this, ctx));
require(path.resolve(hooksPath, "resolveDependencies.js"))(ctx).then(applyCustomConfig.init.bind(this, ctx));
};
2 changes: 1 addition & 1 deletion hooks/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var logger = (function(){
context = ctx;
},
debug: function(msg){
if(context.opts.verbose){
if(context.opts.verbose || context.cmdLine.match("--verbose")){
msg = prefixMsg(msg);
if(hasColors){
console.log(msg.green);
Expand Down
121 changes: 22 additions & 99 deletions hooks/resolveDependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,19 @@
* Check all necessary module dependencies are installed.
* @module resolveDependencies
*/
var resolveDependencies = (function () {
(function () {

/**********
* Modules
**********/
var exec = require('child_process').exec,
fs = require('fs'),
path = require('path');
var exec, fs, path, deferral;

/**********************
* Internal properties
*********************/
var resolveDependencies = {},
sourcePackageJson,
var sourcePackageJson,
targetPackageJson,
tempPackageJson,
lockfile,
completefile,
completeCallback,
hooksPath,
logger;

Expand Down Expand Up @@ -63,31 +57,6 @@ var resolveDependencies = (function () {
fs.unlink(target, cb);
}

function createFile(content, target, cb) {
var cbCalled = false;

var wr = fs.createWriteStream(target);
wr.on("error", function (err) {
done(err);
});
wr.on("close", function (ex) {
done();
});
wr.write(content);
wr.end();

function done(err) {
if (!cbCalled) {
cb(err);
cbCalled = true;
}
}
}

function renameFile(source, target, cb) {
fs.rename(source, target, cb);
}

/*********************
* Internal procedures
*********************/
Expand All @@ -104,44 +73,14 @@ var resolveDependencies = (function () {
});
}

// Check for completefile, indicating this script has already successfully installed dependencies.
function checkCompletefile(){
fileExists(completefile, function (exists) {
if (exists) {
logger.debug("completefile exists, so assuming all dependencies are already installed. Aborting.");
complete();
} else {
checkLockfile();
}
});
}

// Check for a lockfile, indicating an instance of this script is already running
function checkLockfile(){
fileExists(lockfile, function (exists) {
if (exists) {
logger.debug("lockfile exists, so assuming that another instance of this script is running. Aborting.");
complete();
} else {
createFile("locked", lockfile, function(err){
if (err) {
logger.error("Error creating lockfile: " + err);
return -1;
}
checkForRealPackageJson();
});
}
});
}

// Check if a real package.json exists in the project root
function checkForRealPackageJson(){
fileExists(targetPackageJson, function (exists) {
if (exists) {
logger.debug("package.json already exists");
copyFile(targetPackageJson, tempPackageJson, function (err) {
if (err) {
logger.error("Error copying package.json to package.json.tmp: " + err);
deferral.reject("Error copying package.json to package.json.tmp: " + err);
return -1;
}
logger.debug("Copied existing package.json to package.json.tmp");
Expand All @@ -156,35 +95,22 @@ var resolveDependencies = (function () {
// Dependency resolution is complete
function complete() {
logger.debug("Dependency resolution complete");
if(completeCallback){
completeCallback();
}
deferral.resolve();
}

// Rename our lockfile to completefile now dependency resolution is complete
function removeLockfile(){
renameFile(lockfile, completefile, function (err) {
if (err) {
logger.error("Error renaming our lockfile to completefile: " + err);
return -1;
}
logger.debug("Renamed our lockfile to completefile");
complete();
})
}

// Deploy our plugin's package.json and execute npm install
function deployPluginPackageJson() {
logger.debug("Copying package.json");
copyFile(sourcePackageJson, targetPackageJson, function (err) {
if (err) {
logger.error("Error copying plugin's package.json: " + err);
deferral.reject("Error copying plugin's package.json: " + err);
return -1;
}
logger.debug("Copied package.json");
installModules(function(err) {
if (err) {
logger.error("Error installing modules: " + err);
deferral.reject("Error installing modules: " + err);
return -1;
}
logger.debug("Installed modules");
Expand All @@ -193,29 +119,29 @@ var resolveDependencies = (function () {
logger.debug("package.json.tmp exists");
copyFile(tempPackageJson, targetPackageJson, function (err) {
if (err) {
logger.error("Error restoring package.json.tmp to package.json: " + err);
deferral.reject("Error restoring package.json.tmp to package.json: " + err);
return -1;
}
logger.debug("Overwrote our package.json with original package.json.tmp");

logger.debug("Removing package.json.tmp");
deleteFile(tempPackageJson, function (err) {
if (err) {
logger.error("Error removing package.json.tmp: " + err);
deferral.reject("Error removing package.json.tmp: " + err);
return -1;
}
logger.debug("Removed package.json.tmp");
removeLockfile();
complete();
})
});
} else {
deleteFile(targetPackageJson, function (err) {
if (err) {
logger.error("Error removing our package.json: " + err);
deferral.reject("Error removing our package.json: " + err);
return -1;
}
logger.debug("Removed our package.json");
removeLockfile();
complete();
})
}
});
Expand All @@ -224,25 +150,22 @@ var resolveDependencies = (function () {
});
}

module.exports = function (ctx) {
// resolve modules
exec = ctx.requireCordovaModule('child_process').exec,
fs = ctx.requireCordovaModule('fs'),
path = ctx.requireCordovaModule('path'),
deferral = ctx.requireCordovaModule('q').defer();

/*************
* Public API
*************/
resolveDependencies.init = function (ctx, callback) {
completeCallback = callback,
// resolve paths
hooksPath = path.resolve(ctx.opts.projectRoot, "plugins", ctx.opts.plugin.id, "hooks");
logger = require(path.resolve(hooksPath, "logger.js"))(ctx),
sourcePackageJson = path.resolve(ctx.opts.projectRoot, "plugins", ctx.opts.plugin.id, "package.json"),
targetPackageJson = path.resolve(ctx.opts.projectRoot, "package.json"),
tempPackageJson = path.resolve(ctx.opts.projectRoot, "package.json.tmp"),
lockfile = path.resolve(ctx.opts.projectRoot, "plugins", ctx.opts.plugin.id, "dependency_resolution_lock"),
completefile = path.resolve(ctx.opts.projectRoot, "plugins", ctx.opts.plugin.id, "dependency_resolution_complete");
tempPackageJson = path.resolve(ctx.opts.projectRoot, "package.json.tmp");

checkCompletefile();
checkForRealPackageJson();
return deferral.promise;
};
return resolveDependencies;
})();

module.exports = function (ctx, callback) {
resolveDependencies.init(ctx, callback);
};
7 changes: 1 addition & 6 deletions hooks/restoreBackups.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,5 @@ module.exports = function(ctx) {
hooksPath = path.resolve(ctx.opts.projectRoot, "plugins", ctx.opts.plugin.id, "hooks");
logger = require(path.resolve(hooksPath, "logger.js"))(ctx);
logger.debug("Running restoreBackups.js");

if(ctx.hook === "before_plugin_uninstall"){
restoreBackups.init(ctx); // no time to check for deps or files will get removed by plugin rm before backups can be restored
}else{
require(path.resolve(hooksPath, "resolveDependencies.js"))(ctx, restoreBackups.init.bind(this, ctx));
}
require(path.resolve(hooksPath, "resolveDependencies.js"))(ctx).then(restoreBackups.init.bind(this, ctx));
};
17 changes: 8 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.2.1",
"version": "1.2.2",
"name": "cordova-custom-config",
"cordova_name": "cordova-custom-config",
"description": "Cordova/Phonegap plugin to update platform configuration files based on preferences and config-file data defined in config.xml.",
Expand Down Expand Up @@ -30,13 +30,12 @@
],

"dependencies": {
"fs-extra": "*",
"lodash": "*",
"elementtree": "*",
"plist": "*",
"xcode": "*",
"colors": "*",
"tostr": "*",
"dummy": "*"
"fs-extra": "0.26.5",
"lodash": "4.3.0",
"elementtree": "0.1.6",
"plist": "1.2.0",
"xcode": "0.8.3",
"colors": "1.1.2",
"tostr": "0.1.0"
}
}
4 changes: 2 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-custom-config"
version="1.2.1">
version="1.2.2">

<name>cordova-custom-config</name>
<description>Cordova/Phonegap plugin to update platform configuration files based on preferences and config-file data defined in config.xml</description>
Expand All @@ -23,5 +23,5 @@
<hook src="hooks/restoreBackups.js" type="before_plugin_uninstall" />
<hook src="hooks/applyCustomConfig.js" type="after_prepare" />
<hook src="hooks/resolveDependencies.js" type="after_plugin_install" />
<hook src="hooks/resolveDependencies.js" type="after_platform_add" />
<hook src="hooks/resolveDependencies.js" type="before_platform_add" />
</plugin>

0 comments on commit 4d6ce89

Please sign in to comment.