No Description

restoreBackups.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/env node
  2. /**********
  3. * Globals
  4. **********/
  5. var TAG = "cordova-custom-config";
  6. var SCRIPT_NAME = "restoreBackups.js";
  7. // Pre-existing Cordova npm modules
  8. var deferral, path, cwd;
  9. // Npm dependencies
  10. var logger,
  11. fs,
  12. _,
  13. fileUtils;
  14. // Other globals
  15. var hooksPath;
  16. var restoreBackups = (function(){
  17. /**********************
  18. * Internal properties
  19. *********************/
  20. var restoreBackups = {}, context, projectName, logFn, settings;
  21. var PLATFORM_CONFIG_FILES = {
  22. "ios":{
  23. "{projectName}-Info.plist": "{projectName}/{projectName}-Info.plist",
  24. "project.pbxproj": "{projectName}.xcodeproj/project.pbxproj",
  25. "build.xcconfig": "cordova/build.xcconfig",
  26. "build-extras.xcconfig": "cordova/build-extras.xcconfig",
  27. "build-debug.xcconfig": "cordova/build-debug.xcconfig",
  28. "build-release.xcconfig": "cordova/build-release.xcconfig",
  29. "Entitlements-Release.plist": "{projectName}/Entitlements-Release.plist",
  30. "Entitlements-Debug.plist": "{projectName}/Entitlements-Debug.plist"
  31. },
  32. "android":{
  33. "AndroidManifest.xml": "AndroidManifest.xml"
  34. }
  35. };
  36. /*********************
  37. * Internal functions
  38. *********************/
  39. function restorePlatformBackups(platform){
  40. var configFiles = PLATFORM_CONFIG_FILES[platform],
  41. backupFile, backupFileName, backupFilePath, backupFileExists, targetFilePath;
  42. logger.verbose("Checking to see if there are backups to restore...");
  43. for(backupFile in configFiles){
  44. backupFileName = parseProjectName(backupFile);
  45. backupFilePath = path.join(cwd, 'plugins', context.opts.plugin.id, "backup", platform, backupFileName);
  46. backupFileExists = fileUtils.fileExists(backupFilePath);
  47. if(backupFileExists){
  48. targetFilePath = path.join(cwd, 'platforms', platform, parseProjectName(configFiles[backupFile]));
  49. fileUtils.copySync(backupFilePath, targetFilePath);
  50. logFn("Restored backup of '"+backupFileName+"' to :"+targetFilePath);
  51. }
  52. }
  53. }
  54. function parseProjectName(fileName){
  55. return fileName.replace(/{(projectName)}/g, projectName);
  56. }
  57. // Script operations are complete, so resolve deferred promises
  58. function complete(){
  59. deferral.resolve();
  60. }
  61. /*************
  62. * Public API
  63. *************/
  64. restoreBackups.loadDependencies = function(ctx){
  65. fs = require('fs'),
  66. _ = require('lodash'),
  67. fileUtils = require(path.resolve(hooksPath, "fileUtils.js"))(ctx);
  68. logger.verbose("Loaded module dependencies");
  69. };
  70. restoreBackups.init = function(ctx){
  71. context = ctx;
  72. projectName = fileUtils.getProjectName();
  73. logFn = context.hook === "before_plugin_uninstall" ? logger.log : logger.verbose;
  74. settings = fileUtils.getSettings();
  75. if(typeof(settings.autorestore) === "undefined" || settings.autorestore === "false"){
  76. logger.log("Skipping auto-restore of config file backup(s)");
  77. complete();
  78. return;
  79. }
  80. // go through each of the platform directories
  81. var platforms = _.filter(fs.readdirSync('platforms'), function (file) {
  82. return fs.statSync(path.resolve('platforms', file)).isDirectory();
  83. });
  84. _.each(platforms, function (platform, index) {
  85. platform = platform.trim().toLowerCase();
  86. try{
  87. restorePlatformBackups(platform);
  88. if(index === platforms.length - 1){
  89. logger.verbose("Finished restoring backups");
  90. complete();
  91. }
  92. }catch(e){
  93. var msg = "Error restoring backups for platform '"+platform+"': "+ e.message;
  94. logger.error(msg);
  95. if(settings.stoponerror){
  96. deferral.reject(TAG + ": " +msg);
  97. }
  98. }
  99. });
  100. };
  101. return restoreBackups;
  102. })();
  103. module.exports = function(ctx) {
  104. try{
  105. deferral = require('q').defer();
  106. path = require('path');
  107. cwd = path.resolve();
  108. hooksPath = path.resolve(ctx.opts.projectRoot, "plugins", ctx.opts.plugin.id, "hooks");
  109. logger = require(path.resolve(hooksPath, "logger.js"))(ctx);
  110. restoreBackups.loadDependencies(ctx);
  111. }catch(e){
  112. e.message = TAG + ": Error loading dependencies for "+SCRIPT_NAME+" - ensure the plugin has been installed via cordova-fetch or run 'npm install cordova-custom-config': "+e.message;
  113. if(typeof deferral !== "undefined"){
  114. deferral.reject(e.message);
  115. return deferral.promise;
  116. }
  117. throw e;
  118. }
  119. try{
  120. logger.verbose("Running " + SCRIPT_NAME);
  121. restoreBackups.init(ctx);
  122. }catch(e){
  123. var msg = TAG + ": Error running "+SCRIPT_NAME+": "+e.message;
  124. deferral.reject(msg);
  125. }
  126. return deferral.promise;
  127. };