common.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Author: Derek Chia <snipking@gmail.com>
  3. * common functions for cordova plugin after hook
  4. */
  5. const fs = require('fs');
  6. const path = require('path');
  7. module.exports.addAPNSinEntitlements = (entitlementPath, isProduction) => {
  8. if( fs.existsSync(entitlementPath) ) {
  9. fs.readFile(entitlementPath, "utf8", function(err, data) {
  10. if (err) {
  11. throw err;
  12. }
  13. console.log("Reading entitlements file asynchronously");
  14. let toInsert = '<key>aps-environment</key>\n' +
  15. '\t\t<string>development</string>';
  16. if(isProduction) {
  17. toInsert = '<key>aps-environment</key>\n' +
  18. '\t\t<string>production</string>';
  19. }
  20. let re1 = new RegExp('<key>aps-environment<\/key>(.|[\r\n])*<string>.*<\/string>');
  21. let matched = data.match(re1);
  22. let result;
  23. if (matched === null) {
  24. if(data.match(/<\/dict>/g)) {
  25. result = data.replace(/<\/dict>/, '\t' + toInsert + '\n\t</dict>');
  26. } else if(data.match(/<dict\/>/g)) {
  27. result = data.replace(/<dict\/>/, '\t<dict>\n\t\t' + toInsert + '\n\t</dict>');
  28. }
  29. } else {
  30. result = data.replace(re1, toInsert);
  31. }
  32. // write result to entitlements file
  33. fs.writeFile(entitlementPath, result, {"encoding": 'utf8'}, function(err) {
  34. if (err) {
  35. throw err;
  36. }
  37. console.log(entitlementPath + " written successfully");
  38. });
  39. });
  40. } else {
  41. console.log("Entitlement File '" + entitlementPath + "' not found. Make sure your ios platform upper than 4.3.0");
  42. }
  43. }
  44. module.exports.removeAPNSinEntitlements = (entitlementPath) => {
  45. if( fs.existsSync(entitlementPath) ) {
  46. fs.readFile(entitlementPath, "utf8", function(err, data) {
  47. if (err) {
  48. throw err;
  49. }
  50. console.log("Reading entitlements file asynchronously");
  51. let re1 = new RegExp('<key>aps-environment<\/key>(.|[\r\n])*<string>.*<\/string>');
  52. let matched = data.match(re1);
  53. let result;
  54. if (matched != null) {
  55. result = data.replace(re1, "");
  56. }
  57. // write result to entitlements file
  58. fs.writeFile(entitlementPath, result, {"encoding": 'utf8'}, function(err) {
  59. if (err) {
  60. throw err;
  61. }
  62. console.log(entitlementPath + " written successfully");
  63. });
  64. });
  65. } else {
  66. console.log("Entitlement File '" + entitlementPath + "' not found. Make sure your ios platform upper than 4.3.0");
  67. }
  68. }
  69. module.exports.getXcodeProjName = (searchPath) => {
  70. if(searchPath == null || searchPath == undefined) {
  71. searchPath = './';
  72. }
  73. let resultFolderName = null;
  74. let folderNames = fs.readdirSync(searchPath).filter(file => fs.lstatSync(path.join(searchPath, file)).isDirectory());
  75. let folderNamesReg = new RegExp('.*\.xcodeproj', 'g') // get filder name like `*.xcodeproj`
  76. for(let folderName of folderNames) {
  77. if(folderName.match(folderNamesReg)) {
  78. resultFolderName = folderName;
  79. break;
  80. }
  81. }
  82. return resultFolderName.substr(0, resultFolderName.length - 10);
  83. }