pathUtil.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. var fs = require('fs');
  2. var path = require('path');
  3. var Constants = require('./constants');
  4. var exp = module.exports;
  5. /**
  6. * Get system remote service path
  7. *
  8. * @param {String} role server role: frontend, backend
  9. * @return {String} path string if the path exist else null
  10. */
  11. exp.getSysRemotePath = function(role) {
  12. var p = path.join(__dirname, '/../common/remote/', role);
  13. return fs.existsSync(p) ? p : null;
  14. };
  15. /**
  16. * Get userstate remote service path
  17. *
  18. * @param {String} appBase application base path
  19. * @param {String} serverType server type
  20. * @return {String} path string if the path exist else null
  21. */
  22. exp.getUserRemotePath = function(appBase, serverType) {
  23. var p = path.join(appBase, '/app/servers/', serverType, Constants.DIR.REMOTE);
  24. return fs.existsSync(p) ? p : null;
  25. };
  26. /**
  27. * Get userstate remote cron path
  28. *
  29. * @param {String} appBase application base path
  30. * @param {String} serverType server type
  31. * @return {String} path string if the path exist else null
  32. */
  33. exp.getCronPath = function(appBase, serverType) {
  34. var p = path.join(appBase, '/app/servers/', serverType, Constants.DIR.CRON);
  35. return fs.existsSync(p) ? p : null;
  36. };
  37. /**
  38. * List all the subdirectory names of userstate remote directory
  39. * which hold the codes for all the server types.
  40. *
  41. * @param {String} appBase application base path
  42. * @return {Array} all the subdiretory name under servers/
  43. */
  44. exp.listUserRemoteDir = function(appBase) {
  45. var base = path.join(appBase, '/app/servers/');
  46. var files = fs.readdirSync(base);
  47. return files.filter(function(fn) {
  48. if(fn.charAt(0) === '.') {
  49. return false;
  50. }
  51. return fs.statSync(path.join(base, fn)).isDirectory();
  52. });
  53. };
  54. /**
  55. * Compose remote path record
  56. *
  57. * @param {String} namespace remote path namespace, such as: 'sys', 'userstate'
  58. * @param {String} serverType
  59. * @param {String} path remote service source path
  60. * @return {Object} remote path record
  61. */
  62. exp.remotePathRecord = function(namespace, serverType, path) {
  63. return {namespace: namespace, serverType: serverType, path: path};
  64. };
  65. /**
  66. * Get handler path
  67. *
  68. * @param {String} appBase application base path
  69. * @param {String} serverType server type
  70. * @return {String} path string if the path exist else null
  71. */
  72. exp.getHandlerPath = function(appBase, serverType) {
  73. var p = path.join(appBase, '/app/servers/', serverType, Constants.DIR.HANDLER);
  74. return fs.existsSync(p) ? p : null;
  75. };
  76. /**
  77. * Get admin script root path.
  78. *
  79. * @param {String} appBase application base path
  80. * @return {String} script path string
  81. */
  82. exp.getScriptPath = function(appBase) {
  83. return path.join(appBase, Constants.DIR.SCRIPT);
  84. };
  85. /**
  86. * Get logs path.
  87. *
  88. * @param {String} appBase application base path
  89. * @return {String} logs path string
  90. */
  91. exp.getLogPath = function(appBase) {
  92. return path.join(appBase, Constants.DIR.LOG);
  93. };