gulp-processTypescriptDeclaration.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. // Replace import { foo, bar } from ...
  47. Object.keys(options.classMap).forEach(package => {
  48. var babylonRegex = new RegExp(`import {(.*)} from ['"](${package})['"];`, "g");
  49. var match = babylonRegex.exec(str);
  50. let classes = new Set();
  51. while (match != null) {
  52. if (match[1]) {
  53. match[1].split(",").forEach(element => {
  54. classes.add(element.trim());
  55. });
  56. }
  57. match = babylonRegex.exec(str);
  58. }
  59. str = str.replace(babylonRegex, '');
  60. classes.forEach(cls => {
  61. let rg = new RegExp(`([ <])(${cls})([^\\w])`, "g")
  62. str = str.replace(rg, `$1${options.classMap[package]}.$2$3`);
  63. });
  64. });
  65. // Replace import { foo as A, bar as B } from ...
  66. // Object.keys(options.classMap).forEach(package => {
  67. // var babylonRegex = new RegExp(`import {(.*)} from ['"](${package})['"];`, "g");
  68. // var match = babylonRegex.exec(str);
  69. // let classes = new Set();
  70. // while (match != null) {
  71. // if (match[1]) {
  72. // match[1].split(",").forEach(element => {
  73. // classes.add(element.trim());
  74. // });
  75. // }
  76. // match = babylonRegex.exec(str);
  77. // }
  78. // str = str.replace(babylonRegex, '');
  79. // classes.forEach(cls => {
  80. // let rg = new RegExp(`([ <])(${cls})([^\\w])`, "g")
  81. // str = str.replace(rg, `$1${options.classMap[package]}.$2$3`);
  82. // });
  83. // });
  84. // Replace import * as ...
  85. Object.keys(options.classMap).forEach(package => {
  86. var babylonRegex = new RegExp(`import \\* as (.*) from ['"](${package})['"];`, "g");
  87. var match = babylonRegex.exec(str);
  88. let localNamespace = "";
  89. if (match && match[1]) {
  90. localNamespace = match[1].trim();
  91. }
  92. else {
  93. return;
  94. }
  95. str = str.replace(babylonRegex, '');
  96. let rg = new RegExp(`([ <])(${localNamespace}.)([A-Za-z])`, "g")
  97. str = str.replace(rg, `$1${options.classMap[package]}.$3`);
  98. });
  99. }
  100. // Clean up export.
  101. str = str.replace(/export {(.*)};/g, '');
  102. // Clean up left import.
  103. str = str.replace(/import (.*);/g, "");
  104. // Rearrange the d.ts.
  105. str = str.split("\n").filter(line => line.trim()).filter(line => line.indexOf("export * from") === -1).join("\n");
  106. // Remove empty module declaration
  107. var cleanEmptyNamespace = function(str, moduleName) {
  108. let emptyDeclareRegexp = new RegExp("declare module " + moduleName + " {\n}\n", "g");
  109. str = str.replace(emptyDeclareRegexp, "");
  110. emptyDeclareRegexp = new RegExp("declare module " + moduleName + " {\r\n}\r\n", "g");
  111. str = str.replace(emptyDeclareRegexp, "");
  112. return str;
  113. }
  114. str = cleanEmptyNamespace(str, options.moduleName);
  115. // Remove empty module declaration of specific modules
  116. if (options.moduleSpecifics) {
  117. options.moduleSpecifics.forEach(function(specific) {
  118. str = cleanEmptyNamespace(str, specific.namespace);
  119. });
  120. }
  121. return str;
  122. }
  123. module.exports = function(fileLocation, options, cb) {
  124. options = options || { };
  125. fs.readFile(fileLocation, function(err, data) {
  126. if (err) throw err;
  127. data += "";
  128. // For Raanan, litteral import hack TO BETTER INTEGRATE
  129. data = data.replace('import "../sass/main.scss";', "");
  130. if (options.prependText) {
  131. data = options.prependText + '\n' + data.toString();
  132. }
  133. var newData = "";
  134. if (options) {
  135. newData = processData(data, options);
  136. fs.writeFileSync(fileLocation.replace('.module', ''), newData);
  137. }
  138. if (options.doNotAppendNamespace) {
  139. fs.writeFileSync(fileLocation, data);
  140. }
  141. else {
  142. fs.writeFileSync(fileLocation, data + "\n" + newData);
  143. }
  144. cb && cb();
  145. });
  146. }