lanucher.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use strict';
  2. const path = require('path');
  3. const startCluster = require('egg-cluster').startCluster;
  4. const {app} = require('electron');
  5. exports = module.exports;
  6. exports.start = function (argv) {
  7. const { env } = process;
  8. let baseDir = app.getAppPath();
  9. argv.baseDir = baseDir;
  10. argv.framework = path.join(baseDir, 'node_modules/egg');
  11. const pkgInfo = require(path.join(baseDir, 'package.json'));
  12. argv.title = argv.title || `egg-server-${pkgInfo.name}`;
  13. // normalize env
  14. env.HOME = baseDir;
  15. env.NODE_ENV = 'production';
  16. // it makes env big but more robust
  17. env.PATH = env.Path = [
  18. // for nodeinstall
  19. path.join(baseDir, 'node_modules/.bin'),
  20. // support `.node/bin`, due to npm5 will remove `node_modules/.bin`
  21. path.join(baseDir, '.node/bin'),
  22. // adjust env for win
  23. env.PATH || env.Path,
  24. ].filter(x => !!x).join(path.delimiter);
  25. // for alinode
  26. env.ENABLE_NODE_LOG = 'YES';
  27. env.NODE_LOG_DIR = env.NODE_LOG_DIR || path.join(baseDir, 'logs/alinode');
  28. // cli argv -> process.env.EGG_SERVER_ENV -> `undefined` then egg will use `prod`
  29. if (argv.env) {
  30. // if undefined, should not pass key due to `spwan`, https://github.com/nodejs/node/blob/master/lib/child_process.js#L470
  31. env.EGG_SERVER_ENV = argv.env;
  32. }
  33. // remove unused properties from stringify, alias had been remove by `removeAlias`
  34. const ignoreKeys = [ '_', '$0', 'env', 'daemon', 'stdout', 'stderr', 'timeout', 'ignore-stderr', 'node' ];
  35. const clusterOptions = stringify(argv, ignoreKeys);
  36. const options = JSON.parse(clusterOptions);
  37. // console.log('options:', {
  38. // argv,
  39. // options
  40. // });
  41. return new Promise((resolve, reject) => {
  42. startCluster(options, function(){
  43. resolve('success');
  44. });
  45. });
  46. };
  47. exports.stop = function () {
  48. return true;
  49. };
  50. function stringify(obj, ignore) {
  51. const result = {};
  52. Object.keys(obj).forEach(key => {
  53. if (!ignore.includes(key)) {
  54. result[key] = obj[key];
  55. }
  56. });
  57. return JSON.stringify(result);
  58. }