Skip to content
This repository has been archived by the owner on Jun 17, 2021. It is now read-only.

Bi directional communication #100

Merged
merged 4 commits into from
Jul 24, 2018
Merged

Conversation

joshwcomeau
Copy link
Owner

Thanks for your contribution!

Please fill out the following template with details about your pull request:

Related Issue
#25

Summary
Thanks to @superhawk610's great suggestion, we no longer have to rely on echo to send a command to the running process. Yay unblocking windows support!

Incidentally, fixing this issue exposed a bug, which is that Guppy crashes when ejecting a project while the "View Details" modal is open. This is because the modal assumes a task will be provided, but the eject task removes itself.

The fix is simply to unset the selectedTaskId state when the task is deleted. This feels like maybe it should move to Redux, since deriving state from props like this is discouraged... but I'm happy enough with this solution for now.

// confirms this action.
// TODO: Eject deserves its own Redux action, to avoid cluttering up
// this generic "RUN_TASK" action.
// TODO: Is there a way to "future-proof" this, in case the CRA
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked react-scripts's eject code and seems like the prompted question is hard-coded and there is not any parameters for passing prompts (something like --force or --yestoall). I don't think it is something to change by its own. If there is going to be a change I think it would effect multiple places so I don't think we should worry about it for now.

Copy link
Collaborator

@superhawk610 superhawk610 Jul 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The discussion about Next.js support in #59, as well as this issue of future-proofing, makes me think it may be worthwhile to extract each framework's specific interactions into some sort of plugin file that would abstract CLI interactions into a consistent and predictable style - here's a very loose idea of what the structure might look like:

// plugins/create-react-app.plugin.js
const EJECT_CONFIRM_PROMPT = 'Are you sure you want to eject? This action is permanent';

const CMD_INSTALL = (path: string) => `npx create-react-app ${path}`;
const CMD_EJECT = () => `npx create-react-app eject`;
/*...*/
export const create = () => { /*...*/ };
export const eject = () => { /*...*/ };

I imagine each plugin would have a create block and then could optionally have any number of framework-specific blocks as well, maybe exported as an array so they could be iterated over by the UI without any knowledge of how many there are in total?

Additionally, the CMD_ functions could all be piped through @bennygenel's formatCommandForPlatform.

This is clearly far too much boilerplate to be justified for just CRA and Gatsby, but as the project grows it has the marked benefit of placing all the CLI interaction logic in an easy-to-find (and thus easy to maintain) location and adds a further degree of separation between UI and business logic.

@joshwcomeau
Copy link
Owner Author

I checked react-scripts's eject code and seems like the prompted question is hard-coded and there is not any parameters for passing prompts (something like --force or --yestoall). I don't think it is something to change by its own. If there is going to be a change I think it would effect multiple places so I don't think we should worry about it for now.

Yeah, I checked as well; might be worth opening an issue in CRA eventually. But not yet, it's not a real problem yet.

...it may be worthwhile to extract each framework's specific interactions into some sort of plugin file...

Yeah, totally agree. I think the proposal you made makes sense. I like the idea of a platform-specific util (like formatCommandForPlatform) separate from a project-specific util. I think where it gets tricky is that there's actually a lot of things that need to be tweaked per platform; right now the only special task is eject but long-term I want each built-in task to have specific UI around its platform (like build should have custom UI on both CRA and Gatsby, since with Gatsby maybe we could integrate with their new Preview feature, as an example).

So the abstraction is not 100% clear to me yet and I suspect it may require some iteration... but I think it'll become critical as the project matures.

@joshwcomeau
Copy link
Owner Author

@bennygenel @superhawk610 does this look ok? Could you approve the review, or request changes if there's stuff I should change?

Copy link
Collaborator

@bennygenel bennygenel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks ok to me. I tested the code on Windows too with slight changes to the command. Command was using formatCommandForPlatform.

Copy link
Collaborator

@superhawk610 superhawk610 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Tested and working on MacOS 10.13.5.

@joshwcomeau
Copy link
Owner Author

Thanks y'all!

@joshwcomeau joshwcomeau merged commit 1a9cd1e into master Jul 24, 2018
@joshwcomeau joshwcomeau deleted the bi-directional-communication branch July 24, 2018 10:34
const sendCommandToProcess = (child: any, command: string) => {
// Commands have to be suffixed with '\n' to signal that the command is
// ready to be sent. Same as a regular command + hitting the enter key.
child.stdin.write(`${command}\n`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@superhawk610 At the moment, I'm merging windows-support & npm-to-yarn branch (not pushed yet) and there I've noticed that ejecting is not working with-out error message and it seems that it is happening at line 283. The error message is like in this issue.

Telling that the socket is already closed. Not exactly sure what happens here but it could be related that we're not sending end after writing to stdin.

Sending end can also be problematic because it could close the process.

I'll try to eject at Bennys windows-support branch so I know if it was working there. Maybe this could also be related to my setup:
Windows 10
npm -v 5.6.0
node -v v8.11.1

The app is ejected afterwards but there is this error message in console.

Copy link
Collaborator

@AWolf81 AWolf81 Aug 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, not sure what it was but after removing node_modules and reinstalling. Ejecting is working like it is.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants