build.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import fs from 'node:fs';
  2. import path from 'path';
  3. import uglifyjs from 'uglify-js';
  4. const copySpider = ['app', 'kunyu77_open', 'alist_open'];
  5. const root = process.cwd();
  6. const src = path.join(root);
  7. const out = path.join(root, 'dist');
  8. function minify(s, d) {
  9. var jsContent = fs.readFileSync(s).toString();
  10. jsContent = jsContent.replace('./lib/cat.js', 'assets://js/lib/cat.js');
  11. jsContent = jsContent.replace('./cat.js', 'assets://js/lib/cat.js');
  12. jsContent = uglifyjs.minify(jsContent, {
  13. mangle: false,
  14. });
  15. fs.writeFileSync(d, jsContent.code);
  16. }
  17. function listAllFiles(dirPath, arrayOfFiles) {
  18. var files = fs.readdirSync(dirPath);
  19. arrayOfFiles = arrayOfFiles || [];
  20. files.forEach(function (file) {
  21. if (fs.statSync(dirPath + '/' + file).isDirectory()) {
  22. arrayOfFiles = listAllFiles(dirPath + '/' + file, arrayOfFiles);
  23. } else {
  24. arrayOfFiles.push(path.join(dirPath, '/', file));
  25. }
  26. });
  27. return arrayOfFiles;
  28. }
  29. function src2Out() {
  30. if (fs.existsSync(out)) fs.rmSync(out, { recursive: true, force: true });
  31. fs.mkdirSync(out, { recursive: true });
  32. const libSrc = path.join(src, 'lib');
  33. const libOut = path.join(out, 'lib');
  34. fs.mkdirSync(libOut, { recursive: true });
  35. const libs = listAllFiles(libSrc);
  36. for (let index = 0; index < libs.length; index++) {
  37. const element = libs[index];
  38. const relative = path.relative(libSrc, element);
  39. minify(element, path.join(libOut, relative));
  40. }
  41. for (const sp of copySpider) {
  42. minify(path.join(src, sp + '.js'), path.join(out, sp + '.js'));
  43. }
  44. }
  45. src2Out();
  46. fs.copyFileSync(path.join(src, 'config_open.json'), path.join(out, 'config_open.json'));
  47. console.log('done');