common.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. const util = require('util');
  2. const fs = require('fs');
  3. const path = require('path');
  4. Date.prototype.format = function(fmt) {
  5. var o = {
  6. "M+" : this.getMonth()+1, //月份
  7. "d+" : this.getDate(), //日
  8. "h+" : this.getHours(), //小时
  9. "m+" : this.getMinutes(), //分
  10. "s+" : this.getSeconds(), //秒
  11. "q+" : Math.floor((this.getMonth()+3)/3), //季度
  12. "S" : this.getMilliseconds() //毫秒
  13. };
  14. if(/(y+)/.test(fmt)) {
  15. fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
  16. }
  17. for(var k in o) {
  18. if(new RegExp("("+ k +")").test(fmt)){
  19. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
  20. }
  21. }
  22. return fmt;
  23. }
  24. function isEmpty(data){
  25. if(data === null || data === undefined){
  26. return true;
  27. }
  28. if(typeof data === 'string' && data === ''){
  29. return true;
  30. }
  31. if(Array.isArray(data) && data.length === 0){
  32. return true;
  33. }
  34. return false;
  35. }
  36. function deleteFolder(dirPath) {
  37. if(fs.existsSync(dirPath)){
  38. fs.readdirSync(dirPath).forEach(function (file) {
  39. let curPath = path.join(dirPath, file);
  40. if(fs.statSync(curPath).isDirectory()) {
  41. //删除文件夹
  42. deleteFolder(curPath);
  43. } else {
  44. //删除文件
  45. fs.unlinkSync(curPath);
  46. }
  47. });
  48. //删除当前文件夹
  49. fs.rmdirSync(dirPath);
  50. }
  51. }
  52. function copyFolder(from, to) { // 复制文件夹到指定目录
  53. let files = [];
  54. if (fs.existsSync(to)) { // 文件是否存在 如果不存在则创建
  55. files = fs.readdirSync(from);
  56. files.forEach(function (file, index) {
  57. var targetPath = from + "/" + file;
  58. var toPath = to + '/' + file;
  59. if (fs.statSync(targetPath).isDirectory()) { // 复制文件夹
  60. copyFolder(targetPath, toPath);
  61. } else { // 拷贝文件
  62. fs.copyFileSync(targetPath, toPath);
  63. }
  64. });
  65. } else {
  66. fs.mkdirSync(to);
  67. copyFolder(from, to);
  68. }
  69. }
  70. function responseMessage(data,code,codeMessage){
  71. code = code || 0;
  72. codeMessage = codeMessage || 'SUCCESS';
  73. return {
  74. code: code,
  75. codeMessage: codeMessage,
  76. data: data
  77. }
  78. }
  79. /**
  80. * 创建路径
  81. * @param dirPath 必须是从根目录开始的路径
  82. * @returns {string}
  83. */
  84. function mkdirPath(dirPath){
  85. dirPath = dirPath.replace("\\", "/");
  86. let tempDirArray=dirPath.split('/');
  87. let projectPath = "";
  88. for (let i = 0; i < tempDirArray.length; i++) {
  89. if(i !== 0){
  90. projectPath = projectPath+'/'+tempDirArray[i];
  91. }else{
  92. projectPath = tempDirArray[i];
  93. }
  94. if(projectPath !== ""){
  95. if (fs.existsSync(projectPath)) {
  96. let tempstats = fs.statSync(projectPath);
  97. if (!(tempstats.isDirectory())) {
  98. console.error(projectPath+" is not a directory");
  99. throw new Error(projectPath+" is not a directory");
  100. }
  101. }
  102. else{
  103. fs.mkdirSync(projectPath);
  104. }
  105. }
  106. }
  107. return projectPath;
  108. }
  109. var common = {
  110. isEmpty: isEmpty,
  111. responseMessage: responseMessage,
  112. deleteFolder: deleteFolder,
  113. copyFolder: copyFolder,
  114. mkdirPath: mkdirPath,
  115. }
  116. module.exports = common;