file-util.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. const Fs = require('fs');
  2. const Path = require('path');
  3. /**
  4. * 文件工具
  5. */
  6. const FileUtil = {
  7. /**
  8. * 复制文件/文件夹
  9. * @param {Fs.PathLike} srcPath 源路径
  10. * @param {Fs.PathLike} destPath 目标路径
  11. */
  12. copy(srcPath, destPath) {
  13. if (!Fs.existsSync(srcPath)) return;
  14. const stats = Fs.statSync(srcPath);
  15. if (stats.isDirectory()) {
  16. if (!Fs.existsSync(destPath)) Fs.mkdirSync(destPath);
  17. const names = Fs.readdirSync(srcPath);
  18. for (const name of names) {
  19. this.copy(Path.join(srcPath, name), Path.join(destPath, name));
  20. }
  21. } else if (stats.isFile()) {
  22. Fs.writeFileSync(destPath, Fs.readFileSync(srcPath));
  23. }
  24. },
  25. /**
  26. * 删除文件/文件夹
  27. * @param {Fs.PathLike} path 路径
  28. */
  29. delete(path) {
  30. if (!Fs.existsSync(path)) return;
  31. const stats = Fs.statSync(path);
  32. if (stats.isDirectory()) {
  33. const names = Fs.readdirSync(path);
  34. for (const name of names) {
  35. this.delete(Path.join(path, name));
  36. }
  37. Fs.rmdirSync(path);
  38. } else if (stats.isFile()) {
  39. Fs.unlinkSync(path);
  40. }
  41. },
  42. /**
  43. * 遍历文件/文件夹并执行函数
  44. * @param {Fs.PathLike} path 路径
  45. * @param {(filePath: Fs.PathLike, stat: Fs.Stats) => void} handler 处理函数
  46. */
  47. map(path, handler) {
  48. if (!Fs.existsSync(path)) return
  49. const stats = Fs.statSync(path);
  50. if (stats.isDirectory()) {
  51. const names = Fs.readdirSync(path);
  52. for (const name of names) {
  53. this.map(Path.join(path, name), handler);
  54. }
  55. } else if (stats.isFile()) {
  56. handler(path, stats);
  57. }
  58. },
  59. // 递归创建目录 同步方法
  60. mkdirsSync(dirname) {
  61. if (Fs.existsSync(dirname)) {
  62. return true;
  63. } else {
  64. if (this.mkdirsSync(path.dirname(dirname))) {
  65. Fs.mkdirSync(dirname);
  66. return true;
  67. }
  68. }
  69. },
  70. //同步读取文件
  71. readFileSync: function (path) {
  72. return Fs.readFileSync(path, 'utf-8');
  73. },
  74. /**
  75. * 异步写文件
  76. * _path 路径
  77. * _msg 数据
  78. */
  79. writeFile: function (_path, _msg) {
  80. Fs.writeFile(_path, _msg, function (err) {
  81. if (err) throw err;
  82. })
  83. },
  84. /**
  85. * 是否路径
  86. */
  87. isPath: function (path) {
  88. return Fs.existsSync(path);
  89. },
  90. /**
  91. * 获取文件夹名字
  92. */
  93. getDirName(f){
  94. return path.dirname(f)
  95. },
  96. /**
  97. * 获取文件名字
  98. * type: 1 只获取文件名字
  99. * 2 获取文件后缀
  100. * 其他 获取文件名.后缀
  101. */
  102. getFilename(filepath = () => { throw new Error }, type = 1) {
  103. let result = '';
  104. if (type === 1) {
  105. result = path.basename(filepath);
  106. } else if (type === 2) {
  107. result = path.extname(filepath);
  108. } else {
  109. let basename = path.basename(filepath);
  110. let extname = path.extname(filepath);
  111. result = basename.substring(0, basename.indexOf(extname));
  112. }
  113. return result;
  114. },
  115. }
  116. module.exports = FileUtil;