e2e-publish.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. module.exports = function(options) {
  2. var fs = require('fs');
  3. var path = require('path');
  4. var request = require('request');
  5. var inputDir = path.join(__dirname, '..', '..' , 'dist', 'e2e', 'tests');
  6. var uploadQueue = [];
  7. var ignoreFiles = /(\/test\/|\/ts\/|\/q\/|\/ionic-site\/|\/docs\/|\/examples\/|\/inquirer\/|\/lodash\/|\/tooling\/|\/colors\/|\/bin\/|\.ts$|\.bin|\.map$|\.md$|\.git|\.scss$|\.yml$|\.yaml$|\.dart$|\.txt|\.npm|bower|DS_Store|LICENSE)/i;
  8. function uploadFiles(dir, urlPath) {
  9. fs.readdir(dir, function(err, list) {
  10. list.forEach(function(file) {
  11. var url = path.join(urlPath, file);
  12. fs.stat(path.join(dir, file), function(err, stat) {
  13. if (stat && stat.isDirectory()) {
  14. uploadFiles(path.join(dir, file), path.join(urlPath, file);
  15. } else {
  16. if ( shouldProcessPath (url) ){
  17. uploadFile(url, path.join(dir, file));
  18. }
  19. }
  20. });
  21. });
  22. });
  23. setTimeout(postNextUpload, 100);
  24. }
  25. function uploadFile(archiveFileUrlPath, archiveFileLocalPath) {
  26. uploadQueue.push({
  27. url_path: archiveFileUrlPath,
  28. local_path: archiveFileLocalPath,
  29. group_id: options.groupId,
  30. app_id: options.appId,
  31. test_id: options.testId,
  32. access_key: process.env.IONIC_SNAPSHOT_KEY
  33. });
  34. }
  35. function postNextUpload() {
  36. var uploadData = null;
  37. var totalUploading = 0;
  38. for (var i = 0; i < uploadQueue.length; i++) {
  39. if (uploadQueue[i].status === 'uploaded') {
  40. continue;
  41. } else if (uploadQueue[i].status === 'uploading') {
  42. totalUploading++;
  43. continue;
  44. } else {
  45. uploadData = uploadQueue[i];
  46. }
  47. }
  48. if (!uploadData || totalUploading > 20) {
  49. return;
  50. } else if (options.verbose) {
  51. console.log('Uploading: ' + uploadData.url_path);
  52. }
  53. uploadData.status = 'uploading';
  54. request.post({
  55. uri: 'http://' + options.domain + '/e2e/upload-url',
  56. formData: uploadData
  57. },
  58. function(err, httpResponse, body) {
  59. if (err) {
  60. uploadData.status = 'failed';
  61. console.error('Get upload failed:', uploadData.url_path, err);
  62. } else {
  63. if (httpResponse.statusCode == 200) {
  64. uploadE2E(body, uploadData);
  65. } else {
  66. console.error('Get upload error:', httpResponse.statusCode, body);
  67. }
  68. }
  69. }
  70. );
  71. }
  72. function uploadE2E(uploadUrl, uploadData) {
  73. var formData = {
  74. file: fs.createReadStream(uploadData.local_path)
  75. };
  76. request.post({
  77. uri: uploadUrl,
  78. formData: formData
  79. },
  80. function(err, httpResponse, body) {
  81. setTimeout(postNextUpload, 100);
  82. if (err) {
  83. console.error('Upload failed:', uploadUrl, err);
  84. uploadData.status = 'failed';
  85. } else {
  86. if (httpResponse.statusCode == 200) {
  87. uploadData.status = 'uploaded';
  88. if (options.verbose) {
  89. console.error('Uploaded:', uploadData.url_path);
  90. }
  91. } else {
  92. console.error('Upload error:', httpResponse.statusCode, body);
  93. uploadData.status = 'failed';
  94. }
  95. }
  96. }
  97. );
  98. }
  99. function shouldProcessPath(urlPath) {
  100. if ( urlPath && urlPath.length > 0 ) {
  101. var cleanedUpString = urlPath.substring(1);
  102. var tokens = cleanedUpString.split('/');
  103. // {component}/test/{testName}/{file}
  104. var extension = path.extname(cleanedUpString);
  105. return tokens && tokens.length > 3 && tokens[1] === 'test' && ( extension === '.html' || extension === '.js' );
  106. }
  107. return false;
  108. }
  109. console.log('Uploading e2e tests:', options.testId);
  110. uploadFiles(inputDir, '');
  111. };