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. // Path management.
  12. process.env.PATH += (path.delimiter + path.join(__dirname, 'node_modules', '.bin'));
  13. // Global Variables.
  14. const config = require("../Config/config.js");
  15. const modules = config.modules.concat(config.viewerModules);
  16. const basePath = config.build.outputDirectory;
  17. const enginePath = path.join(config.core.computed.srcDirectory, "Engines/engine.ts");
  18. /**
  19. * Get Files from folder.
  20. */
  21. const getFiles = function(dir, files_) {
  22. files_ = files_ || [];
  23. var files = fs.readdirSync(dir);
  24. for (var i in files) {
  25. var name = dir + '/' + files[i];
  26. if (fs.statSync(name).isDirectory()) {
  27. getFiles(name, files_);
  28. } else {
  29. files_.push(name);
  30. }
  31. }
  32. return files_;
  33. }
  34. /**
  35. * Update the version in the engine class for Babylon
  36. */
  37. function updateEngineVersion(newVersion) {
  38. colorConsole.log("Updating version in engine.ts to: " + newVersion.green);
  39. let engineContent = fs.readFileSync(enginePath).toString();
  40. let replaced = engineContent.replace(/(public static get Version\(\): string {\s*return ")(.*)(";\s*})/g, "$1" + newVersion + "$3");
  41. fs.writeFileSync(enginePath, replaced);
  42. colorConsole.emptyLine();
  43. }
  44. /**
  45. * Get the version from the engine class for Babylon
  46. */
  47. function getEngineVersion() {
  48. colorConsole.log("Get version from engine.ts");
  49. const engineContent = fs.readFileSync(enginePath).toString();
  50. const versionRegex = new RegExp(`public static get Version\\(\\): string {[\\s\\S]*return "([\\s\\S]*?)";[\\s\\S]*}`, "gm");
  51. const match = versionRegex.exec(engineContent);
  52. if (match && match.length) {
  53. const version = match[1];
  54. colorConsole.log("Version found: " + version.green);
  55. colorConsole.emptyLine();
  56. return version;
  57. }
  58. colorConsole.error("Version not found in engine.ts");
  59. process.exit(1);
  60. }
  61. /**
  62. * Publish a package to npm.
  63. */
  64. function publish(version, packageName, publishPath, public) {
  65. colorConsole.log(' Publishing ' + packageName.blue.bold + " from " + publishPath.cyan);
  66. let tag = "";
  67. // check for alpha or beta
  68. if (version.indexOf('alpha') !== -1 || version.indexOf('beta') !== -1) {
  69. tag = ' --tag preview';
  70. }
  71. //publish the respected package
  72. var cmd = 'npm publish "' + publishPath + '"' + tag;
  73. if (public) {
  74. cmd += " --access public";
  75. }
  76. if (doNotPublish) {
  77. colorConsole.log(" If publishing enabled: " + cmd.yellow);
  78. }
  79. else {
  80. colorConsole.log(" Executing: " + cmd.yellow);
  81. shelljs.exec(cmd);
  82. }
  83. colorConsole.success(' Publishing ' + "OK".green);
  84. }
  85. /**
  86. * Build the folder with Gulp.
  87. */
  88. function buildBabylonJSAndDependencies() {
  89. colorConsole.log("Running gulp compilation");
  90. let exec = shelljs.exec("gulp typescript-libraries --gulpfile ../Gulp/gulpfile.js");
  91. if (exec.code) {
  92. colorConsole.error("Error during compilation, aborting");
  93. process.exit(1);
  94. }
  95. }
  96. /**
  97. * Process ES6 Packages.
  98. */
  99. function processEs6Packages(version) {
  100. config.modulesES6.forEach(moduleName => {
  101. let module = config[moduleName];
  102. let es6Config = module.build.es6;
  103. colorConsole.log("Process " + "ES6".magenta + " Package: " + moduleName.blue.bold);
  104. let distPath = module.computed.distES6Directory;
  105. let packagePath = module.computed.packageES6Directory;
  106. let legacyPackageJson = require(module.computed.packageJSONPath);
  107. colorConsole.log(" Cleanup " + packagePath.cyan);
  108. rmDir(packagePath);
  109. colorConsole.log(" Copy Dist folder " + distPath.cyan + " to " + packagePath.cyan);
  110. fs.copySync(distPath, packagePath);
  111. if (module.build.requiredFiles) {
  112. module.build.requiredFiles.forEach(file => {
  113. let destination = path.join(packagePath, path.basename(file));
  114. colorConsole.log(" Copy required file: ", file.cyan, destination.cyan);
  115. fs.copySync(file, destination);
  116. });
  117. }
  118. if (es6Config.requiredFiles) {
  119. es6Config.requiredFiles.forEach(file => {
  120. let destination = path.join(packagePath, path.basename(file));
  121. colorConsole.log(" Copy es6 required file: ", file.cyan, destination.cyan);
  122. fs.copySync(file, destination);
  123. });
  124. }
  125. let files = getFiles(packagePath)
  126. .map(f => f.replace(packagePath + "/", ""))
  127. .filter(f => f.indexOf("assets/") === -1);
  128. legacyPackageJson.name = es6Config.packageName;
  129. legacyPackageJson.version = version;
  130. legacyPackageJson.main = "index.js";
  131. legacyPackageJson.module = "index.js";
  132. legacyPackageJson.esnext = "index.js";
  133. legacyPackageJson.typings = "index.d.ts";
  134. legacyPackageJson.files = files;
  135. ["dependencies", "peerDependencies", "devDependencies"].forEach(key => {
  136. if (legacyPackageJson[key]) {
  137. let dependencies = legacyPackageJson[key];
  138. legacyPackageJson[key] = {};
  139. Object.keys(dependencies).forEach(packageName => {
  140. if (packageName.indexOf("babylonjs") !== -1) {
  141. colorConsole.log(" Checking Internal Dependency: " + packageName.cyan);
  142. let dependencyName = packageName;
  143. for (var moduleName of modules) {
  144. if (config[moduleName] && config[moduleName].build.umd && config[moduleName].build.umd.packageName === packageName) {
  145. if (config[moduleName].build.es6) {
  146. dependencyName = config[moduleName].build.es6.packageName;
  147. colorConsole.log(" Replace Dependency: " + packageName.cyan + " by " + dependencyName.cyan);
  148. break;
  149. }
  150. }
  151. }
  152. legacyPackageJson[key][dependencyName] = version;
  153. } else if (!module.isCore) {
  154. legacyPackageJson[key][packageName] = dependencies[packageName];
  155. }
  156. });
  157. }
  158. });
  159. // Inject tslib as a dependency
  160. var mainPackageJSONPath = path.join(config.computed.rootFolder, "package.json");
  161. var mainPackageJSON = require(mainPackageJSONPath);
  162. legacyPackageJson["dependencies"]["tslib"] = mainPackageJSON["dependencies"]["tslib"];
  163. let packageJSONPath = path.join(packagePath, "package.json");
  164. fs.writeFileSync(packageJSONPath, JSON.stringify(legacyPackageJson, null, 4));
  165. // Do not publish yet.
  166. // publish(version, es6Config.packageName, packagePath, 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 || module.computed.distDirectory;
  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. };