index.js 8.3 KB

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