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

importing firebase-tools in gulp makes tasks hang #86

Closed
rolandjitsu opened this issue Oct 29, 2015 · 6 comments
Closed

importing firebase-tools in gulp makes tasks hang #86

rolandjitsu opened this issue Oct 29, 2015 · 6 comments

Comments

@rolandjitsu
Copy link

It looks like if I import { deploy } from 'firebase-tools' or require('firebase-tools') in my gulp file, any tasks I start it seems to be hanging after it's done and it never exists.

You can check it on Travis by looking at one of my failed builds. If you look at what changed from the previous versions you can see that I've added firebase tools to manage the deployments to Firebase.

@mbleigh
Copy link
Contributor

mbleigh commented Oct 29, 2015

@rolandjitsu this is unfortunately a current limitation of the Firebase real-time client (which is used by firebase-tools behind the scenes. You can wait for the promise to return and then manually exit the process with process.exit(0) or alternatively use something like require('child_process').spawnSync to deploy via shelling out instead.

@mbleigh mbleigh closed this as completed Oct 29, 2015
@rolandjitsu
Copy link
Author

@mbleigh I am unsure what you mean with You can wait for the promise to return and then manually exit the process with process.exit(0), could you have a short example for me?

Also with require('child_process').spawnSync would be useful.

@mbleigh
Copy link
Contributor

mbleigh commented Oct 29, 2015

var tools = require('firebase-tools');
tools.deploy({
  firebase: 'YOUR_FIREBASE_PROJECT',
  token: 'YOUR_FIREBASE_TOKEN'
}).then(function() {
  process.exit(0);
}).catch(function(err) {
  console.log(err);
  process.exit(1);
});

via spawn:

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

var proc = spawn("./node_modules/.bin/firebase", ["deploy", "-f", "YOUR_FIREBASE_PROJECT", "--token", "YOUR_TOKEN"]);
proc.on('close', function(code) {
  if (code === 0) {
    // succeeded
  } else {
    // failed
  }
});

Note: I wrote these without testing them, but they should be close to correct.

@rolandjitsu
Copy link
Author

@mbleigh thanks. I already had this done, the second version, but I thought you may have another way of doing it.

@jensenbox
Copy link

Are there tools that will show any open resources? Took me a while to find out that it was firebase causing this.

Would love a better answer than "kill the beast"

@rolandjitsu
Copy link
Author

@jensenbox to find out if there any processes running you could ps aux on Mac and maybe do a grep if you are looking for something specific. But that would not help much anyway. So the best way would be what @mbleigh suggested, spawn a child and run the process there. I have a simple fn that does just that:

import gulp from 'gulp';
import {exec} from 'child_process';
import {env, log, colors} from 'gulp-util';

// https://github.com/firebase/firebase-tools#commands
function runFirebaseCommand(cmd, args = []) {
    let binary = process.platform === 'win32' ? 'node_modules\\.bin\\firebase' : 'node_modules/.bin/firebase'; // Windows or Linux path
    const TOKEN = process.env.FIREBASE_TOKEN || env.token;
    if (!TOKEN) {
        log(colors.red('No FIREBASE_TOKEN found in env or --token option passed.'));
        return Promise.reject();
    }
    let defaultArgs = [
        '--non-interactive',
        '--token',
        `"${TOKEN}"`
    ];
    if (Array.isArray(args)) args.unshift.apply(args, defaultArgs);
    else args = defaultArgs;
    binary += ` ${cmd}`;
    args.unshift(binary);
    let proc = exec(args.join(' '));
    // streamProcLog(proc); // a fn to stream data to from the child process
    return proc;
}

gulp.task(function deploy() {
    return runFirebaseCommand('deploy');
});

And you can use it as such runFirebaseCommand('deploy:hosting'). Keep in mind that the above example is using Gulp 4 and ES6.

If you want to pass extra args, use the second arg of the fn runFirebaseCommand('<command>', ['--arg2', '--arg2']).

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

3 participants