Define the same Grunt Task twice with different properties - javascript

I use Grunt to run Nightwatch tests. I have created a Grunt task for running an express proxy server that is run before the tests can be run. Then I registered a task to run this proxy first and after that execute the Nightwatch tests. Now I want to register a second Grunt task that runs a different express server and then executes the nightwatch tests. How can I define a second express task? This is my express task:
express: {
options: {
// Override defaults here
},
dev: {
options: {
script: 'server/server.js'
}
}
},
And I start it like this:
grunt.registerTask('testnightwatch', [ 'express:dev' , 'nightwatch']);
I need something like this:
express2: {
options: {
// Override defaults here
},
dev: {
options: {
script: 'server/server2.js'
}
}
},
grunt.registerTask('testnightwatch', [ 'express2:dev' , 'nightwatch']);
But this does not work... Does anybody know how to do this?

You can do this:
express: {
options: {
// Override defaults here
},
dev1: {
options: {
script: 'server/server.js'
}
},
dev2: {
options: {
script: 'server/server2.js'
}
}
},
and start it like this
grunt.registerTask('testnightwatch1', [ 'express:dev1' , 'nightwatch']);
or this
grunt.registerTask('testnightwatch2', [ 'express:dev2' , 'nightwatch']);

I think you can do something like that :
express: {
express1 : {
options: {},
dev: {}
},
express2 : {
options: {},
dev: {}
}
}
But you have to check if your task supports multi task.

Related

Trigger grunt task after browserify/watchify run

I'm trying to build my JS-bundle via grunt/browserify. I also use grunt for scss compile. For that I use grunt-watch to trigger scss compile on changes.
I wanted the same behavior for JS-browserify. grunt-browserify comes with the "watch" option, which does the trick very well.
My only issue is: I want to get notified after a "re-browserify". For SCSS I use grunt-notify. But I do not find any way to trigger a grunt task after browserify-watch.
My gruntfile excerpt:
var gruntOptions = {
notify: {
browserify_node_modules: {
options: {
title: "Browserify",
message: "node_modules build"
}
}
},
browserify: {
node_modules: {
src: ["node_modules.js"],
dest: "trunk/js/lib/node_modules.js",
options: {
watch: true,
}
}
}
};
Best case scenario:
options: {
watch: true,
finishTask: "notify:browserify_node_modules"
}
Thanks!
You can check if the file generated by Browserify has been changed and then, trigger a task.
Install watch task in Grunt:
npm install grunt-contrib-watch --save-dev
Configure watch task within Gruntfile.js:
grunt.initConfig({
// other tasks
your_task: {
// ...
},
watch: {
dev: {
files: 'trunk/js/lib/node_modules.js', // file generated by Browserify
options: { spawn: false },
tasks: ['your_task']
}
}
});
grunt.loadNpmTasks('grunt-contrib-watch');

grunt-frontend cannot assign to read only property 'spidermonkey'

I have a problem with grunt-frontend package.
When I run grunt task I get:
Running "frontend-js:main" (frontend-js) task
Processing /assets/js/main.min.js
Warning: Cannot assign to read only property 'spidermonkey' of /assets/js/main.min.js Used --force, continuing.
What is going wrong?
I have Gruntfile.js configured like in the documentation:
'frontend-js': {
main: {
options: {
minify: true,
uglify: {}
},
files: {
'out/js/f.js': [
'test/js/file1.js',
'test/js/file2.js'
]
}
}
}
When I set minify: false everything works normally, except minification of course.

pass middleman variable to grunt file

My middleman template has an 'id' variable that I put my html emails job name into.
I know if I change my middleman erb file from index.html.erb to newName.html.erb it will output that as the final files name.
My problem is that most of my grunt tasks require the file name I want them to run on (I've tried using *.html, but it only works for some tasks) and short of editing that in the grunt file prior to starting grunt up they won't execute if I change the erb file name.
Is there a way to pass grunt that 'id' variable to name the file middleman is outputting and also plug that variable into the various tasks so they too accept that as what the filename?
Here is my grunt config:
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
// Build html
middleman: {
options: {
useBundle: true
},
server: {},
build: {
options: {
command: "build"
}
}
},
// Format html file
prettify: {
options: {
// Task-specific options go here.
},
one: {
src: 'build/index.html',
dest: '_output/index.html'
}
},
// Run the text converter
execute: {
simple_target_with_args: {
options: {
// execute node with additional arguments
args: ['_output/index.html']
},
src: ['node_modules/node-text-converter/converter.js']
}
},
'special-html': {
compile: {
files: {
'_output/index.html': '_output/index.html',
}
}
},
'phantomjs_screenshot': {
main: {
options: {
delay: 1000
},
files: [{
expand: true,
cwd: '_output',
src: ['**/*.html'],
dest: '_output/screenshots/',
ext: '.jpg'
}]
}
}
});
You should store your variable in a JSON file and import it both into Middleman and Grunt.

How can i run a task more than once within the grunt initConfig function

I am running a grunt-contrib-clean task at the start of my grunt build to clear out the target directory. I also want to run another clean task at the end to do some tiding up.
module.exports = function (grunt) {
grunt.initConfig({
clean: {
options: { force: true },
all: {
//..
}
},
//Other tasks
clean2: {
options: { force: true },
all: {
//..
}
}
});
grunt.registerTask('default', ['']);
grunt.registerTask('build', ['clean', 'clean2']);
grunt.loadNpmTasks('grunt-contrib-clean');
};
How do I call the clean task twice with different arguments?
What you have already is pretty close. Looking at the documentation for grunt-contrib-clean, it seems you should take a look at their "long" usage example.
I believe you can do this simply by configuring clean as so:
clean: {
target: {
src: "path/to/target",
options: { force: true },
all: {
//..
}
},
otherFolder: {
src: "path/to/other/folder",
options: { force: true },
all: {
//..
}
}
}
Then, you can register the task by referencing which clean you want:
grunt.registerTask('build', ['clean:target', 'clean:otherFolder']);
As a side note: I would double check if you really need to use the force option. Per the documentation: use with caution.

Unable to pass arguments from command to grunt file

I am using the code bellow for arguments which are using in protractor configuration file
protractor: {
options: {
keepAlive: true,
configFile: "test/config.js",
args:{
params:{
user:"user1",
password:"password1"
}
}
},
and retrieving in protractor conf file as browser.params.user,browser.params.password
These are working files.
I want to change the user and password values from command.
How to change the values?
This is a simple work around:
When you pass a parameter to your grunt task:
grunt e2e --user=alex --password=password
it's available as
grunt.option('user')
You can then edit the values you have in the config using:
var protConfig = grunt.config.get('protractor');
protConfig.options['someKey']=newValue;
grunt.config('protractor', protConfig);
grunt.task.run('protractor');
Not sure this is the best way to go about it, but it's working fine for me.
Also notice that we're wrapping the protractor task rather than calling it right away
how to fetch --user argument in protractor Specs?
In the code below I register a task "myprotractor" and whatever comes after the task as argument will go as parameter into the anonymous function:
grunt myprotractor:dev:pwd
module.exports = function(grunt) {
grunt.registerTask('myprotractor', function(user, pwd) {
console.log(user + pwd);
grunt.config('protractor', {
options: {
keepAlive: true,
configFile: "test/config.js",
args: {
params: {
user: user,
password: pwd
}
}
}
});
//here I am running the task
grunt.task.run([
'protractor'
]);
});
};
If you need you can configure 2 targets for protractor, having some common configuration and the args being set depending if you wish them from cmd or from config.
grunt myprotractor:cmd:dev:pwd
module.exports = function(grunt) {
grunt.registerTask('myprotractor', function(target, user, pwd) {
console.log(user + pwd);
grunt.config('protractor', {
options: {
keepAlive: true,
configFile: "test/config.js"
},
cmd: {
options: {
args: {
params: {
user: user,
password: pwd
}
}
}
},
config: {}
});
//here I am running the task with a target
grunt.task.run([
'protractor:' + target
]);
});
};

Categories

Resources