version_generator.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. var fs = require('fs');
  2. var path = require('path');
  3. var crypto = require('crypto');
  4. var manifest = {
  5. packageUrl: 'http://localhost/tutorial-hot-update/remote-assets/',
  6. remoteManifestUrl: 'http://localhost/tutorial-hot-update/remote-assets/project.manifest',
  7. remoteVersionUrl: 'http://localhost/tutorial-hot-update/remote-assets/version.manifest',
  8. version: '1.0.0',
  9. assets: {},
  10. searchPaths: []
  11. };
  12. var dest = './remote-assets/';
  13. var src = './jsb/';
  14. // Parse arguments
  15. var i = 2;
  16. while (i < process.argv.length) {
  17. var arg = process.argv[i];
  18. switch (arg) {
  19. case '--url' :
  20. case '-u' :
  21. var url = process.argv[i + 1];
  22. manifest.packageUrl = url;
  23. manifest.remoteManifestUrl = url + 'project.manifest';
  24. manifest.remoteVersionUrl = url + 'version.manifest';
  25. i += 2;
  26. break;
  27. case '--version' :
  28. case '-v' :
  29. manifest.version = process.argv[i + 1];
  30. i += 2;
  31. break;
  32. case '--src' :
  33. case '-s' :
  34. src = process.argv[i + 1];
  35. i += 2;
  36. break;
  37. case '--dest' :
  38. case '-d' :
  39. dest = process.argv[i + 1];
  40. i += 2;
  41. break;
  42. default :
  43. i++;
  44. break;
  45. }
  46. }
  47. function readDir(dir, obj) {
  48. var stat = fs.statSync(dir);
  49. if (!stat.isDirectory()) {
  50. return;
  51. }
  52. var subpaths = fs.readdirSync(dir), subpath, size, md5, compressed, relative;
  53. for (var i = 0; i < subpaths.length; ++i) {
  54. if (subpaths[i][0] === '.') {
  55. continue;
  56. }
  57. subpath = path.join(dir, subpaths[i]);
  58. stat = fs.statSync(subpath);
  59. if (stat.isDirectory()) {
  60. readDir(subpath, obj);
  61. } else if (stat.isFile()) {
  62. // Size in Bytes
  63. size = stat['size'];
  64. md5 = crypto.createHash('md5').update(fs.readFileSync(subpath)).digest('hex');
  65. compressed = path.extname(subpath).toLowerCase() === '.zip';
  66. relative = path.relative(src, subpath);
  67. relative = relative.replace(/\\/g, '/');
  68. relative = encodeURI(relative);
  69. obj[relative] = {
  70. 'size': size,
  71. 'md5': md5
  72. };
  73. if (compressed) {
  74. obj[relative].compressed = true;
  75. }
  76. }
  77. }
  78. }
  79. var mkdirSync = function (path) {
  80. try {
  81. fs.mkdirSync(path);
  82. } catch (e) {
  83. if (e.code != 'EEXIST') throw e;
  84. }
  85. }
  86. var http = require('http');
  87. //http://192.168.0.190:8080/view/all/job/android/lastBuild/buildNumber
  88. var options = {
  89. hostname: '192.168.0.190',
  90. port: 8080,
  91. path: '/view/all/job/android/lastBuild/buildNumber',
  92. method: 'GET',
  93. headers: {
  94. 'Content-Type': 'text/plain;charset=US-ASCII'
  95. }
  96. };
  97. var type = ['chinese', 'math', 'pinyin','poetry']
  98. var replaceTypeFuc = function () {
  99. type.forEach(value => {
  100. if (manifest.remoteManifestUrl.includes(value)) {
  101. options.path = `/view/all/job/android_${value}/lastBuild/buildNumber`
  102. }
  103. })
  104. }
  105. replaceTypeFuc()
  106. var req = http.get(options, function (res) {
  107. console.log('STATUS: ' + res.statusCode);
  108. console.log('HEADERS: ' + JSON.stringify(res.headers));
  109. res.setEncoding('utf8');
  110. res.on('data', function (chunk) {
  111. console.log('BODY: ' + chunk);
  112. let buildNumber = manifest.version == '0.0.0' ? '0' : process.env.BUILD_NUMBER
  113. manifest.version = '1.' + chunk + '.' + buildNumber
  114. // Iterate assets and src folder
  115. readDir(path.join(src, 'src'), manifest.assets);
  116. readDir(path.join(src, 'assets'), manifest.assets);
  117. var destManifest = path.join(dest, 'project.manifest');
  118. var destVersion = path.join(dest, 'version.manifest');
  119. mkdirSync(dest);
  120. fs.writeFile(destManifest, JSON.stringify(manifest), (err) => {
  121. if (err) throw err;
  122. console.log('Manifest successfully generated');
  123. });
  124. delete manifest.assets;
  125. delete manifest.searchPaths;
  126. fs.writeFile(destVersion, JSON.stringify(manifest), (err) => {
  127. if (err) throw err;
  128. console.log('Version successfully generated');
  129. });
  130. });
  131. });
  132. req.on('error', function (e) {
  133. console.log('problem with request: ' + e.message);
  134. });
  135. req.end();