reacto icon indicating copy to clipboard operation
reacto copied to clipboard

Open project from command line

Open frontendwizard opened this issue 7 years ago • 37 comments

frontendwizard avatar Mar 21 '18 12:03 frontendwizard

I truly have no idea how to do this. Do you have any leads? Also, is this necessary at the moment?

eveningkid avatar Mar 21 '18 15:03 eveningkid

This is a good thing to have, as other code editors supports it (e.g code path/to/file/or/project). I'm not quite sure on how these things works on Mac, though.

vaporwavie avatar Mar 21 '18 17:03 vaporwavie

I found an article about that. I'll give it a try. Should the cli be called reacto? I feel like this would be good.

frontendwizard avatar Mar 21 '18 19:03 frontendwizard

Sure, if you find any details about this and don't have time to implement it, share it there. I can take care of it. :) Let me know, so we can update this issue and make it available asap.

eveningkid avatar Mar 25 '18 13:03 eveningkid

yeah, I did found out that you can use the app binary as a cli by calling /Application/Reacto.app/Contents/MacOS/Reacto and then handling the arguments on the main.js as explained here: https://discuss.atom.io/t/bundle-a-cli-app-inside-the-electron-app/40113/2 I'll be putting some time on this approach today.

frontendwizard avatar Mar 25 '18 16:03 frontendwizard

https://github.com/atom/atom/blob/master/src/main-process/parse-command-line.js atom deals with this using yargs

frontendwizard avatar Mar 25 '18 17:03 frontendwizard

Really interesting approach. What are your thoughts on such a CLI helper for Reacto?

eveningkid avatar Mar 25 '18 17:03 eveningkid

I'm not really sure it is needed if we are doing only one thing without any options. But if we want this to a be a more complete solution and have more options, yargs is indeed a nice solution. Short story: we might not need now, but we'll surely need it later.

frontendwizard avatar Mar 25 '18 18:03 frontendwizard

Correction, right now it'd already be useful by parsing the options and showing the version when we type reacto -v.

frontendwizard avatar Mar 25 '18 18:03 frontendwizard

The only problem with this approach is that I don't really know how to test this in development. Do you have any ideas on how to test this without making a package?

frontendwizard avatar Mar 25 '18 19:03 frontendwizard

If we only get the current version of Reacto, I don't think that a CLI is currently needed. I guess you'll need to create a package even for testing. Simply set the bin attribute to where it should call the CLI from (quickly look this up this online, bin property of package.json). Your CLI will be just a node script I guess, right?

eveningkid avatar Mar 27 '18 18:03 eveningkid

Ok, I agree that we do not need many options for now. The cli effectively is the own binary. We gotta handle the call from cli by parsing process.argv on main.js and checking if there's more arguments on it. If there is, than we check if the argument is a directory (I guess we should also open single files...) and then load the directory through the filetree function. Is it only my machine that takes like 20+ minutes to pack the application?

frontendwizard avatar Mar 27 '18 18:03 frontendwizard

I think your approach is correct and could work. I just have no idea how to actually do all the communication between a running reacto instance and what we get from the command line.

If you feel like spending time on this, please do some experiments and share your results here. I think that at this point, this is the only way to actually get answers. Maybe it won't work, maybe it will, haha.

And usually, I stop the build process when it says something like building "maps" or something. So usually, as soon as it updated the .dmg file, it's all good. Why asking? Do you need to pack it every single time for testing? :o

eveningkid avatar Mar 28 '18 16:03 eveningkid

This might be helpful, https://github.com/atom/atom/blob/d15e65a2c0101df280a60c9d99c03ff8153fffca/script/lib/install-application.js

techwritescode avatar Mar 28 '18 18:03 techwritescode

Ok. Did some progress here. Now I'm parsing the command line and getting the desired path to open. The quest is to make it open the project. I believe I should send a message with ipcMain to the render process to trigger the switchProject action with the directory path. Does that makes sense? Where should this logic live on the application? Should I add a listener on the open project component to take care of this?

frontendwizard avatar Mar 29 '18 13:03 frontendwizard

Somewhere inside public/main.js, when something is caught from the command line, simply call mainWindow.webContents.send('switch-project', cwd).

Then, inside src/editor/events, create a file called switch-project.js, which simply dispatches the switchProject action (you can check other files in that folder, you'll quickly get the idea!).

Finally, add the listener to the src/editor/events/index.js file: import switch-project, add it to the events variable, and let the EventsManager listen to the switch-project event. Don't forget to pass it the cwd argument value.

I guess you have the whole recipe now! Thanks again for your help, I'm excited about this feature now!

eveningkid avatar Mar 29 '18 16:03 eveningkid

Ok, I made it. It's opening a directory as project from the command line. Now I need to add a menu command to add reacto bin to PATH. I can't test this on other OS (windows and linux). It might require some adjustments.

frontendwizard avatar Apr 01 '18 14:04 frontendwizard

Amazing @julianorafael, please let us know when it's ready!!

eveningkid avatar Apr 01 '18 14:04 eveningkid

I solved this by creating a symlink from the binary to /usr/local/bin on mac os. I added menu options to add and to delete this symlink and it only shows on darwin platforms for now. The only problem remainig is that the app remains attached to the console. I need to detach it, but I'm not sure how to do it yet.

frontendwizard avatar Apr 03 '18 17:04 frontendwizard

@julianorafael could you please share your code so far so that could be easier to understand your issue at the moment?

eveningkid avatar Apr 06 '18 07:04 eveningkid

Ok. I opened up a PR with the code. It appears that there has been some major changes on main.js and my code will need some changes. I couldn't find a way to do what I did on the ready event of the app with the sweet-electron. Also, as I said before, it opens from the terminal, but does not detach from it, which is quite annoying.

frontendwizard avatar Apr 06 '18 12:04 frontendwizard

Thank you. I will add a .ready((is, mainWindow) => ...) method to sweet-electron tomorrow. I guess it will be enough to adapt code from main.js. If not, please let me know and I'll add it as well. :)

For the menu, it should also be quite easy to fix the conflict, same for events.js. Oh also, for the menu, I will add an instance of electron-is as .menu(...) first parameter so you'll be able to write is.macOS() inside menu.js.

For the terminal issue, is it different for Atom right now? If so, have you tried to find how they deal with it? 👀

Thank you again for all your energy and time, I definitely appreciate it.

eveningkid avatar Apr 06 '18 16:04 eveningkid

Your welcome sir, pleasure is mine. I'm a bit short on time, but I'm doing whatever I can. I'll find a way to deal with it and I'll refactor the code as soon as the method is available. The refactor is quite simple. The only thing is the ready event. Thank you for the help and the attention, this is one of my first contributions with actual code to OSS projects. :)

frontendwizard avatar Apr 06 '18 18:04 frontendwizard

Alright, I've just added the .ready method on sweet() instance! You'll find an example here, but it is exactly as what I've explained in my previous message. :) (it should be fine, but if it's not, please let me know)

.ready((is, mainWindow) => ...)

Don't forget to upgrade sweet-electron to its latest version.

Also, I am extremely glad that this is your first contribution to an open-source project!

Keep me updated and keep it up!

eveningkid avatar Apr 07 '18 08:04 eveningkid

I'm having problems with the ready method. image

frontendwizard avatar Apr 12 '18 14:04 frontendwizard

code

frontendwizard avatar Apr 12 '18 14:04 frontendwizard

I'm on version 1.0.7 of the sweet-electron

frontendwizard avatar Apr 12 '18 14:04 frontendwizard

The error you're having shows the Reacto icon instead of Electron's. Are you sure all this is running in development mode?

eveningkid avatar Apr 12 '18 14:04 eveningkid

no, is not. in development mode I disabled the open from cmd because it'd always open the reacto directory by default since we always start the development mode from the cmd

frontendwizard avatar Apr 12 '18 14:04 frontendwizard

Have you tried small debugging like the following?

const electron = require('electron');
const sweet = require('sweet-electron')(electron);
console.log(sweet().ready);

Replacing all main.js's content with this short code and see what happens? Because just now I've tried with a very basic project and the ready method works just fine.

eveningkid avatar Apr 12 '18 14:04 eveningkid