How can I not define a function in a package and use the package on another file where the function is defined?


#1

I made a few posts today and realized most of what I need to do. Basically, I have a package called eval. I want eval to evaluate a function called main, and display that output/result at the top. To do this, I wrote eval main in the message.textContent space. The function will change every time and will be in different files, but will always be called main. So basically if I hit toggle eval then the function main in the file I am in will be evaluated and displayed up top in the display box. In the package, it says main is undefined, but I don’t want the package to search within itself for the function, but rather wherever I am when I toggle it.


Call function from different file? Also, creating new file does not have any language on it
Function is reserved word
#2

It sounds like you need to require the file that is currently open and then call the main function. This is, of course, assuming that the file that is currently open exports main.

You can find information on the Node.js module system here:

https://nodejs.org/api/modules.html


#3

I can put the exports into the new file, but then the package will have to know the file names. The only constant thing is the function’s name, main


#4

No, the package won’t need to know the file names. You can require a string variable just as easily as a string literal:

path = "foo"
require path

#5

So in my case, I would have

path "main"
require path

? This still returns cannot find module ‘main’


#6

Also, thanks for helping me out so much. If what you just suggested doesn’t work, then I have another method that I don’t exactly know how to do, but would like to propose. I have made a package that whatever text I highlight with my cursor and hit the keystroke, then that text is transformed to “Akshar13”. I did this as a test a while back, but what if I made it so that the highlighted text was compiled/ran? This would be better, but if that’s not possible then I can continue with what you had said.


#7

No, because the path should be the file name where the main function is declared and exported. So you can do something like:

path = atom.workspace.getActiveTextEditor().getPath()
require path

#8

Thank you again, but I just need a little bit more help.

Basically, after the message.context part that displays something at the top in that message box, I put eval main. Is that correct?

Also, with eval main there, I tested it by putting a function called main in another file:

main = (x) ->
x + 3

when I run the package on this, I get a box at the top saying function(x) {return x+3}

Basically, how do I fix this. I think it’s because I didn’t define X, but whenever I do, it simply returns the same thing.


#9

You do know that CoffeeScript is an indentation-significant language, yes? This code:

main = (x) ->
x + 3

Does not equal:

main = (x) ->
  x + 3

The second snippet defines a function that takes a parameter x and returns the value of x plus 3.

The first snippet defines a function that always returns undefined. And executes the expression x + 3 when the script is loaded.


#10

My apologies when typing the reply, but I have the correct indentation in my actual code in atom


#11

Thank you again for everything, I just need help with this one last part.