test-server.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. var fs = require ('fs');
  2. var os = require ('os');
  3. var exec = require('child_process').exec;
  4. var ServeMe = require("serve-me");
  5. var dir = './www';
  6. var cmd = '';
  7. var cmdPrepare = '';
  8. var host = '';
  9. var preferredTarget = '';
  10. // var confFile = 'config.xml';
  11. var confFile = 'www/js/apppreferences-test.js'
  12. if (process.argv[2] === 'ios') {
  13. dir = './platforms/ios/www';
  14. // cmd = 'ios-sim launch ./platforms/ios/build/emulator/HelloCordova.app --log ./console.log --devicetypeid iPhone-6 --exit';
  15. cmd = 'cordova emulate ios';
  16. cmdPrepare = 'cordova prepare ios';
  17. host = '127.0.0.1';
  18. preferredTarget = 'iPhone-5s'
  19. } else if (process.argv[2] === 'android') {
  20. dir = './platforms/android/assets/www';
  21. cmd = 'cordova emulate android';
  22. cmdPrepare = 'cordova prepare android';
  23. host = '10.0.2.2';
  24. if (process.argv[3]) {
  25. cmdPrepare += " --target=" + process.argv[3];
  26. }
  27. } else if (process.argv[2] === 'windows') {
  28. dir = './platforms/windows/www';
  29. cmd = 'cordova emulate windows';
  30. cmdPrepare = 'cordova prepare windows';
  31. host = '127.0.0.1';
  32. } else if (process.argv[2] === 'browser') {
  33. dir = './platforms/browser/www';
  34. cmd = 'cordova emulate browser';
  35. if (os.platform () === 'darwin')
  36. cmd += ' --target=Safari';
  37. cmdPrepare = 'cordova prepare browser';
  38. host = '127.0.0.1';
  39. }
  40. var serveMe = ServeMe ({
  41. // debug: true,
  42. // log: true,
  43. directory: dir,
  44. secure: false
  45. });
  46. function handleError (err, stdout, stderr) {
  47. if (err) {
  48. if (stdout) console.log (stdout);
  49. if (stderr) console.error (stderr);
  50. process.exit (1);
  51. }
  52. }
  53. fs.readFile (confFile, function (err, buf) {
  54. handleError (err, null, err);
  55. serveMe.start("0", function() {
  56. var port = serveMe.server.server.address().port;
  57. var configXml = buf.toString('utf-8').replace (/<content src="[^"]+/m, '<content src="http://' + host + ':' + port);
  58. // console.log (configXml);
  59. fs.writeFile (confFile, configXml, function (err) {
  60. handleError (err, null, err);
  61. console.log ('Changes applied to the', confFile);
  62. exec(cmd + " --list", {}, function callback(error, stdout, stderr){
  63. handleError (error, stdout, stderr);
  64. var targets = stdout.split (/\n/m).filter (function (t) {
  65. return t[0] === "\t";
  66. }).map (function (t) {
  67. return t.substr (1);
  68. });
  69. // console.log ('Existing targets:', targets);
  70. console.log ('Preferred target:', preferredTarget);
  71. if (preferredTarget && targets.length) {
  72. var tList = targets.filter (function (t) {return t.match (preferredTarget)});
  73. if (tList && tList.length) {
  74. cmd += ' --target="' + tList[0] + '"';
  75. console.log ('Found target %s', tList[0]);
  76. }
  77. }
  78. console.log ('Executing prepare %s', cmdPrepare);
  79. exec(cmdPrepare, {}, function callback(error, stdout, stderr){
  80. handleError (error, stdout, stderr);
  81. console.log ('Prepare completed');
  82. console.log ('Executing emulator %s', cmd);
  83. exec(cmd, {}, function callback(error, stdout, stderr){
  84. handleError (error, stdout, stderr);
  85. console.log ('Emulator running');
  86. setTimeout (function () {process.exit(1)}, 30000);
  87. });
  88. });
  89. });
  90. });
  91. });
  92. })
  93. serveMe.on("http_request", function(data){
  94. // console.log("http request", data);
  95. });
  96. serveMe.get ("/test/success", function(){
  97. console.log ("test success");
  98. process.exit ();
  99. });
  100. serveMe.get ("/test/fail", function (req, res){
  101. console.log ("test failures: ", req.url);
  102. process.exit (1);
  103. });