publisher.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. // Dependecies.
  2. const prompt = require('prompt');
  3. const shelljs = require('shelljs');
  4. const fs = require('fs-extra');
  5. const path = require('path');
  6. const rmDir = require("../NodeHelpers/rmDir");
  7. const colorConsole = require("../NodeHelpers/colorConsole");
  8. // CMD Arguments Management.
  9. let doNotBuild = false;
  10. let doNotPublish = false;
  11. // Pathe management.
  12. process.env.PATH += (path.delimiter + path.join(__dirname, 'node_modules', '.bin'));
  13. // Global Variables.
  14. const config = require("../gulp/config.json");
  15. const modules = config.modules.concat(config.viewerModules);
  16. const basePath = config.build.outputDirectory;
  17. const tempPath = config.build.tempDirectory + "packageES6/";
  18. const coreSrc = config.core.build.srcDirectory;
  19. const enginePath = coreSrc + "Engines/engine.ts";
  20. /**
  21. * Get Files from folder.
  22. */
  23. const getFiles = function(dir, files_) {
  24. files_ = files_ || [];
  25. var files = fs.readdirSync(dir);
  26. for (var i in files) {
  27. var name = dir + '/' + files[i];
  28. if (fs.statSync(name).isDirectory()) {
  29. getFiles(name, files_);
  30. } else {
  31. files_.push(name);
  32. }
  33. }
  34. return files_;
  35. }
  36. /**
  37. * Update the version in the engine class for Babylon
  38. */
  39. function updateEngineVersion(newVersion) {
  40. colorConsole.log("Updating version in engine.ts to: " + newVersion.green);
  41. let engineContent = fs.readFileSync(enginePath).toString();
  42. let replaced = engineContent.replace(/(public static get Version\(\): string {\s*return ")(.*)(";\s*})/g, "$1" + newVersion + "$3");
  43. fs.writeFileSync(enginePath, replaced);
  44. colorConsole.emptyLine();
  45. }
  46. /**
  47. * Get the version from the engine class for Babylon
  48. */
  49. function getEngineVersion() {
  50. colorConsole.log("Get version from engine.ts");
  51. const engineContent = fs.readFileSync(enginePath).toString();
  52. const versionRegex = new RegExp(`public static get Version\\(\\): string {[\\s\\S]*return "([\\s\\S]*?)";[\\s\\S]*}`, "gm");
  53. const match = versionRegex.exec(engineContent);
  54. if (match && match.length) {
  55. const version = match[1];
  56. colorConsole.log("Version found: " + version.green);
  57. colorConsole.emptyLine();
  58. return version;
  59. }
  60. colorConsole.error("Version not found in engine.ts");
  61. process.exit(1);
  62. }
  63. /**
  64. * Publish a package to npm.
  65. */
  66. function publish(version, packageName, basePath, public) {
  67. colorConsole.log(' Publishing ' + packageName.blue.bold + " from " + basePath.cyan);
  68. let tag = "";
  69. // check for alpha or beta
  70. if (version.indexOf('alpha') !== -1 || version.indexOf('beta') !== -1) {
  71. tag = ' --tag preview';
  72. }
  73. //publish the respected package
  74. var cmd = 'npm publish \"' + basePath + "\"" + tag;
  75. if (public) {
  76. cmd += " --access public";
  77. }
  78. if (doNotPublish) {
  79. colorConsole.log(" If publishing enabled: " + cmd.yellow);
  80. }
  81. else {
  82. colorConsole.log(" Executing: " + cmd.yellow);
  83. shelljs.exec(cmd);
  84. }
  85. colorConsole.success(' Publishing ' + "OK".green);
  86. }
  87. /**
  88. * Build the folder with Gulp.
  89. */
  90. function buildBabylonJSAndDependencies() {
  91. colorConsole.log("Running gulp compilation");
  92. let exec = shelljs.exec("gulp typescript-libraries --gulpfile ../Gulp/gulpfile.js");
  93. if (exec.code) {
  94. colorConsole.error("Error during compilation, aborting");
  95. process.exit(1);
  96. }
  97. }
  98. /**
  99. * Process ES6 Packages.
  100. */
  101. function processEs6Packages(version) {
  102. modules.forEach(moduleName => {
  103. let module = config[moduleName];
  104. let es6Config = module.build.es6;
  105. if (!es6Config) {
  106. return;
  107. }
  108. colorConsole.log("Process " + "ES6".magenta + " Package: " + moduleName.blue.bold);
  109. let projectPath = es6Config.tsFolder;
  110. let buildPath = path.normalize(tempPath + moduleName);
  111. let legacyPackageJson = require(module.build.packageJSON || basePath + module.build.distOutputDirectory + 'package.json');
  112. colorConsole.log(" Cleanup " + buildPath.cyan);
  113. rmDir(buildPath);
  114. let command = 'tsc --inlineSources -t es5 -m esNext -p ' + projectPath + ' --outDir ' + buildPath;
  115. colorConsole.log(" Executing " + command.yellow);
  116. let tscCompile = shelljs.exec(command);
  117. if (tscCompile.code !== 0) {
  118. throw new Error("Tsc compilation failed");
  119. }
  120. if (module.build.requiredFiles) {
  121. module.build.requiredFiles.forEach(file => {
  122. colorConsole.log(" Copy required file: ", file.cyan, (buildPath + '/' + path.basename(file)).cyan);
  123. fs.copySync(file, buildPath + '/' + path.basename(file));
  124. });
  125. }
  126. if (es6Config.requiredFiles) {
  127. es6Config.requiredFiles.forEach(file => {
  128. colorConsole.log(" Copy es6 required file: ", file.cyan, (buildPath + '/' + path.basename(file)).cyan);
  129. fs.copySync(file, buildPath + '/' + path.basename(file));
  130. });
  131. }
  132. let files = getFiles(buildPath).map(f => f.replace(buildPath + "/", "")).filter(f => f.indexOf("assets/") === -1);
  133. legacyPackageJson.name = es6Config.packageName;
  134. legacyPackageJson.version = version;
  135. legacyPackageJson.main = "index.js";
  136. legacyPackageJson.module = "index.js";
  137. legacyPackageJson.esnext = "index.js";
  138. legacyPackageJson.typings = "index.d.ts";
  139. legacyPackageJson.files = files;
  140. ["dependencies", "peerDependencies", "devDependencies"].forEach(key => {
  141. if (legacyPackageJson[key]) {
  142. let dependencies = legacyPackageJson[key];
  143. legacyPackageJson[key] = {};
  144. Object.keys(dependencies).forEach(packageName => {
  145. if (packageName.indexOf("babylonjs") !== -1) {
  146. colorConsole.log(" Checking Internal Dependency: " + packageName.cyan);
  147. let dependencyName = packageName;
  148. for (var moduleName of modules) {
  149. if (config[moduleName] && config[moduleName].build.processDeclaration && config[moduleName].build.processDeclaration.packageName === packageName) {
  150. if (config[moduleName].build.es6) {
  151. dependencyName = config[moduleName].build.es6.packageName;
  152. colorConsole.log(" Replace Dependency: " + packageName.cyan + " by " + dependencyName.cyan);
  153. break;
  154. }
  155. }
  156. }
  157. legacyPackageJson[key][dependencyName] = version;
  158. } else if (!module.isCore) {
  159. legacyPackageJson[key][packageName] = dependencies[packageName];
  160. }
  161. });
  162. }
  163. });
  164. fs.writeFileSync(buildPath + '/package.json', JSON.stringify(legacyPackageJson, null, 4));
  165. // Do not publish yet.
  166. // publish(version, es6Config.packageName, buildPath, true);
  167. colorConsole.emptyLine();
  168. });
  169. }
  170. /**
  171. * Process Legacy Packages.
  172. */
  173. function processLegacyPackages(version) {
  174. modules.forEach(moduleName => {
  175. let module = config[moduleName];
  176. colorConsole.log("Process " + "UMD".magenta + " Package: " + moduleName.blue.bold);
  177. if (moduleName === "core") {
  178. processLegacyCore(version);
  179. }
  180. else if (moduleName === "viewer") {
  181. processLegacyViewer(module, version);
  182. }
  183. else {
  184. let outputDirectory = module.build.legacyPackageOutputDirectory || basePath + module.build.distOutputDirectory;
  185. if (module.build.requiredFiles) {
  186. module.build.requiredFiles.forEach(file => {
  187. colorConsole.log(" Copy required file: ", file.cyan, (outputDirectory + '/' + path.basename(file)).cyan);
  188. fs.copySync(file, outputDirectory + '/' + path.basename(file));
  189. });
  190. }
  191. let packageJson = require(outputDirectory + 'package.json');
  192. packageJson.version = version;
  193. colorConsole.log(" Update package version to: " + version.green);
  194. if (packageJson.dependencies) {
  195. Object.keys(packageJson.dependencies).forEach(key => {
  196. if (key.indexOf("babylonjs") !== -1) {
  197. packageJson.dependencies[key] = version;
  198. }
  199. });
  200. }
  201. fs.writeFileSync(outputDirectory + 'package.json', JSON.stringify(packageJson, null, 4));
  202. publish(version, moduleName, outputDirectory);
  203. colorConsole.emptyLine();
  204. }
  205. });
  206. }
  207. /**
  208. * Special treatment for legacy viewer.
  209. */
  210. function processLegacyViewer(module, version) {
  211. let projectPath = '../../Viewer';
  212. let buildPath = projectPath + "/build/src/";
  213. if (module.build.requiredFiles) {
  214. module.build.requiredFiles.forEach(file => {
  215. colorConsole.log(" Copy required file: ", file.cyan, (buildPath + path.basename(file)).cyan);
  216. fs.copySync(file, buildPath + path.basename(file));
  217. });
  218. }
  219. // The viewer needs to be built using tsc on the viewer's main repository
  220. // build the viewer.
  221. colorConsole.log(" Executing " + ('tsc -p ' + projectPath).yellow);
  222. let tscCompile = shelljs.exec('tsc -p ' + projectPath);
  223. if (tscCompile.code !== 0) {
  224. throw new Error("tsc compilation failed");
  225. }
  226. let packageJson = require(buildPath + '/package.json');
  227. let files = getFiles(buildPath).map(f => f.replace(buildPath + "/", "")).filter(f => f.indexOf("assets/") === -1);
  228. packageJson.files = files;
  229. packageJson.version = version;
  230. packageJson.module = "index.js";
  231. packageJson.main = "babylon.viewer.js";
  232. packageJson.typings = "index.d.ts";
  233. fs.writeFileSync(buildPath + '/package.json', JSON.stringify(packageJson, null, 4));
  234. publish(version, "viewer", buildPath);
  235. colorConsole.emptyLine();
  236. }
  237. /**
  238. * Special treatment for legacy core.
  239. */
  240. function processLegacyCore(version) {
  241. let package = {
  242. "name": "core",
  243. "path": "/../../"
  244. };
  245. let packageJson = require('../../package.json');
  246. // make a temporary directory
  247. fs.ensureDirSync(basePath + '/package/');
  248. let files = [
  249. {
  250. path: basePath + "/babylon.d.ts",
  251. objectName: "babylon.d.ts"
  252. },
  253. {
  254. path: basePath + "/babylon.js",
  255. objectName: "babylon.js"
  256. },
  257. {
  258. path: basePath + "/babylon.max.js",
  259. objectName: "babylon.max.js"
  260. },
  261. {
  262. path: basePath + "/babylon.max.js.map",
  263. objectName: "babylon.max.js.map"
  264. },
  265. {
  266. path: basePath + "/Oimo.js",
  267. objectName: "Oimo.js"
  268. },
  269. {
  270. path: basePath + package.path + "readme.md",
  271. objectName: "readme.md"
  272. }
  273. ];
  274. //copy them to the package path
  275. files.forEach(file => {
  276. fs.copySync(file.path, basePath + '/package/' + file.objectName);
  277. });
  278. // update package.json
  279. packageJson.version = version;
  280. colorConsole.log(" Generating file list");
  281. let packageFiles = ["package.json"];
  282. files.forEach(file => {
  283. if (!file.isDir) {
  284. packageFiles.push(file.objectName);
  285. } else {
  286. //todo is it better to read the content and add it? leave it like that ATM
  287. packageFiles.push(file.objectName + "/index.js", file.objectName + "/index.d.ts", file.objectName + "/es6.js")
  288. }
  289. });
  290. colorConsole.log(" Updating package.json");
  291. packageJson.files = packageFiles;
  292. packageJson.main = "babylon.js";
  293. packageJson.typings = "babylon.d.ts";
  294. fs.writeFileSync(basePath + '/package/' + 'package.json', JSON.stringify(packageJson, null, 4));
  295. publish(version, package.name, basePath + '/package/');
  296. // remove package directory
  297. fs.removeSync(basePath + '/package/');
  298. // now update the main package.json
  299. packageJson.files = packageJson.files.map(file => {
  300. if (file !== 'package.json' && file !== 'readme.md') {
  301. return 'dist/preview release/' + file;
  302. } else {
  303. return file;
  304. }
  305. });
  306. packageJson.main = "dist/preview release/babylon.js";
  307. packageJson.typings = "dist/preview release/babylon.d.ts";
  308. fs.writeFileSync('../../package.json', JSON.stringify(packageJson, null, 4));
  309. colorConsole.emptyLine();
  310. }
  311. const createVersion = function(version) {
  312. // Prevent to build for test Cases.
  313. if (!doNotBuild) {
  314. buildBabylonJSAndDependencies();
  315. }
  316. // Create the packages and publish if needed.
  317. processLegacyPackages(version);
  318. // Do not publish es6 yet.
  319. doNotPublish = true;
  320. processEs6Packages(version);
  321. }
  322. /**
  323. * Main function driving the publication.
  324. */
  325. module.exports = function(noBuild, noPublish, askVersion) {
  326. doNotBuild = noBuild;
  327. doNotPublish = noPublish;
  328. if (askVersion) {
  329. prompt.start();
  330. prompt.get(['version'], function (err, result) {
  331. const version = result.version;
  332. // Update the engine version if needed.
  333. if (!version || !version.length) {
  334. colorConsole.error("New version required.");
  335. Process.exit(1);
  336. return;
  337. }
  338. updateEngineVersion(version);
  339. createVersion(version);
  340. // Invite user to tag with the new version.
  341. if (newVersion) {
  342. colorConsole.log("Done, please tag git with " + version);
  343. }
  344. });
  345. }
  346. else {
  347. const version = getEngineVersion();
  348. createVersion(version);
  349. }
  350. };