使用节点js将shell脚本转换为javascript

时间:2016-02-03 01:17:57

标签: node.js bash shell cordova ionic-framework

我有以下shell脚本,用于修改cordova应用程序的facebook插件的plist。

#!/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

cordova hooks guide上,建议在node.js中写入钩子,以便它们是跨平台的。另外,我是一个Windows用户,所以这个脚本在我的系统上不起作用。我提到this文章尝试使用节点将shell脚本转换为javascript但我不完全理解shell脚本。如何将此脚本转换为在节点中执行?

修改 我意识到我需要解释脚本中我不理解的内容。 前3行,我理解它是将文件放在变量PLIST

第4行是here标签。 在此之前我可以编写javascript来读取来自platforms / ios / / -Info.plist的文件

var fs = require ('fs');

var PLIST = fs.readFileSync(fileName);

我不明白接下来的几行是做什么的。 我理解的唯一部分是删除:LSApplicationQueriesSchemes,它删除一些变量或名为LSApplicationQueriesSchemes的部分,并可能用新值重写。

1 个答案:

答案 0 :(得分:1)

您可以使用plist包生成/更新plist文件。这是一般流程(已授予,您只有一个.plist文件):

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); 

毋庸置疑,您必须仔细比较bash生成的文件和nodejs,以确保您获得相同的结果。