123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- // Gulp Tools
- var fs = require("fs");
- var processData = function(data, options) {
- var str = "" + data;
- // Start process by extracting all lines.
- let lines = str.split('\n');
- var firstIndex = lines.findIndex((line => { return line.indexOf(`'${options.packageName}/'`) !== -1 }));
- var lastIndex = lines.findIndex(((line, idx) => { return line.trim() === '}' && idx > firstIndex }));
- lines.splice(firstIndex, lastIndex - firstIndex + 1);
- // Let's go line by line and check if we have special folder replacements
- // Replaces declare module 'babylonjs'; by declare module BABYLON for instance
- for (var index = 0; index < lines.length; index++) {
- var namespace = options.moduleName;
- var regex = /declare module '(.*)' {/g;
- if (options.moduleSpecifics) {
- var match = regex.exec(lines[index]);
- if (!match) {
- continue;
- }
- var module = match[1];
- options.moduleSpecifics.forEach(function(specific) {
- if (module.indexOf(specific.path) > -1) {
- namespace = specific.namespace;
- }
- });
- }
- lines[index] = lines[index].replace(regex, `declare module ${namespace} {`);
- }
- // Recreate the file.
- str = lines.join('\n');
- // Let s clean up all the import * from BABYLON or the package itself as we know it is part of
- // the same namespace... Should be
- str = str.replace("import * as BABYLON from 'babylonjs';", "");
- let regexp = new RegExp(`import {(.*)} from ['"]${options.packageName}(.*)['"];`, 'g');
- str = str.replace(regexp, '');
- // Let s clean other chosen imports from the mix.
- if (options.importsToRemove) {
- while (options.importsToRemove.length) {
- let remove = options.importsToRemove.pop();
- str = str.replace(new RegExp(`import '${remove}';`), '');
- }
- }
- // Find all other imported classes (Part of BABYLON or Loaders for instance)
- // and suffix them by the namespace.
- if ((options.classMap)) {
- Object.keys(options.classMap).forEach(package => {
- var babylonRegex = new RegExp(`import {(.*)} from ['"](${package})['"];`, "g");
- var match = babylonRegex.exec(str);
- let classes = new Set();
- while (match != null) {
- if (match[1]) {
- match[1].split(",").forEach(element => {
- classes.add(element.trim());
- });
- }
- match = babylonRegex.exec(str);
- }
- str = str.replace(babylonRegex, '');
- classes.forEach(cls => {
- let rg = new RegExp(`([ <])(${cls})([^\\w])`, "g")
- str = str.replace(rg, `$1${options.classMap[package]}.$2$3`);
- });
- })
- }
- // Clean up export.
- str = str.replace(/export {(.*)};/g, '');
- // Clean up left import.
- str = str.replace(/import (.*);/g, "");
- // Rearrange the d.ts.
- str = str.split("\n").filter(line => line.trim()).filter(line => line.indexOf("export * from") === -1).join("\n");
- // Remove empty module declaration
- var cleanEmptyNamespace = function(str, moduleName) {
- let emptyDeclareRegexp = new RegExp("declare module " + moduleName + " {\n}\n", "g");
- str = str.replace(emptyDeclareRegexp, "");
- emptyDeclareRegexp = new RegExp("declare module " + moduleName + " {\r\n}\r\n", "g");
- str = str.replace(emptyDeclareRegexp, "");
- return str;
- }
- str = cleanEmptyNamespace(str, options.moduleName);
- // Remove empty module declaration of specific modules
- if (options.moduleSpecifics) {
- options.moduleSpecifics.forEach(function(specific) {
- str = cleanEmptyNamespace(str, specific.namespace);
- });
- }
- return str;
- }
- module.exports = function(fileLocation, options, cb) {
- options = options || { };
- fs.readFile(fileLocation, function(err, data) {
- if (err) throw err;
- data += "";
- // For Raanan, litteral import hack TO BETTER INTEGRATE
- data = data.replace('import "../sass/main.scss";', "");
- if (options.prependText) {
- data = options.prependText + '\n' + data.toString();
- }
-
- var newData = "";
- if (options) {
- newData = processData(data, options);
- fs.writeFileSync(fileLocation.replace('.module', ''), newData);
- }
- if (options.doNotAppendNamespace) {
- fs.writeFileSync(fileLocation, data);
- }
- else {
- fs.writeFileSync(fileLocation, data + "\n" + newData);
- }
- cb && cb();
- });
- }
|