version_generator.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 = '/packages-hot-update/manifest/';
  13. var src = './build/jsb-link/src';
  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. const fetch = require('node-fetch');
  87. const url1 = 'https://jenkins.black-art.cn/view/all/job/b22_client_android/lastBuild/buildNumber';
  88. const username = 'lanyunfei';
  89. const password = 'bend2690';
  90. const auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');
  91. fetch(url1, {
  92. method: 'GET',
  93. headers: {
  94. 'Authorization': auth
  95. }
  96. })
  97. .then(response => {
  98. if (!response.ok) {
  99. throw new Error('Network response was not ok ' + response.statusText);
  100. }
  101. return response.text();
  102. })
  103. .then(data => {
  104. console.log('Build Number:', data);
  105. let buildNumber = manifest.version == '0.0.0' ? '0' : process.env.BUILD_NUMBER
  106. manifest.version = '1.' + data + '.' + buildNumber
  107. // Iterate assets and src folder
  108. readDir(path.join(src, 'src'), manifest.assets);
  109. readDir(path.join(src, 'assets'), manifest.assets);
  110. var destManifest = path.join(dest, 'project.manifest');
  111. var destVersion = path.join(dest, 'version.manifest');
  112. mkdirSync(dest);
  113. fs.writeFile(destManifest, JSON.stringify(manifest), (err) => {
  114. if (err) throw err;
  115. console.log('Manifest successfully generated');
  116. });
  117. delete manifest.assets;
  118. delete manifest.searchPaths;
  119. fs.writeFile(destVersion, JSON.stringify(manifest), (err) => {
  120. if (err) throw err;
  121. console.log('Version successfully generated');
  122. });
  123. })
  124. .catch(error => {
  125. console.error('There was a problem with the fetch operation:', error);
  126. });