-
Notifications
You must be signed in to change notification settings - Fork 744
Syncify with a Subprocess. #402
Copy link
Copy link
Closed
Labels
help wantedmedium priorityquestionQuestion from a user (which may not require code/documentation changes to the project)Question from a user (which may not require code/documentation changes to the project)
Description
Hi Everyone,
I just wanted to open a discussion about something that I've been working on, before I work too hard on it.
I've talked about this before, but here's my idea:
- Because of the way Node works, it's a lot easier to write things asyncly.
- But we want shelljs commands to be sync.
- Therefore, it would be nicer if we could syncify things,
- However, this doesn't work:
function syncSomething(arg1) {
var ret = null, err = null, done = false;
something(arg1, function (err, ret) {
// We will never get here, because the event loop will never be clear, so the callback can never start.
ret = _ret;
err = _err;
done = true;
});
while(!done) continue;
if (err) throw err;
return ret;
}- So, the only way to syncify (at least without a native extension) something is to make a second event loop.
- The only way to do that is to start a second process.
- So, what we could do is this:
- After all the argument parsing,
common.wrapcould do this:
// ...
if (callback || options.cp === false) {
func.apply(null, args.concat([callback]));
} else {
if (!this.cp) this.cp = new ChildProcess();
return this.cp.cmd(this.name, args);
}ChildProcess.prototype.cmdcould do this:
ChildProcess.prototype.cmd = function(cmd, args) {
var id = Math.random().toString();
write({ type: 'cmd', cmd, args, id }); // I've already written read/write, they work, so I won't go into it here.
var ret, err, done;
while (!done) {
var events = read();
for (var i = 0; i < events.length; ++i) {
if (events[i].type === 'cmd' && events[i].id === id) {
ret = events[i].ret;
err = events[i].err;
done = true;
}
}
}
if (err) throw err;
return ret;
}- But since I realized that this was going to basically be a rewrite of shelljs, I thought we should discuss it first.
- Also, @levithomason setup a really awesome build and lint system for shelljs/shx, so if we're going to re-write it, I think we might want to get something similar. (@levithomason, would you be interested/willing in working this?)
Things this would fix (@shelljs/contributors, feel free to edit this and add stuff):
- Showing npm loading bar and colors on stdout #412, command exec("something here", {silent: false}) does not output "colored" text #186 - We can spawn the subprocess with
{ stdio: 'inherit' }, and then it can runexeccommands the same way, since it doesn't need to do the weird thing that exec currently does. That waystdin/stdout/stderraretty's. - [Feature Request] Async versions of all commands #2 - It's easier to have an async version if you don't have to maintain a sync version.
- Some packages that may be helpful #3 - We can use packages that are stream-only.
- 100% cpu usage when a nodejs script goes side ways executing a command. #5, high cpu usage during synchronous exec #167 - We don't have to rely on a hacky wait loop.
- Synchronous exec stalls permenantly when there is an error/w the shell #7
- Maybe Something went wrong #158 - I think this might be caused by exec's wait hack, or by improper cleanup.
- exec command not working. Showing "shell.js : internal error" in nwjs #248
So, @shelljs/contributors and @shelljs/shx, what are your thoughts on this?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
help wantedmedium priorityquestionQuestion from a user (which may not require code/documentation changes to the project)Question from a user (which may not require code/documentation changes to the project)