首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用节点js将shell脚本转换为javascript

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

Stack Overflow用户
提问于 2016-02-03 01:17:57
回答 1查看 2.8K关注 0票数 0

我有下面的shell脚本,它是为cordova应用程序修改facebook插件的plist而创建的。

代码语言:javascript
复制
#!/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

科多瓦钩子指南上,建议用node.js编写钩子,因此它们是跨平台的。而且,我是一个windows用户,所以这个脚本不能在我的系统上工作。我参考了文章,尝试使用节点将shell脚本转换为javascript,但我并不完全理解shell脚本。如何将此脚本转换为在节点中执行?

编辑我意识到我需要解释我在脚本中不理解的内容。前3行,我知道它是在变量PLIST中获取文件。

第4行是这里的标签。在此之前,我可以编写javascript从平台/ios//-Info.plist读取文件,如

代码语言:javascript
复制
var fs = require ('fs');

var PLIST = fs.readFileSync(fileName);

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

EN

回答 1

Stack Overflow用户

发布于 2016-02-04 16:24:07

您可以使用plist包生成/更新plist文件。下面是一般流程(当然,您只有一个.plist文件):

代码语言:javascript
复制
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文件,以确保您有相同的结果。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35166833

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档