gulp-processTypescriptDeclaration.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Gulp Tools
  2. var fs = require("fs");
  3. var processData = function(data, options) {
  4. var str = "" + data;
  5. // Start process by extracting all lines.
  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. // Replaces declare module 'babylonjs'; by declare module BABYLON for instance
  12. for (var index = 0; index < lines.length; index++) {
  13. var namespace = options.moduleName;
  14. var regex = /declare module '(.*)' {/g;
  15. if (options.moduleSpecifics) {
  16. var match = regex.exec(lines[index]);
  17. if (!match) {
  18. continue;
  19. }
  20. var module = match[1];
  21. options.moduleSpecifics.forEach(function(specific) {
  22. if (module.indexOf(specific.path) > -1) {
  23. namespace = specific.namespace;
  24. }
  25. });
  26. }
  27. lines[index] = lines[index].replace(regex, `declare module ${namespace} {`);
  28. }
  29. // Recreate the file.
  30. str = lines.join('\n');
  31. // Let s clean up all the import * from BABYLON or the package itself as we know it is part of
  32. // the same namespace... Should be
  33. str = str.replace("import * as BABYLON from 'babylonjs';", "");
  34. let regexp = new RegExp(`import {(.*)} from ['"]${options.packageName}(.*)['"];`, 'g');
  35. str = str.replace(regexp, '');
  36. // Let s clean other chosen imports from the mix.
  37. if (options.importsToRemove) {
  38. while (options.importsToRemove.length) {
  39. let remove = options.importsToRemove.pop();
  40. str = str.replace(new RegExp(`import '${remove}';`), '');
  41. }
  42. }
  43. // Find all other imported classes (Part of BABYLON or Loaders for instance)
  44. // and suffix them by the namespace.
  45. if ((options.classMap)) {
  46. Object.keys(options.classMap).forEach(package => {
  47. var babylonRegex = new RegExp(`import {(.*)} from ['"](${package})['"];`, "g");
  48. var match = babylonRegex.exec(str);
  49. let classes = new Set();
  50. while (match != null) {
  51. if (match[1]) {
  52. match[1].split(",").forEach(element => {
  53. classes.add(element.trim());
  54. });
  55. }
  56. match = babylonRegex.exec(str);
  57. }
  58. str = str.replace(babylonRegex, '');
  59. classes.forEach(cls => {
  60. let rg = new RegExp(`([ <])(${cls})([^\\w])`, "g")
  61. str = str.replace(rg, `$1${options.classMap[package]}.$2$3`);
  62. });
  63. })
  64. }
  65. // Clean up export.
  66. str = str.replace(/export {(.*)};/g, '');
  67. // Clean up left import.
  68. str = str.replace(/import (.*);/g, "");
  69. // Rearrange the d.ts.
  70. str = str.split("\n").filter(line => line.trim()).filter(line => line.indexOf("export * from") === -1).join("\n");
  71. // Remove empty module declaration
  72. var cleanEmptyNamespace = function(str, moduleName) {
  73. let emptyDeclareRegexp = new RegExp("declare module " + moduleName + " {\n}\n", "g");
  74. str = str.replace(emptyDeclareRegexp, "");
  75. emptyDeclareRegexp = new RegExp("declare module " + moduleName + " {\r\n}\r\n", "g");
  76. str = str.replace(emptyDeclareRegexp, "");
  77. return str;
  78. }
  79. str = cleanEmptyNamespace(str, options.moduleName);
  80. // Remove empty module declaration of specific modules
  81. if (options.moduleSpecifics) {
  82. options.moduleSpecifics.forEach(function(specific) {
  83. str = cleanEmptyNamespace(str, specific.namespace);
  84. });
  85. }
  86. return str;
  87. }
  88. module.exports = function(fileLocation, options, cb) {
  89. options = options || { };
  90. fs.readFile(fileLocation, function(err, data) {
  91. if (err) throw err;
  92. data += "";
  93. // For Raanan, litteral import hack TO BETTER INTEGRATE
  94. data = data.replace('import "../sass/main.scss";', "");
  95. if (options.prependText) {
  96. data = options.prependText + '\n' + data.toString();
  97. }
  98. var newData = "";
  99. if (options) {
  100. newData = processData(data, options);
  101. fs.writeFileSync(fileLocation.replace('.module', ''), newData);
  102. }
  103. if (options.doNotAppendNamespace) {
  104. fs.writeFileSync(fileLocation, data);
  105. }
  106. else {
  107. fs.writeFileSync(fileLocation, data + "\n" + newData);
  108. }
  109. cb && cb();
  110. });
  111. }