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
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');
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With