暫無描述

fileUtils.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env node
  2. /**********
  3. * Globals
  4. **********/
  5. var fs,
  6. path,
  7. _,
  8. et,
  9. tostr;
  10. /**
  11. * Provides files utilities
  12. */
  13. var fileUtils = (function(){
  14. /**********************
  15. * Internal properties
  16. *********************/
  17. var fileUtils = {}, context, configXmlData, settings;
  18. /************
  19. * Public API
  20. ************/
  21. // Parses a given file into an elementtree object
  22. fileUtils.parseElementtreeSync = function(filename) {
  23. var contents = fs.readFileSync(filename, 'utf-8');
  24. if(contents) {
  25. //Windows is the BOM. Skip the Byte Order Mark.
  26. contents = contents.substring(contents.indexOf('<'));
  27. }
  28. return new et.ElementTree(et.XML(contents));
  29. };
  30. // Parses the config.xml into an elementtree object and stores in the config object
  31. fileUtils.getConfigXml = function() {
  32. if(!configXmlData) {
  33. configXmlData = fileUtils.parseElementtreeSync(path.join(context.opts.projectRoot, 'config.xml'));
  34. }
  35. return configXmlData;
  36. };
  37. // Returns plugin settings from config.xml
  38. fileUtils.getSettings = function (){
  39. if(!settings){
  40. settings = {};
  41. var name, preferences = fileUtils.getConfigXml().findall("preference");
  42. _.each(preferences, function (preference) {
  43. name = preference.attrib.name;
  44. if(name.match("cordova-custom-config")){
  45. settings[name.split('-').pop()] = preference.attrib.value;
  46. }
  47. });
  48. }
  49. return settings;
  50. };
  51. // Returns project name from config.xml
  52. fileUtils.getProjectName = function(){
  53. if(!configXmlData) {
  54. fileUtils.getConfigXml();
  55. }
  56. return configXmlData.findtext('name');
  57. };
  58. fileUtils.fileExists = function(filePath){
  59. try {
  60. return fs.statSync(filePath).isFile();
  61. }
  62. catch (err) {
  63. return false;
  64. }
  65. };
  66. fileUtils.directoryExists = function(dirPath){
  67. try {
  68. return fs.statSync(dirPath).isDirectory();
  69. }
  70. catch (err) {
  71. return false;
  72. }
  73. };
  74. fileUtils.createDirectory = function (dirPath){
  75. return fs.mkdirSync(dirPath);
  76. };
  77. fileUtils.copySync = function (sourcePath, targetPath){
  78. var contents = fs.readFileSync(sourcePath);
  79. fs.writeFileSync(targetPath, contents);
  80. };
  81. fileUtils.copySyncRelative = function (sourcePath, targetPath){
  82. fileUtils.copySync(path.resolve(sourcePath), path.resolve(targetPath));
  83. };
  84. fileUtils.init = function(ctx){
  85. context = ctx;
  86. // Load modules
  87. fs = require('fs');
  88. path = require('path');
  89. _ = require('lodash');
  90. et = require('elementtree');
  91. tostr = require('tostr');
  92. };
  93. return fileUtils;
  94. })();
  95. module.exports = function(ctx){
  96. fileUtils.init(ctx);
  97. return fileUtils;
  98. };