index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. 'use strict';
  2. const fs = require('fs');
  3. const path = require('path');
  4. const utility = require('utility');
  5. [
  6. require('./lib/framework'),
  7. require('./lib/plugin'),
  8. { getFrameworkOrEggPath },
  9. ]
  10. .forEach(obj => Object.assign(exports, obj));
  11. /**
  12. * Try to get framework dir path
  13. * If can't find any framework, try to find egg dir path
  14. *
  15. * @param {String} cwd - current work path
  16. * @param {Array} [eggNames] - egg names, default is ['egg']
  17. * @return {String} framework or egg dir path
  18. * @deprecated
  19. */
  20. function getFrameworkOrEggPath(cwd, eggNames) {
  21. eggNames = eggNames || [ 'egg' ];
  22. const moduleDir = path.join(cwd, 'node_modules');
  23. if (!fs.existsSync(moduleDir)) {
  24. return '';
  25. }
  26. // try to get framework
  27. // 1. try to read egg.framework property on package.json
  28. const pkgFile = path.join(cwd, 'package.json');
  29. if (fs.existsSync(pkgFile)) {
  30. const pkg = utility.readJSONSync(pkgFile);
  31. if (pkg.egg && pkg.egg.framework) {
  32. return path.join(moduleDir, pkg.egg.framework);
  33. }
  34. }
  35. // 2. try the module dependencies includes eggNames
  36. const names = fs.readdirSync(moduleDir);
  37. for (const name of names) {
  38. const pkgfile = path.join(moduleDir, name, 'package.json');
  39. if (!fs.existsSync(pkgfile)) {
  40. continue;
  41. }
  42. const pkg = utility.readJSONSync(pkgfile);
  43. if (pkg.dependencies) {
  44. for (const eggName of eggNames) {
  45. if (pkg.dependencies[eggName]) {
  46. return path.join(moduleDir, name);
  47. }
  48. }
  49. }
  50. }
  51. // try to get egg
  52. for (const eggName of eggNames) {
  53. const pkgfile = path.join(moduleDir, eggName, 'package.json');
  54. if (fs.existsSync(pkgfile)) {
  55. return path.join(moduleDir, eggName);
  56. }
  57. }
  58. return '';
  59. }