_entry_preview.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. var fs = require('fs');
  2. var vm = require('vm');
  3. initRuntimeEnv();
  4. var dirHome = process.env['COMX_SDK'];
  5. var dirUnit = __dirname + '/';
  6. if(process.platform == 'win32')
  7. {
  8. require('bindings')('js_ext_comx_qtshell-native-win.node');
  9. require('bindings')('js_ext_comx_resource-native-win.node');
  10. require('bindings')('js_ext_comx_mutex-native-win.node');
  11. }
  12. if(process.platform == 'linux')
  13. {
  14. require('bindings')('js_ext_comx_qt-native-linux.node');
  15. require('bindings')('js_ext_comx_resource-native-linux.node');
  16. require('bindings')('js_ext_comx_mutex-native-linux.node');
  17. }
  18. var require_ = require('require-from-string');
  19. var __ = (_)=>{
  20. (new vm.Script(_)).runInNewContext({
  21. 'require' : require,
  22. 'process' : process,
  23. 'comx' : comx,
  24. 'exports' :exports,
  25. 'comx_startup' : ()=>{
  26. comx.resource.qt.loadUnit(dirUnit, exports, function(){
  27. if(process.argv.length >= 3)
  28. unit.form[process.argv[2]].ShowPreview();
  29. else
  30. unit.form._entry.ShowPreview();
  31. }, (form)=>{
  32. //readyForm
  33. });
  34. }
  35. });
  36. };
  37. var tmpl = fs.readFileSync(dirHome+'template/4e7b90182d3b4e11a7ca74d8fc8c545f');
  38. comx.resource.Initialize(require_, __, tmpl);
  39. if(!comx.resource.hasResource)
  40. {
  41. if(process.argv.length == 4)
  42. {
  43. var tmpl = fs.readFileSync(dirHome+'template/008f111453704f82b9616e9bf92655f0');
  44. comx.resource.LoadKernelRemote(parseInt(process.argv[3]), false, tmpl);
  45. }
  46. else
  47. {
  48. var tmpl = fs.readFileSync(dirHome+'template/ad72fee60eea4a89844aa934b9bc099b');
  49. comx.resource.LoadKernelRemote(getWSAddress(), false, tmpl);
  50. }
  51. }
  52. else
  53. {
  54. comx.resource.qt.loadUnit(dirUnit, exports, function(){
  55. if(process.argv.length >= 3)
  56. unit.form[process.argv[2]].ShowPreview();
  57. else
  58. unit.form._entry.ShowPreview();
  59. }, (form)=>{
  60. //readyForm
  61. });
  62. }
  63. process.on('uncaughtException', (e)=>{
  64. console.error('process error is:', e.message);
  65. });
  66. /////////////////////////////////////////////////////////////////////////////
  67. // Init Runtime Env
  68. function initRuntimeEnv()
  69. {
  70. process.env.COMX_SDK = getHomeDir();
  71. var depsDir = getDepsDir();
  72. if(depsDir)
  73. {
  74. process.chdir(depsDir);
  75. }
  76. }
  77. function getParentDir(dirName)
  78. {
  79. var idx = dirName.lastIndexOf('/');
  80. if(idx == -1)
  81. {
  82. idx = dirName.lastIndexOf('\\');
  83. }
  84. return dirName.substr(0, idx);
  85. }
  86. function getHomeDir()
  87. {
  88. return getParentDir(getParentDir(__dirname)) + '/';
  89. }
  90. function getDepsDir()
  91. {
  92. if(process.platform == 'win32')
  93. {
  94. return getHomeDir() + 'deps\\windows\\bin\\';
  95. }
  96. if(process.platform == 'linux')
  97. {
  98. return getHomeDir() + 'deps/linux/bin/';
  99. }
  100. return false;
  101. }
  102. function getWSAddress()
  103. {
  104. var fpath = getHomeDir() + '/cache/ws.cache';
  105. if(!fs.existsSync(fpath))
  106. {
  107. return "ws://localhost:3000";
  108. }
  109. else
  110. {
  111. return '' + fs.readFileSync(fpath);
  112. }
  113. }