generate.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. plugins: []
  13. };
  14. await Promise.all(bundledPlugins.map(async (bundleFolder) => {
  15. if (!bundleFolder.startsWith('_')) {
  16. try {
  17. const targetPluginPath = path.resolve(basePath, bundleFolder, 'index.js');
  18. await fs.stat(targetPluginPath);
  19. const origin = await fs.readFile(targetPluginPath, 'utf-8');
  20. const mexports = origin.match(/module.exports\s*=\s*([\s\S]*)$/)[1];
  21. const platform = mexports.match(/platform:\s*['"`](.*)['"`]/)[1]
  22. const version = mexports.match(/version:\s*['"`](.*)['"`]/)[1]
  23. const srcUrl = mexports.match(/srcUrl:\s*['"`](.*)['"`]/)[1]
  24. output.plugins.push({
  25. name: platform,
  26. url: srcUrl,
  27. version: version
  28. })
  29. } catch(e) {
  30. console.warn('异常:', e);
  31. }
  32. }
  33. }))
  34. await fs.writeFile(path.resolve(pluginPath, 'plugins.json'), JSON.stringify(output));
  35. await fs.copyFile(path.resolve(pluginPath, 'plugins.json'), path.resolve(__dirname, '../plugins.json'))
  36. console.log('done√');
  37. }
  38. run();