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 | 1 1 1 1 1 1 1 1 1 1 1 1 1 | //#!/usr/bin/env node /********** * Globals **********/ // Pre-existing Cordova npm modules var deferral, path, cwd; // Npm dependencies var logger, fs, _, fileUtils; // Other globals var hooksPath; var restoreBackups = (function(){ /********************** * Internal properties *********************/ var restoreBackups = {}, context, projectName, logFn, settings; var PLATFORM_CONFIG_FILES = { "ios":{ "{projectName}-Info.plist": "{projectName}/{projectName}-Info.plist", "project.pbxproj": "{projectName}.xcodeproj/project.pbxproj", "build.xcconfig": "cordova/build.xcconfig", "build-extras.xcconfig": "cordova/build-extras.xcconfig", "build-debug.xcconfig": "cordova/build-debug.xcconfig", "build-release.xcconfig": "cordova/build-release.xcconfig", "Entitlements-Release.plist": "{projectName}/Entitlements-Release.plist", "Entitlements-Debug.plist": "{projectName}/Entitlements-Debug.plist" }, "android":{ "AndroidManifest.xml": "AndroidManifest.xml" } }; /********************* * Internal functions *********************/ function restorePlatformBackups(platform){ var configFiles = PLATFORM_CONFIG_FILES[platform], backupFile, backupFileName, backupFilePath, backupFileExists, targetFilePath; logger.verbose("Checking to see if there are backups to restore..."); for(backupFile in configFiles){ backupFileName = parseProjectName(backupFile); backupFilePath = path.join(cwd, 'plugins', context.opts.plugin.id, "backup", platform, backupFileName); backupFileExists = fileUtils.fileExists(backupFilePath); if(backupFileExists){ targetFilePath = path.join(cwd, 'platforms', platform, parseProjectName(configFiles[backupFile])); fileUtils.copySync(backupFilePath, targetFilePath); logFn("Restored backup of '"+backupFileName+"' to :"+targetFilePath); } } } function parseProjectName(fileName){ return fileName.replace(/{(projectName)}/g, projectName); } // Script operations are complete, so resolve deferred promises function complete(){ deferral.resolve(); } /************* * Public API *************/ restoreBackups.loadDependencies = function(ctx){ fs = require('fs'), _ = require('lodash'), fileUtils = require(path.resolve(hooksPath, "fileUtils.js"))(ctx); logger.verbose("Loaded module dependencies"); restoreBackups.init(ctx); }; restoreBackups.init = function(ctx){ context = ctx; projectName = fileUtils.getProjectName(); logFn = context.hook === "before_plugin_uninstall" ? logger.log : logger.verbose; settings = fileUtils.getSettings(); if(typeof(settings.autorestore) === "undefined" || settings.autorestore === "false"){ logger.log("Skipping auto-restore of config file backup(s)"); complete(); return; } // go through each of the platform directories var platforms = _.filter(fs.readdirSync('platforms'), function (file) { return fs.statSync(path.resolve('platforms', file)).isDirectory(); }); _.each(platforms, function (platform, index) { platform = platform.trim().toLowerCase(); try{ restorePlatformBackups(platform); if(index === platforms.length - 1){ logger.verbose("Finished restoring backups"); complete(); } }catch(e){ var msg = "Error restoring backups for platform '"+platform+"': "+ e.message; logger.error(msg); if(settings.stoponerror){ deferral.reject(msg); } } }); }; return restoreBackups; })(); module.exports = function(ctx) { deferral = ctx.requireCordovaModule('q').defer(); path = ctx.requireCordovaModule('path'); cwd = path.resolve(); hooksPath = path.resolve(ctx.opts.projectRoot, "plugins", ctx.opts.plugin.id, "hooks"); logger = require(path.resolve(hooksPath, "logger.js"))(ctx); logger.verbose("Running restoreBackups.js"); try{ restoreBackups.loadDependencies(ctx); }catch(e){ logger.warn("Error loading dependencies ("+e.message+") - attempting to resolve"); require(path.resolve(hooksPath, "resolveDependencies.js"))(ctx).then(restoreBackups.loadDependencies.bind(this, ctx)); } return deferral.promise; }; |