Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gulp hook before 'cordova build xxx'

I have my gulpfile.js with some tasks and I want to execute one task when I do cordova build

I created a before_build folder inside of the hooks folder with a simple console.log("a")in a js file.

But whe I run for example cordova build android it says 'console' is undefined, I have to do something else to run Javascript? I couldn´t find more info.

Thanks!

EDIT:

I added #!/usr/bin/env node at the top of my .js file and the console.log works but now I want to do gulp myTask and is throwing to me gulp is not defined

like image 539
Mati Tucci Avatar asked Feb 03 '15 20:02

Mati Tucci


2 Answers

You can run it without leaving Node by pasting this code into the file before_build/010_compile_css.js:

#!/usr/bin/env node

var gulp = require('gulp');
var path  = require('path');

var rootdir = process.argv[2];
var gulpfile = path.join(rootdir, 'gulpfile.js');

process.stdout.write('Compiling SCSS');

require(gulpfile);

//interaction
gulp.start('scss');
like image 169
JBCP Avatar answered Sep 30 '22 16:09

JBCP


I found I had to use __dirname to require the 'gulpfile.js' file.

#!/usr/bin/env node

module.exports = function(context) {
  var Q = context.requireCordovaModule('q');
  var deferral = new Q.defer();

  var path = require('path'),
      gulp = require('gulp'),
      gulpfile = path.join(__dirname, 'gulpfile');
  require(gulpfile);

  gulp.start('myTask').once('task_stop', function(){
      console.log('myTask done');
      deferral.resolve();
  });

  return deferral.promise;
}

NB: The 'gulpfile.js' and 'hook.js' are in same directory here. You can set custom path to hook js file in Cordova's config.xml file:

<hook type="before_build" src="app/hook.js" />
like image 25
David Douglas Avatar answered Sep 30 '22 16:09

David Douglas