lanucher.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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.NODE_ENV = 'production';
  21. // it makes env big but more robust
  22. env.PATH = env.Path = [
  23. // for nodeinstall
  24. path.join(baseDir, 'node_modules/.bin'),
  25. // support `.node/bin`, due to npm5 will remove `node_modules/.bin`
  26. path.join(baseDir, '.node/bin'),
  27. // adjust env for win
  28. env.PATH || env.Path,
  29. ].filter(x => !!x).join(path.delimiter);
  30. // cli argv -> process.env.EGG_SERVER_ENV -> `undefined` then egg will use `prod`
  31. if (argv.env) {
  32. // if undefined, should not pass key due to `spwan`, https://github.com/nodejs/node/blob/master/lib/child_process.js#L470
  33. env.EGG_SERVER_ENV = argv.env;
  34. }
  35. // remove unused properties from stringify, alias had been remove by `removeAlias`
  36. const ignoreKeys = [ '_', '$0', 'env', 'daemon', 'stdout', 'stderr', 'timeout', 'ignore-stderr', 'node' ];
  37. const clusterOptions = stringify(argv, ignoreKeys);
  38. const options = JSON.parse(clusterOptions);
  39. // console.log('[lanucher] options:', options)
  40. return new Promise((resolve, reject) => {
  41. startCluster(options, function(){
  42. resolve('success');
  43. });
  44. });
  45. };
  46. function stringify(obj, ignore) {
  47. const result = {};
  48. Object.keys(obj).forEach(key => {
  49. if (!ignore.includes(key)) {
  50. result[key] = obj[key];
  51. }
  52. });
  53. return JSON.stringify(result);
  54. }