0

I'm using TypeScript and have all of my JS files compiled to ./dist/. I want to enumerate these JS files and write a line to ./index.js for each file:

module.exports.{filename} = require("./dist/{filename}").{filename};

Example:

module.exports.MyClass1 = require("./dist/MyClass1").MyClass1;
module.exports.MyClass2 = require("./dist/MyClass2").MyClass2;
module.exports.MyClass3 = require("./dist/MyClass3").MyClass3;
// ... etc. ...

This would auto-export all of my classes so that I don't have to do it by hand. I need to implement this as a gulp task, but I'm not exactly sure how to go about it.

Josh M.
  • 26,437
  • 24
  • 119
  • 200
  • Why not just require the classes you need? What problem is that `index.js` supposed to solve? And if you need to just use all files in a directory, why do it as a gulp task? You could just do this programmatically. Who told you to implement it as a gulp task? – crackmigg Jan 31 '16 at 19:26
  • I'd like to see an example of your suggestion; it may work fine for me. Currently I have no `index.js` at all because I was planning on importing the classes I need directly from the `./dist` dir. But it seems to be less user-friendly this way. If I add the exports to `index.js`, Then I can just `const MyModule = require("MyModule"); var x = new MyModule.Class1(...)`. – Josh M. Jan 31 '16 at 19:48
  • I'm relatively new to Node.JS so please correct me if I'm misunderstanding something. – Josh M. Jan 31 '16 at 19:49
  • From your profile I was guessing that you maybe are trying to do C# stuff in node.js, which will probably be not the best way. It would be helpful to see what you are trying to do with the `index.js`, to better understand. Could you provide the part where you require the index.js and then do something with it? – crackmigg Jan 31 '16 at 19:51
  • Then maybe this is what you need: http://stackoverflow.com/a/5365577/387573 – crackmigg Jan 31 '16 at 20:18
  • @migg, you may be right about my background and what I'm trying to do. Really I'm simply trying to expose all my exported classes in the most "node-friendly" way. Your link is pretty much exactly what I need to do, I just wanted to automate it via gulp during the build process. For more context, here is the project I'm talking about: https://github.com/QuantumConcepts/js-base -- all of the TS files are compiled to `./dist/`, and I want to fill `./index.js` with those exported modules. – Josh M. Jan 31 '16 at 22:24
  • @migg, if you create an answer regarding your "then maybe this is what you need" comment/link above, I'll accept it. I ended up using similar code to that other question/answer. – Josh M. Feb 01 '16 at 15:29
  • 1
    Then the link to the "duplicate" is enough, I don't need to answer an already answered question again ^^ – crackmigg Feb 01 '16 at 19:20

0 Answers0