index.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. let prompt = require('prompt');
  2. let shelljs = require('shelljs');
  3. let fs = require('fs-extra');
  4. let path = require('path');
  5. let basePath = '../../dist/preview release';
  6. // This can be changed when we have a new major release.
  7. let minimumDependency = '>=3.2.0-alpha';
  8. let packages = [
  9. {
  10. name: 'core',
  11. path: '../../'
  12. },
  13. {
  14. name: 'gui',
  15. path: basePath + '/gui/'
  16. },
  17. {
  18. name: 'materials',
  19. path: basePath + '/materialsLibrary/'
  20. },
  21. {
  22. name: 'postProcess',
  23. path: basePath + '/postProcessesLibrary/'
  24. },
  25. {
  26. name: 'gltf2interface',
  27. path: basePath + '/gltf2interface/'
  28. },
  29. {
  30. name: 'loaders',
  31. path: basePath + '/loaders/'
  32. },
  33. {
  34. name: 'serializers',
  35. path: basePath + '/serializers/'
  36. },
  37. {
  38. name: 'proceduralTextures',
  39. path: basePath + '/proceduralTexturesLibrary/'
  40. },
  41. {
  42. name: 'inspector',
  43. path: basePath + '/inspector/'
  44. },
  45. {
  46. name: 'viewer',
  47. path: basePath + '/../../Viewer/',
  48. required: [
  49. basePath + '/viewer/readme.md',
  50. basePath + '/viewer/package.json',
  51. ]
  52. },
  53. {
  54. name: 'viewer-assets',
  55. path: basePath + '/../../Viewer/dist/build/assets/',
  56. required: [
  57. basePath + '/../../Viewer/assets/readme.md',
  58. basePath + '/../../Viewer/assets/package.json',
  59. ]
  60. }
  61. ];
  62. function updateEngineVersion(newVersion) {
  63. console.log("updating version in babylon.engine.ts");
  64. let engineContent = fs.readFileSync("../../src/Engine/babylon.engine.ts").toString();
  65. let replaced = engineContent.replace(/(public static get Version\(\): string {\s*return ")(.*)(";\s*})/g, "$1" + newVersion + "$3");
  66. fs.writeFileSync("../../src/Engine/babylon.engine.ts", replaced);
  67. }
  68. function runGulp() {
  69. // run gulp typescript-all
  70. console.log("Running gulp compilation");
  71. let exec = shelljs.exec("gulp typescript-all --gulpfile ../Gulp/gulpfile.js");
  72. if (exec.code) {
  73. console.log("error during compilation, aborting");
  74. process.exit(1);
  75. }
  76. }
  77. function processPackages(version) {
  78. packages.forEach((package) => {
  79. if (package.name === "core") {
  80. processCore(package, version);
  81. } else if (package.name === "viewer") {
  82. processViewer(package, version);
  83. } else {
  84. if (package.required) {
  85. package.required.forEach(file => {
  86. fs.copySync(file, package.path + '/' + path.basename(file));
  87. });
  88. }
  89. let packageJson = require(package.path + 'package.json');
  90. packageJson.version = version;
  91. if (packageJson.dependencies) {
  92. Object.keys(packageJson.dependencies).forEach(key => {
  93. if (key.indexOf("babylonjs") !== -1) {
  94. packageJson.dependencies[key] = version;
  95. }
  96. });
  97. }
  98. if (packageJson.peerDependencies) packageJson.peerDependencies.babylonjs = minimumDependency;
  99. fs.writeFileSync(package.path + 'package.json', JSON.stringify(packageJson, null, 4));
  100. publish(version, package.name, package.path);
  101. }
  102. });
  103. }
  104. //check if logged in
  105. console.log("Using npm user:");
  106. let loginCheck = shelljs.exec('npm whoami');
  107. if (loginCheck.code === 0) {
  108. prompt.start();
  109. prompt.get(['version'], function (err, result) {
  110. let version = result.version;
  111. updateEngineVersion(version);
  112. if (process.argv.indexOf('--no-build') === -1) {
  113. runGulp();
  114. }
  115. processPackages(version);
  116. console.log("done, please tag git with " + version);
  117. });
  118. } else {
  119. console.log('not logged in.');
  120. }
  121. function processCore(package, version) {
  122. let packageJson = require(package.path + 'package.json');
  123. // make a temporary directory
  124. fs.ensureDirSync(basePath + '/package/');
  125. let files = [
  126. {
  127. path: basePath + "/babylon.d.ts",
  128. objectName: "babylon.d.ts"
  129. },
  130. {
  131. path: basePath + "/es6.js",
  132. objectName: "es6.js"
  133. },
  134. {
  135. path: basePath + "/babylon.js",
  136. objectName: "babylon.js"
  137. },
  138. {
  139. path: basePath + "/babylon.max.js",
  140. objectName: "babylon.max.js"
  141. },
  142. {
  143. path: basePath + "/babylon.worker.js",
  144. objectName: "babylon.worker.js"
  145. },
  146. {
  147. path: basePath + "/Oimo.js",
  148. objectName: "Oimo.js"
  149. },
  150. {
  151. path: package.path + "readme.md",
  152. objectName: "readme.md"
  153. }
  154. ];
  155. //copy them to the package path
  156. files.forEach(file => {
  157. fs.copySync(file.path, basePath + '/package/' + file.objectName);
  158. });
  159. // update package.json
  160. packageJson.version = version;
  161. console.log("generating file list");
  162. let packageFiles = ["package.json"];
  163. files.forEach(file => {
  164. if (!file.isDir) {
  165. packageFiles.push(file.objectName);
  166. } else {
  167. //todo is it better to read the content and add it? leave it like that ATM
  168. packageFiles.push(file.objectName + "/index.js", file.objectName + "/index.d.ts", file.objectName + "/es6.js")
  169. }
  170. });
  171. console.log("updating package.json");
  172. packageJson.files = packageFiles;
  173. packageJson.main = "babylon.js";
  174. packageJson.typings = "babylon.d.ts";
  175. fs.writeFileSync(basePath + '/package/' + 'package.json', JSON.stringify(packageJson, null, 4));
  176. publish(version, package.name, basePath + '/package/');
  177. // remove package directory
  178. fs.removeSync(basePath + '/package/');
  179. // now update the main package.json
  180. packageJson.files = packageJson.files.map(file => {
  181. if (file !== 'package.json' && file !== 'readme.md') {
  182. return 'dist/preview release/' + file;
  183. } else {
  184. return file;
  185. }
  186. });
  187. packageJson.main = "dist/preview release/babylon.js";
  188. packageJson.typings = "dist/preview release/babylon.d.ts";
  189. fs.writeFileSync(package.path + 'package.json', JSON.stringify(packageJson, null, 4));
  190. }
  191. function processViewer(package, version) {
  192. let buildPath = package.path + "dist/build/src";
  193. if (package.required) {
  194. package.required.forEach(file => {
  195. fs.copySync(file, buildPath + '/' + path.basename(file));
  196. });
  197. }
  198. // the viewer needs to be built using tsc on the viewer's main repository
  199. // build the viewer
  200. shelljs.exec('tsc -p ' + buildPath);
  201. let packageJson = require(buildPath + '/package.json');
  202. let files = getFiles(buildPath).map(f => f.replace(buildPath + "/", "")).filter(f => f.indexOf("assets/") === -1);
  203. packageJson.files = files;
  204. packageJson.version = version;
  205. packageJson.main = "index.js";
  206. packageJson.typings = "index.d.ts";
  207. fs.writeFileSync(buildPath + '/package.json', JSON.stringify(packageJson, null, 4));
  208. publish(version, package.name, buildPath);
  209. }
  210. function publish(version, packageName, basePath) {
  211. console.log('Publishing ' + packageName + " from " + basePath);
  212. let tagDef = "";
  213. // check for alpha or beta
  214. if (version.indexOf('alpha') !== -1 || version.indexOf('beta') !== -1) {
  215. tagDef = '--tag preview';
  216. }
  217. //publish the respected package
  218. console.log("executing " + 'npm publish \"' + basePath + "\"" + ' ' + tagDef);
  219. shelljs.exec('npm publish \"' + basePath + "\"" + ' ' + tagDef);
  220. }
  221. function getFiles(dir, files_) {
  222. files_ = files_ || [];
  223. var files = fs.readdirSync(dir);
  224. for (var i in files) {
  225. var name = dir + '/' + files[i];
  226. if (fs.statSync(name).isDirectory()) {
  227. getFiles(name, files_);
  228. } else {
  229. files_.push(name);
  230. }
  231. }
  232. return files_;
  233. }