1
0

generator.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. var fs = require('fs');
  2. var path = require('path');
  3. var crypto = require('crypto');
  4. var serverRoot = 'https://pub.dwstatic.com/wxgame/allstar/';
  5. var manifest = {
  6. packageUrl: serverRoot,
  7. remoteManifestUrl: serverRoot + 'remote-assets/project.manifest',
  8. remoteVersionUrl: serverRoot + 'remote-assets/version.manifest',
  9. version: '1.0.0',
  10. assets: {},
  11. searchPaths: []
  12. };
  13. var dest = './remote-assets/';
  14. var src = './jsb/';
  15. // Parse arguments
  16. var i = 2;
  17. while ( i < process.argv.length) {
  18. var arg = process.argv[i];
  19. switch (arg) {
  20. case '--url' :
  21. case '-u' :
  22. var url = process.argv[i+1];
  23. manifest.packageUrl = url;
  24. manifest.remoteManifestUrl = url + 'project.manifest';
  25. manifest.remoteVersionUrl = url + 'version.manifest';
  26. i += 2;
  27. break;
  28. case '--version' :
  29. case '-v' :
  30. manifest.version = process.argv[i+1];
  31. i += 2;
  32. break;
  33. case '--src' :
  34. case '-s' :
  35. src = process.argv[i+1];
  36. i += 2;
  37. break;
  38. case '--dest' :
  39. case '-d' :
  40. dest = process.argv[i+1];
  41. i += 2;
  42. break;
  43. default :
  44. i++;
  45. break;
  46. }
  47. }
  48. function readDir (dir, obj) {
  49. var stat = fs.statSync(dir);
  50. if (!stat.isDirectory()) {
  51. return;
  52. }
  53. var subpaths = fs.readdirSync(dir), subpath, size, md5, compressed, relative;
  54. for (var i = 0; i < subpaths.length; ++i) {
  55. if (subpaths[i][0] === '.') {
  56. continue;
  57. }
  58. subpath = path.join(dir, subpaths[i]);
  59. stat = fs.statSync(subpath);
  60. if (stat.isDirectory()) {
  61. readDir(subpath, obj);
  62. }
  63. else if (stat.isFile()) {
  64. // Size in Bytes
  65. size = stat['size'];
  66. md5 = crypto.createHash('md5').update(fs.readFileSync(subpath, 'binary')).digest('hex');
  67. compressed = path.extname(subpath).toLowerCase() === '.zip';
  68. relative = path.relative(src, subpath);
  69. relative = relative.replace(/\\/g, '/');
  70. relative = encodeURI(relative);
  71. obj[relative] = {
  72. 'size' : size,
  73. 'md5' : md5
  74. };
  75. if (compressed) {
  76. obj[relative].compressed = true;
  77. }
  78. }
  79. }
  80. }
  81. var mkdirSync = function (path) {
  82. try {
  83. fs.mkdirSync(path);
  84. } catch(e) {
  85. if ( e.code != 'EEXIST' ) throw e;
  86. }
  87. }
  88. // Iterate res and src folder
  89. readDir(path.join(src, 'src'), manifest.assets);
  90. readDir(path.join(src, 'res'), manifest.assets);
  91. var destManifest = path.join(dest, 'project.manifest');
  92. var destVersion = path.join(dest, 'version.manifest');
  93. mkdirSync(dest);
  94. fs.writeFile(destManifest, JSON.stringify(manifest), (err) => {
  95. if (err) throw err;
  96. console.log('Manifest successfully generated');
  97. });
  98. delete manifest.assets;
  99. delete manifest.searchPaths;
  100. fs.writeFile(destVersion, JSON.stringify(manifest), (err) => {
  101. if (err) throw err;
  102. console.log('Version successfully generated');
  103. });