index.js 8.4 KB

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