Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Odd behaviour when spawning ghostface with child_process.spawn #12

Open
arjunmehta opened this issue Aug 23, 2015 · 3 comments
Open

Odd behaviour when spawning ghostface with child_process.spawn #12

arjunmehta opened this issue Aug 23, 2015 · 3 comments

Comments

@arjunmehta
Copy link

Hi there,

I'm trying to spawn ghostface using child_process.spawn().
It's strange because none of the output is getting piped to the console, nor do any exit events trigger.

My guess is that the main ghostface isn't the process actually producing the output, nor the exit codes. Is there some other sub-process generating the output?

var spawn = require('child_process').spawn;
var spawned_process;

spawned_process = spawn('./node_modules/.bin/ghostface', ['test/browser_test_compiled.js']);
spawned_process.stdout.pipe(process.stdout);
spawned_process.stderr.pipe(process.stderr);

spawned_process.on('exit', function(code, signal){
    console.log("EXITED with code", code);
});

spawned_process.on('error', function(err){
    console.log("ERROR", err);
});

The same setup works for things like browserify, for example if I do:

spawned_process = spawn('./node_modules/.bin/browserify', ['test/browser_tes.js']);

It outputs the stdout and exits with a 0.

Any thoughts?

@fardog
Copy link
Owner

fardog commented Aug 23, 2015

Hmm; I haven't tried this, but I bet this is due to how ghostface detects if it should expect input on stdin (say via a cat somefile.js | ghostface) and it's probably just hanging waiting for input. See this line https://github.com/fardog/ghostface/blob/master/lib/cli.js#L89

This isn't ideal, and I'll leave this issue open and check into it further. However for your use case, you may find it much simpler: ghostface is actually requireable, I just haven't documented this yet.

var fs = require('fs')
var path = require('path')
var ghostface = require('ghostface')

var options = {
  html: path.join(__dirname, 'file.html'),
  timeout: 1000,
  input: fs.createReadStream(path.join(__dirname, 'file.js')),
  phantomPath: '/usr/sbin/phantomjs'
}

ghostface(options, process, function(code, signal) {
  if(code > 0) {
    console.error(sprintf('\nphantomjs exited abnormally: %d'), code)
  }

  process.exit(code || signal === 'SIGTERM' ? 0 : 1)
})

…admittedly, this is not a very clean interface, but it wasn't intended for public consumption (yet!). Let me know if you think that'll work for you, and like I say, I'll leave this issue open as a reminder to:

  • Document the programatic interface
  • Figure out what the root of this bug is

@fardog
Copy link
Owner

fardog commented Aug 23, 2015

oh, and a quick note on the above: the process object that I pass in, you could construct yourself, it just wants something that takes this shape:

{
  stdin: readableStream,
  stderr: writableStream,
  stdout: writableStream
}

You don't have to specifically pass in the process object, but that's possibly easiest for what you're looking to do. You can see how I very simply mock this object in the tests: https://github.com/fardog/ghostface/blob/master/test/index.js#L175

@arjunmehta
Copy link
Author

This actually does work for my situation. Thank you!

While it isn't ideal, it works for me. And while I can personally work around the issue, maybe other people won't be able to, so keeping the issue open is probably a good idea :D

Also, piping into the CLI is what I tried to do initially, and it works for me too, except even that wasn't exiting with proper error codes either. BUT I filed a bug report for that.

Super cool that this can be used with a require!
Thanks again.

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

No branches or pull requests

2 participants