generate.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. const fs = require('fs/promises');
  2. const path = require('path');
  3. const rimraf = require('rimraf')
  4. const basePath = path.resolve(__dirname, '../dist');
  5. async function run() {
  6. console.log('生成json文件...');
  7. const pluginPath = path.resolve(basePath, '_plugins');
  8. await rimraf(pluginPath);
  9. await fs.mkdir(pluginPath);
  10. const bundledPlugins = await fs.readdir(basePath);
  11. const output = {
  12. desc: "此链接为 MusicFree 插件,插件开发及使用方式参考 https://musicfree.upup.fun",
  13. plugins: []
  14. };
  15. await Promise.all(bundledPlugins.map(async (bundleFolder) => {
  16. if (!bundleFolder.startsWith('_')) {
  17. try {
  18. const targetPluginPath = path.resolve(basePath, bundleFolder, 'index.js');
  19. await fs.stat(targetPluginPath);
  20. const origin = await fs.readFile(targetPluginPath, 'utf-8');
  21. const mexports = origin.match(/module.exports\s*=\s*([\s\S]*)$/)[1];
  22. const platform = mexports.match(/platform:\s*['"`](.*)['"`]/)[1]
  23. const version = mexports.match(/version:\s*['"`](.*)['"`]/)?.[1]
  24. const srcUrl = mexports.match(/srcUrl:\s*['"`](.*)['"`]/)?.[1]
  25. output.plugins.push({
  26. name: platform,
  27. url: srcUrl,
  28. version: version
  29. })
  30. } catch(e) {
  31. console.warn('异常:', e);
  32. }
  33. }
  34. }))
  35. await fs.writeFile(path.resolve(pluginPath, 'plugins.json'), JSON.stringify(output));
  36. await fs.copyFile(path.resolve(pluginPath, 'plugins.json'), path.resolve(__dirname, '../plugins.json'))
  37. console.log('done√');
  38. }
  39. run();