plugin-get-all.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // eslint-disable-next-line import/no-extraneous-dependencies
  2. const path = require('path')
  3. const fs = require('fs-extra')
  4. const chalk = require('chalk')
  5. const { came } = require('./util')
  6. // 验证是否是插件
  7. function isPlugin(source) {
  8. let result = true
  9. if (!fs.lstatSync(source).isDirectory()) {
  10. return false
  11. }
  12. const configPath = path.resolve(source, './stage-config.js')
  13. const packagePath = path.resolve(source, './package.json')
  14. if (result && !fs.existsSync(configPath)) {
  15. result = false
  16. }
  17. if (result && !fs.existsSync(packagePath)) {
  18. result = false
  19. }
  20. if (!result) {
  21. console.log(chalk.yellow(`${source} 不符合 Lin-CMS 插件规范`))
  22. }
  23. return result
  24. }
  25. function getPlugins(source) {
  26. if (!fs.existsSync(source)) {
  27. console.log(chalk.yellow(`目录不存在: ${source}`))
  28. return []
  29. }
  30. const folders = fs.readdirSync(source)
  31. const pluginsList = []
  32. folders.forEach(item => {
  33. const itemPath = path.join(source, item)
  34. if (!isPlugin(itemPath)) {
  35. return
  36. }
  37. const config = {}
  38. config.name = item
  39. config.camelCaseName = came(item)
  40. config.path = path.resolve(__dirname, `../src/plugins/${item}/`)
  41. config.packageCtx = JSON.parse(fs.readFileSync(path.resolve(itemPath, './package.json'), 'utf8'))
  42. pluginsList.push(config)
  43. })
  44. return pluginsList
  45. }
  46. module.exports = getPlugins