lanucher.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. /**
  7. * egg server start
  8. *
  9. * @param {Object} argv
  10. * @return {Promise}
  11. */
  12. exports.start = function (argv) {
  13. const { env } = process;
  14. let baseDir = app.getAppPath();
  15. argv.baseDir = baseDir;
  16. argv.framework = path.join(baseDir, 'node_modules/egg');
  17. const appName = app.getName();
  18. argv.title = argv.title || `egg-server-${appName}`;
  19. // normalize env
  20. env.HOME = baseDir;
  21. env.NODE_ENV = 'production';
  22. // it makes env big but more robust
  23. env.PATH = env.Path = [
  24. // for nodeinstall
  25. path.join(baseDir, 'node_modules/.bin'),
  26. // support `.node/bin`, due to npm5 will remove `node_modules/.bin`
  27. path.join(baseDir, '.node/bin'),
  28. // adjust env for win
  29. env.PATH || env.Path,
  30. ].filter(x => !!x).join(path.delimiter);
  31. // for alinode
  32. env.ENABLE_NODE_LOG = 'YES';
  33. env.NODE_LOG_DIR = env.NODE_LOG_DIR || path.join(baseDir, 'logs/alinode');
  34. // cli argv -> process.env.EGG_SERVER_ENV -> `undefined` then egg will use `prod`
  35. if (argv.env) {
  36. // if undefined, should not pass key due to `spwan`, https://github.com/nodejs/node/blob/master/lib/child_process.js#L470
  37. env.EGG_SERVER_ENV = argv.env;
  38. }
  39. // remove unused properties from stringify, alias had been remove by `removeAlias`
  40. const ignoreKeys = [ '_', '$0', 'env', 'daemon', 'stdout', 'stderr', 'timeout', 'ignore-stderr', 'node' ];
  41. const clusterOptions = stringify(argv, ignoreKeys);
  42. const options = JSON.parse(clusterOptions);
  43. // console.log('[lanucher] options:', options)
  44. return new Promise((resolve, reject) => {
  45. startCluster(options, function(){
  46. resolve('success');
  47. });
  48. });
  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. }