Gulp running `shopify login` doesn't open the browser to authenticate

Viewed 200

folks,

Trying to automate some repeated tasks when switching projects and hit a weird edge-case snag.

When I use the shopify-cli tool to log in to a store, shopify login --store=storename, my terminal (on mac os) opens my browser to authenticate with shopify. shopify-cli will sit there indefinitely waiting for me to verify my identity in my browser, you get a big "Successfully authenticated. You may now close your browser." message in your browser window, and the shell tool finishes running, outputting: 'Logged into store storename.myshopify.com".

Problem is, when I put the same command in gulp:

gulp.task('login', function(cli) {
    var cmd = spawn('shopify', ['login', '--store=storename'], {});
    cmd.on('close', function (code, stdout, stderr) {
        console.log(stdout);
        console.log(stderr);
        cli(err);
    })
});

or like so:

gulp.task('login', function(cli) {
    exec('shopify login --store=storename', function(err, stdout, stderr) {
        console.log(stdout);
        console.log(stderr);
        cli(err);
    })
});

Gulp doesn't push through the browser open/authenticate process to stdout. So login just hangs there not doing anything useful, waiting for the browser that never opened to tell it I've authenticated successfully. I suspect this is related to using console.log to push through the output of stdout; that it somehow loses the browser open request. But reading through the docs, I don't see an alternate way to handle or forward shell command output.

Any guidance you can offer is most appreciated. Thank you!

1 Answers

This below code works fine for me in both scenarios.

  1. Opening browser window and exiting with success on login
  2. Exiting with success if already logged in

You needed to pass stdio:inherit option to spawn. Child process is returned so that Gulp does not complain about task completion.

const gulp = require('gulp')
const {spawn} = require('child_process');

gulp.task('login', function() {
    const cmd = spawn('shopify', ['login', '--store=store-name'], {
        stdio: 'inherit'
    });
    cmd.on('close', function(code, stdout, stderr) {
        console.log(code)
        console.log(stdout);
        console.log(stderr);
    })
    return cmd;
});
Related