Gulp hook before 'cordova build xxx'

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

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

But, for example, I run, for example, cordova build android it says 'console' is undefined , do I need to do something else to run Javascript? I could not find more information.

Thanks!

EDIT:

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

+6
source share
3 answers

You can run it without leaving Node by pasting this code into the 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'); 
+8
source

I found that I had to use __dirname to request 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: here 'gulpfile.js' and 'hook.js' are in the same directory. You can set your own path to include the js file in the Cordova config.xml file:

 <hook type="before_build" src="app/hook.js" /> 
+4
source

Finally, I can run the command, here is my .js file inside hooks/before_build

 #!/usr/bin/env node var sys = require('sys') var exec = require('child_process').exec; function puts(error, stdout, stderr) { sys.puts(stdout) } exec("gulp", puts); 
+3
source

Source: https://habr.com/ru/post/981938/


All Articles