Skip to content

Instantly share code, notes, and snippets.

@csantanapr
Created August 12, 2014 02:52
Show Gist options
  • Save csantanapr/9fc45c76b4d9a2d5ef85 to your computer and use it in GitHub Desktop.
Save csantanapr/9fc45c76b4d9a2d5ef85 to your computer and use it in GitHub Desktop.
WL Hook
var path = require('path'),
fs = require('fs');
//this node modules will be require from cordova-lib in main function
var shell;
module.exports = function(context) {
//global module to be share globally in this module
shell = context.requireCordovaModule('shelljs');
// TODO: hardcoded for now (could be pass by cmdline arg or config,xml, or config.json)
var wlappPath = path.join('../fixtures/wlproject', 'apps/HybridTestApp');
var cdvProjName;
var wlNative;
var wlWWW;
var wlpropFile;
var cdvpropFile;
var parser;
var platformPath;
var hook = context.hook;
var currentPlatforms = context.opts.platforms;
var projectRoot = path.resolve(context.opts.projectRoot);
var platforms = context.requireCordovaModule('../cordova/platforms');
console.log('Worklight Hook');
//console.log(context);
//console.log(process.cwd());
currentPlatforms.forEach(function(platformId) {
platformPath = path.join(projectRoot, 'platforms', platformId);
parser = new platforms[platformId].parser(platformPath);
if (platformId === 'ios') {
if (hook === 'after_prepare') {
wlNative = 'iphone/native';
wlWWW = 'www';
wlpropFile = path.join(wlappPath, wlNative, 'worklight.plist');
cdvProjName = fs.readdirSync(platformPath).filter(function(e) { return e.match(/\.xcodeproj$/i); })[0];
cdvProjName = cdvProjName.substring(0, cdvProjName.indexOf('.xcodeproj'));
cdvpropFile = path.join(platformPath, cdvProjName, 'Resources/worklight.plist');
afterPrepare(platformPath, wlappPath, wlNative, wlWWW, wlpropFile, cdvpropFile, parser);
}
} else if (platformId === 'android') {
if (hook === 'after_prepare') {
wlNative = 'android/native';
wlWWW = 'assets/www';
wlpropFile = path.join(wlappPath, wlNative, 'assets', 'wlclient.properties');
cdvpropFile = path.join(platformPath, 'assets', 'wlclient.properties');
afterPrepare(platformPath, wlappPath, wlNative, wlWWW, wlpropFile, cdvpropFile, parser);
}
} else {
console.log('Hook not implemented yet for ' + platformId);
}
});
};
function afterPrepare(platformPath, wlappPath, wlNative, wlWWW, wlpropFile, cdvpropFile, parser) {
console.log('running worklight after prepare for ' + platformPath);
//TODO: this function should trigger a wlproject refresh to get an udpated version of worklight.plist, index.html (WLStaticAppProp)
var wlIndexHTML = path.join(wlappPath, wlNative, wlWWW, 'default/index.html');
var cdvWLStaticAppPropFile = path.join(parser.www_dir(), 'worklight/static_app_props.js');
var wlIndexHTMLString = fs.readFileSync(wlIndexHTML, {
encoding: 'utf8'
});
var WLStaticAppPropStart = wlIndexHTMLString.indexOf('WL.StaticAppProp');
var WLStaticAppPropEnd = wlIndexHTMLString.indexOf('};', WLStaticAppPropStart);
var WLStaticAppProp;
WLStaticAppProp = 'var skinName = "default";' + '\n';
WLStaticAppProp = WLStaticAppProp + 'cordova.exec(null,null,"WLApp","writeUserPref", [{"key" : "wlSkinName", "value": skinName}] );' + '\n';
WLStaticAppProp = WLStaticAppProp + 'var WL = WL ? WL : {};' + '\n';
WLStaticAppProp = WLStaticAppProp + wlIndexHTMLString.slice(WLStaticAppPropStart, WLStaticAppPropEnd) + '};' + '\n';
shell.mkdir('-p', path.dirname(cdvWLStaticAppPropFile));
console.log('Generating ' + cdvWLStaticAppPropFile);
fs.writeFileSync(cdvWLStaticAppPropFile, WLStaticAppProp, {
encoding: 'utf8'
});
copyFile(wlpropFile, cdvpropFile);
var wlchecksumFile = path.join(wlappPath, wlNative, wlWWW, 'default/worklight/checksum.js');
var cdvchecksumFile = path.join(parser.www_dir(), 'default/worklight/checksum.js');
//TODO: For now just copy from wlproject, this should be calculated by worklight from content of parser.www_dir()
copyFile(wlchecksumFile, cdvchecksumFile);
}
function copyFile(wlFile, cdvFile) {
console.log('copying', wlFile, cdvFile);
shell.mkdir('-p', path.dirname(cdvFile));
shell.cp('-f', wlFile, cdvFile);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment