utils.mjs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import fs from 'fs';
  2. import archiver from 'archiver';
  3. import { exec } from 'child_process';
  4. import { readdir, stat } from 'fs/promises';
  5. export function readFileRange(file, start, length) {
  6. return new Promise((resolve, reject) => {
  7. fs.open(file, 'r', (err, fd) => {
  8. if (err) {
  9. reject('Error opening file:', er);
  10. return;
  11. }
  12. const buffer = Buffer.alloc(length);
  13. fs.read(fd, buffer, 0, length, start, (err, bytesRead, buffer) => {
  14. if (err) {
  15. reject('Error reading file:', err)
  16. return;
  17. }
  18. fs.close(fd, (err) => {
  19. if (err) {
  20. reject('Error closing file:', err)
  21. return ;
  22. }
  23. resolve(buffer);
  24. });
  25. });
  26. });
  27. })
  28. }
  29. export function pad(num, n) {
  30. var strNum = num.toString();
  31. var len = strNum.length;
  32. while (len < n) {
  33. strNum = "0" + strNum;
  34. len++;
  35. }
  36. return strNum;
  37. }
  38. export function execAsync(command) {
  39. return new Promise((resolve, reject) => {
  40. console.log('[CMD] ' + command);
  41. exec(command, (error, stdout, stderr) => {
  42. if (error) {
  43. reject(error);
  44. return;
  45. }
  46. resolve(stdout);
  47. });
  48. })
  49. }
  50. /**
  51. * 压缩zip
  52. * @param {string} dir 压缩目录
  53. * @param {string} targetFilePath 压缩文件路径
  54. * @param {string[]} skipFiles 压缩忽略文件 支持按开头匹配,和文件类型通配符匹配,如:'*.js', '*.css', '*.html'
  55. * @returns {Promise<void>}
  56. */
  57. export async function compressZip(dir, targetFilePath, skipFiles) {
  58. const output = fs.createWriteStream(targetFilePath);
  59. const archive = archiver('zip', {
  60. zlib: { level: 9 } // Sets the compression level.
  61. });
  62. archive.pipe(output);
  63. function checkPathSkip(pathString) {
  64. if (!pathString)
  65. return true;
  66. if (pathString.startsWith('.') || pathString.startsWith('/.'))
  67. return true;
  68. if (!skipFiles || skipFiles.length === 0)
  69. return;
  70. return skipFiles.find((item) => (
  71. pathString.startsWith(item) || pathString.startsWith(`/${item}`)
  72. || (item.includes('*.') && pathString.endsWith(item.split('*')[1]))
  73. ));
  74. }
  75. async function loopDir(path, subPrefix) {
  76. const files = await readdir(path);
  77. for (const file of files) {
  78. const subPath = subPrefix + '/' + file;
  79. if (checkPathSkip(subPath))
  80. continue;
  81. console.log('File: ' + subPath);
  82. const filestat = await stat(dir + subPath);
  83. if (filestat.isDirectory()) {
  84. await loopDir(dir + subPath, subPath);
  85. } else {
  86. archive.file(dir + subPath, { name: subPath });
  87. }
  88. }
  89. }
  90. await loopDir(dir, '')
  91. console.log('等待压缩zip...');
  92. const waitArchive = new Promise((resolve, reject) => {
  93. output.on('close', function() {
  94. console.log(archive.pointer() + ' total bytes');
  95. console.log('archiver has been finalized and the output file descriptor has closed.');
  96. resolve();
  97. });
  98. archive.on('error', function(err) {
  99. reject(err);
  100. });
  101. })
  102. await archive.finalize();
  103. await waitArchive;
  104. }