gulp-processTypescriptDeclaration.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Gulp Tools
  2. var fs = require("fs");
  3. var processData = function(data, options) {
  4. var str = "" + data;
  5. // Start process
  6. let lines = str.split('\n');
  7. var firstIndex = lines.findIndex((line => { return line.indexOf(`'${options.packageName}/'`) !== -1 }));
  8. var lastIndex = lines.findIndex(((line, idx) => { return line.trim() === '}' && idx > firstIndex }));
  9. lines.splice(firstIndex, lastIndex - firstIndex + 1);
  10. // Let's go line by line and check if we have special folder replacements
  11. for (var index = 0; index < lines.length; index++) {
  12. var namespace = options.moduleName;
  13. var regex = /declare module '(.*)' {/g;
  14. if (options.moduleSpecifics) {
  15. var match = regex.exec(lines[index]);
  16. if (!match) {
  17. continue;
  18. }
  19. var module = match[1];
  20. options.moduleSpecifics.forEach(function(specific) {
  21. if (module.indexOf(specific.path) > -1) {
  22. namespace = specific.namespace;
  23. }
  24. });
  25. }
  26. lines[index] = lines[index].replace(regex, `declare module ${namespace} {`);
  27. }
  28. str = lines.join('\n');
  29. str = str.replace("import * as BABYLON from 'babylonjs';", "");
  30. let regexp = new RegExp(`import {(.*)} from ['"]${options.packageName}(.*)['"];`, 'g');
  31. str = str.replace(regexp, '');
  32. if (options.importsToRemove) {
  33. while (options.importsToRemove.length) {
  34. let remove = options.importsToRemove.pop();
  35. str = str.replace(new RegExp(`import '${remove}';`), '');
  36. }
  37. }
  38. // Find all used BABYLON and BABYLON-Loaders classes:
  39. if ((options.classMap)) {
  40. Object.keys(options.classMap).forEach(package => {
  41. var babylonRegex = new RegExp(`import {(.*)} from ['"](${package})['"];`, "g");
  42. var match = babylonRegex.exec(str);
  43. let classes = new Set();
  44. while (match != null) {
  45. if (match[1]) {
  46. match[1].split(",").forEach(element => {
  47. classes.add(element.trim());
  48. });
  49. }
  50. match = babylonRegex.exec(str);
  51. }
  52. str = str.replace(babylonRegex, '');
  53. classes.forEach(cls => {
  54. let rg = new RegExp(`([ <])(${cls})([^\\w])`, "g")
  55. str = str.replace(rg, `$1${options.classMap[package]}.$2$3`);
  56. });
  57. })
  58. }
  59. str = str.replace(/export {(.*)};/g, '');
  60. str = str.replace(/import (.*);/g, "");
  61. str = str.split("\n").filter(line => line.trim()).filter(line => line.indexOf("export * from") === -1).join("\n");
  62. // Remove empty module declaration
  63. var cleanEmptyNamespace = function(str, moduleName) {
  64. let emptyDeclareRegexp = new RegExp("declare module " + moduleName + " {\n}\n", "g");
  65. str = str.replace(emptyDeclareRegexp, "");
  66. emptyDeclareRegexp = new RegExp("declare module " + moduleName + " {\r\n}\r\n", "g");
  67. str = str.replace(emptyDeclareRegexp, "");
  68. return str;
  69. }
  70. str = cleanEmptyNamespace(str, options.moduleName);
  71. if (options.moduleSpecifics) {
  72. options.moduleSpecifics.forEach(function(specific) {
  73. str = cleanEmptyNamespace(str, specific.namespace);
  74. });
  75. }
  76. return str;
  77. }
  78. module.exports = function(fileLocation, options, cb) {
  79. options = options || { };
  80. fs.readFile(fileLocation, function(err, data) {
  81. if (err) throw err;
  82. data += "";
  83. // For Raanan, litteral import hack TO BETTER INTEGRATE
  84. data = data.replace('import "../sass/main.scss";', "");
  85. if (options.prependText) {
  86. data = options.prependText + '\n' + data.toString();
  87. }
  88. var newData = "";
  89. if (options) {
  90. newData = processData(data, options);
  91. fs.writeFileSync(fileLocation.replace('.module', ''), newData);
  92. }
  93. if (options.doNotAppendNamespace) {
  94. fs.writeFileSync(fileLocation, data);
  95. }
  96. else {
  97. fs.writeFileSync(fileLocation, data + "\n" + newData);
  98. }
  99. cb && cb();
  100. });
  101. }