|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// Add Platform Class |
| 4 | +// v1.0 |
| 5 | +// Automatically adds the platform class to the body tag |
| 6 | +// after the `prepare` command. By placing the platform CSS classes |
| 7 | +// directly in the HTML built for the platform, it speeds up |
| 8 | +// rendering the correct layout/style for the specific platform |
| 9 | +// instead of waiting for the JS to figure out the correct classes. |
| 10 | + |
| 11 | +var fs = require('fs'); |
| 12 | +var path = require('path'); |
| 13 | + |
| 14 | +var rootdir = process.argv[2]; |
| 15 | + |
| 16 | +function addPlatformBodyTag(indexPath, platform) { |
| 17 | + // add the platform class to the body tag |
| 18 | + try { |
| 19 | + var platformClass = 'platform-' + platform; |
| 20 | + var cordovaClass = 'platform-cordova platform-webview'; |
| 21 | + |
| 22 | + var html = fs.readFileSync(indexPath, 'utf8'); |
| 23 | + |
| 24 | + var bodyTag = findBodyTag(html); |
| 25 | + if(!bodyTag) return; // no opening body tag, something's wrong |
| 26 | + |
| 27 | + if(bodyTag.indexOf(platformClass) > -1) return; // already added |
| 28 | + |
| 29 | + var newBodyTag = bodyTag; |
| 30 | + |
| 31 | + var classAttr = findClassAttr(bodyTag); |
| 32 | + if(classAttr) { |
| 33 | + // body tag has existing class attribute, add the classname |
| 34 | + var endingQuote = classAttr.substring(classAttr.length-1); |
| 35 | + var newClassAttr = classAttr.substring(0, classAttr.length-1); |
| 36 | + newClassAttr += ' ' + platformClass + ' ' + cordovaClass + endingQuote; |
| 37 | + newBodyTag = bodyTag.replace(classAttr, newClassAttr); |
| 38 | + |
| 39 | + } else { |
| 40 | + // add class attribute to the body tag |
| 41 | + newBodyTag = bodyTag.replace('>', ' class="' + platformClass + ' ' + cordovaClass + '">'); |
| 42 | + } |
| 43 | + |
| 44 | + html = html.replace(bodyTag, newBodyTag); |
| 45 | + |
| 46 | + fs.writeFileSync(indexPath, html, 'utf8'); |
| 47 | + |
| 48 | + process.stdout.write('add to body class: ' + platformClass + '\n'); |
| 49 | + } catch(e) { |
| 50 | + process.stdout.write(e); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +function findBodyTag(html) { |
| 55 | + // get the body tag |
| 56 | + try{ |
| 57 | + return html.match(/<body(?=[\s>])(.*?)>/gi)[0]; |
| 58 | + }catch(e){} |
| 59 | +} |
| 60 | + |
| 61 | +function findClassAttr(bodyTag) { |
| 62 | + // get the body tag's class attribute |
| 63 | + try{ |
| 64 | + return bodyTag.match(/ class=["|'](.*?)["|']/gi)[0]; |
| 65 | + }catch(e){} |
| 66 | +} |
| 67 | + |
| 68 | +if (rootdir) { |
| 69 | + |
| 70 | + // go through each of the platform directories that have been prepared |
| 71 | + var platforms = (process.env.CORDOVA_PLATFORMS ? process.env.CORDOVA_PLATFORMS.split(',') : []); |
| 72 | + |
| 73 | + for(var x=0; x<platforms.length; x++) { |
| 74 | + // open up the index.html file at the www root |
| 75 | + try { |
| 76 | + var platform = platforms[x].trim().toLowerCase(); |
| 77 | + var indexPath; |
| 78 | + |
| 79 | + if(platform == 'android') { |
| 80 | + indexPath = path.join('platforms', platform, 'assets', 'www', 'index.html'); |
| 81 | + } else { |
| 82 | + indexPath = path.join('platforms', platform, 'www', 'index.html'); |
| 83 | + } |
| 84 | + |
| 85 | + if(fs.existsSync(indexPath)) { |
| 86 | + addPlatformBodyTag(indexPath, platform); |
| 87 | + } |
| 88 | + |
| 89 | + } catch(e) { |
| 90 | + process.stdout.write(e); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments