Nikhil
Nikhil

Reputation: 1186

Converting a shell script to javascript using node js

I have the following shell script which is created to modify plist of facebook plugin for cordova applications.

#!/bin/bash                                                                                                    

# Put this in /hooks/after_prepare/
PLIST=platforms/ios/*/*-Info.plist                                                                             

cat << EOF |                                                                                                   
Add :NSAppTransportSecurity dict                                                                               
Add :NSAppTransportSecurity:NSAllowsArbitraryLoads bool YES                                                    
Add :NSAppTransportSecurity:NSExceptionDomains:facebook.com:NSIncludesSubdomains bool YES                      
Add :NSAppTransportSecurity:NSExceptionDomains:facebook.com:NSThirdPartyExceptionRequiresForwardSecrecy bool NO
Add :NSAppTransportSecurity:NSExceptionDomains:fbcdn.net:NSIncludesSubdomains bool YES                         
Add :NSAppTransportSecurity:NSExceptionDomains:fbcdn.net:NSThirdPartyExceptionRequiresForwardSecrecy bool NO   
Add :NSAppTransportSecurity:NSExceptionDomains:akamaihd.net:NSIncludesSubdomains bool YES                      
Add :NSAppTransportSecurity:NSExceptionDomains:akamaihd.net:NSThirdPartyExceptionRequiresForwardSecrecy bool NO

Delete :LSApplicationQueriesSchemes                                                                            

Add :LSApplicationQueriesSchemes array                                                                         
Add :LSApplicationQueriesSchemes:0 string  'fbapi'                                                             
Add :LSApplicationQueriesSchemes:1 string  'fbapi20130214'                                                     
Add :LSApplicationQueriesSchemes:2 string  'fbapi20130410'                                                     
Add :LSApplicationQueriesSchemes:3 string  'fbapi20130702'                                                     
Add :LSApplicationQueriesSchemes:4 string  'fbapi20131010'                                                     
Add :LSApplicationQueriesSchemes:5 string  'fbapi20131219'                                                     
Add :LSApplicationQueriesSchemes:6 string  'fbapi20140410'                                                     
Add :LSApplicationQueriesSchemes:7 string  'fbapi20140116'                                                     
Add :LSApplicationQueriesSchemes:8 string  'fbapi20150313'                                                     
Add :LSApplicationQueriesSchemes:9 string  'fbapi20150629'                                                     
Add :LSApplicationQueriesSchemes:10 string 'fbauth'                                                            
Add :LSApplicationQueriesSchemes:11 string 'fbauth2'                                                           
EOF                                                                                                            
while read line                                                                                                
do                                                                                                             
  /usr/libexec/PlistBuddy -c "$line" $PLIST                                                                    
done                                                                                                           

true

On the cordova hooks guide it is recommended that hooks be written in node.js so they are cross platform. Also, I am a windows user so this script does not work on my system. I referred this article to try and convert the shell script to javascript using node but i do not completely understand the shell script. How do I convert this script to execute in node?

EDIT I realise i need to explain what i did not understand in the script. First 3 lines i understand it is getting the file in variable PLIST

4th line is the here tag. Until this point i can write javascript to read file from platforms/ios//-Info.plist as

var fs = require ('fs');

var PLIST = fs.readFileSync(fileName);

I dont understand what the next few lines do. The only part i understand is the delete :LSApplicationQueriesSchemes , It deletes some variable or a section named LSApplicationQueriesSchemes and probably rewrite with new values.

Upvotes: 0

Views: 3336

Answers (1)

Shanoor
Shanoor

Reputation: 13692

You can use the plist package to generate/update your plist file. Here is the general flow (granted, you have only one .plist file):

var fs = require('fs');
var path = require('path');
var glob = require('glob');
var plist = require('plist');
var _ = require('lodash');

var p = path.normalize(__dirname + '/platforms/ios/*/*-Info.plist');
var files = glob.sync(p);

// if you have only one file
var filename = files[0];

// parse the original file
var obj = plist.parse(fs.readFileSync(filename, 'utf8'));

// build an object with everything to add
var objToAdd = {
  NSAppTransportSecurity: {
    NSAllowsArbitraryLoads: true,
    NSExceptionDomains: {
      'fbcdn.net': {
        NSIncludesSubdomains: true,
        NSThirdPartyExceptionRequiresForwardSecrecy: false
      },
      // ...
    }
  }
}

// modify the original loaded data for 'Delete :LSApplicationQueriesSchemes'
obj.LSApplicationQueriesSchemes = [
  'fbapi',
  'fbapi20130214',
  'fbapi20130410',
  // ...
];

// merge the 2 objects
var finalObj = _.merge(obj, objToAdd);

// build the plist
var finalPlist = plist.build(finalObj);

// write back to the file
fs.writeFileSync(filename, finalPlist); 

Needless to say you have to compare carefully the bash generated file and the nodejs one to be sure you have the same result.

Upvotes: 1

Related Questions