index.js 8.6 KB

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