I need to move a file out of the way before one of my Grunt tasks run, and then put it back after the task has completed.
How do I do this with GruntJS?
Basically I want to run this command:
# move node-webkit out of the way
mv app/node-webkit ./tmp
# run grunt task
# move node-webkit back
mv ./tmp/node-webkit ./app/
Yeah, have a look at grunt-shell. In your init config:
shell: {
move: {
command: 'mv app/node-webkit ./tmp'
},
moveback: {
command: 'mv ./tmp/node-webkit ./app/'
}
}
Then, register a function that runs the move command before the other tasks you want to run, then run the moveback task.
module.exports = function(grunt) {
'use strict';
grunt.registerTask('mytask', [
'shell:move',
'othertaskshere',
'shell:moveback'
]);
};
Related
I've been struggling in building automated build using Jenkins with symfony 3.4.
How to properly set environment variables in Jenkins that symfony can find it.
here's my pipeline.
node {
def app
stage('composer install') {
sh 'export $(cat env/env_vars | xargs)'
sh 'composer install --optimize-autoloader'
}
stage('yarn install') {
sh 'yarn install'
}
stage ('build assets') {
sh 'yarn encore production'
}
stage('Clone repository') {
// clone
}
stage('Build image') {
// build here
}
stage('Push image') {
// push here
}
}
then after I run my build.
I always got this message
....
Creating the "app/config/parameters.yml" file
Some parameters are missing. Please provide them.
database_host ('%env(DATABASE_HOST)%'): Script Incenteev\ParameterHandler
\ScriptHandler::buildParameters handling the symfony-scripts event terminated with an exception
[Symfony\Component\Console\Exception\RuntimeException]
Aborted
....
I already used some jenkins plugin like EnvInjector and something similar. But still symfony can't find my environment variables.
You can probably solve this like this:
stage('composer install') {
sh 'export $(cat env/env_vars | xargs) && composer install --optimize-autoloader'
}
This will make the environment variables available in the same shell session.
I have this gruntfile.js fragment
grunt.initConfig({
browserify: {
browser: {
src: [ require('./package.json').main ],
dest: './browser/shortid.js'
},
How would I perform exactly the same thing by calling browserify directly (without grunt) from bash command line? From what directory should I execute browserify?
The following command is equivalent and it would need to be executed in the same directory as your package.json:
browserify . > ./browser/shortid.js
I have 3 separate apps in 3 different folders. e.g. folder1, folder2 and folder3. folder3 is the combination of folder1 and folder2. Right now, I run dist task on both folder1 and folder2, then with bower link I link those dist folders with in folder3. Is there any way I could run a task with in folder3 that will create dist on folder1 and folder2, and link them with folder3? In short I want to run grunt task of folder1 and folder2 from folder3 and link them.
grunt.config.set('exec', {
loginapp: {
command: 'grunt -b C:/project/loginapp dist'
}
});
grunt.registerTask('all', [
'exec:loginapp'
]);
Folder structure
c:\project
----loginapp
dist
GruntFile.js
----webapp
public
bower_components
dist-loginapp
GruntFile.js
Hope this helps. I am in webapp > GruntFile where I run dist and that should run dist task on loginapp before its on dist task.
You can use -b flag to specify an alternate base path where there is a Gruntfile.
Then use grunt-exec to create task that run grunt task of your other projects.
E.g :
module.exports = function(grunt) {
grunt.config.set('exec', {
distFolder1: {
command: 'grunt -b relative/path/to/Gruntfile/of/folder1 taskName'
},
distFolder2: {
command: 'grunt -b relative/path/to/Gruntfile/of/folder2 taskName'
},
});
grunt.loadNpmTasks('grunt-exec');
};
Now you just have create a register task that run these two task and you other task.
E.g :
module.exports = function(grunt) {
grunt.registerTask('distAndLink', [
'exec:distfolder1',
'exec:distfolder2',
// your other task (bower link)
]);
};
and then run grunt distAndLink from folder 3.
In order to customize my grunt tasks, I need access to the grunt task name given on the command line when starting grunt.
The options is no problem, since its well documented (grunt.options).
It's also well documented how to find out the task name, when running a grunt task.
But I need access to the task name before.
Eg, the user writes
grunt build --target=client
When configuring the grunt job in my Gruntfile.js, I can use
grunt.option('target') to get 'client'.
But how do I get hold of parameter build before the task build starts?
Any guidance is much appreciated!
Your grunt file is basically just a function. Try adding this line to the top:
module.exports = function( grunt ) {
/*==> */ console.log(grunt.option('target'));
/*==> */ console.log(grunt.cli.tasks);
// Add your pre task code here...
Running with grunt build --target=client should give you the output:
client
[ 'build' ]
At that point, you can run any code you need to before your task is run including setting values with new dependencies.
A better way is to use grunt.task.current which has information about the currently running task, including a name property. Within a task, the context (i.e. this) is the same object. So . . .
grunt.registerTask('foo', 'Foobar all the things', function() {
console.log(grunt.task.current.name); // foo
console.log(this.name); // foo
console.log(this === grunt.task.current); // true
});
If build is an alias to other tasks and you just want to know what command was typed that led to the current task execution, I typically use process.argv[2]. If you examine process.argv, you'll see that argv[0] is node (because grunt is a node process), argv[1] is grunt, and argv[2] is the actual grunt task (followed by any params in the remainder of argv).
EDIT:
Example output from console.log(grunt.task.current) on grunt#0.4.5 from within a task (can't have a current task from not a current task).
{
nameArgs: 'server:dev',
name: 'server',
args: [],
flags: {},
async: [Function],
errorCount: [Getter],
requires: [Function],
requiresConfig: [Function],
options: [Function],
target: 'dev',
data: { options: { debugPort: 5858, cwd: 'server' } },
files: [],
filesSrc: [Getter]
}
You can use grunt.util.hooker.hook for this.
Example (part of Gruntfile.coffee):
grunt.util.hooker.hook grunt.task, (opt) ->
if grunt.task.current and grunt.task.current.nameArgs
console.log "Task to run: " + grunt.task.current.nameArgs
CMD:
C:\some_dir>grunt concat --cmp my_cmp
Task to run: concat
Running "concat:coffee" (concat) task
Task to run: concat:coffee
File "core.coffee" created.
Done, without errors.
There is also a hack that I've used to prevent certain task execution:
grunt.util.hooker.hook grunt.task, (opt) ->
if grunt.task.current and grunt.task.current.nameArgs
console.log "Task to run: " + grunt.task.current.nameArgs
if grunt.task.current.nameArgs is "<some task you don't want user to run>"
console.log "Ooooh, not <doing smth> today :("
exit() # Not valid. Don't know how to exit :), but will stop grunt anyway
CMD, when allowed:
C:\some_dir>grunt concat:coffee --cmp my_cmp
Running "concat:coffee" (concat) task
Task to run: concat:coffee
File "core.coffee" created.
Done, without errors.
CMD, when prevented:
C:\some_dir>grunt concat:coffee --cmp my_cmp
Running "concat:coffee" (concat) task
Task to run: concat:coffee
Ooooh, not concating today :(
Warning: exit is not defined Use --force to continue.
Aborted due to warnings.
Just installed latest Grunt on Ubuntu 12.04. Here is my gruntfile:
module.exports = function(grunt){
//project configuration
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
concat: {
slides : {
src : ['src/top.html', 'src/bottom.html'],
dest : ['build/index.html']
}
}
});
//enable plugins
grunt.loadNpmTasks('grunt-contrib');
grunt.registerTask('default', ['concat:slides']);
}
This creates the build/ directory fine, but gives me the output of:
Running "concat:slides" (concat) task Warning: Unable to write
"build/index.html" file (Error code: undefined). Use --force to
continue.
I tried running chmod 777 on the directory, as I thought it might have something to do with permissions, but that didn't seem to change anything.
How can I make it so Grunt will write to build/index.html?
Figured it out:
//Does not work
dest : ['build/index.html']
Works as a string, but not an array:
//Works
dest : 'build/index.html'
I changed tasks/concat.js to accept arrays for dest:
// Write the destination file.
// If f.dest is an array take the first element
var dest = ([].concat(f.dest))[0]
grunt.file.write(dest, src);
but later I decided to use the files form instead of src/dest:
files: { 'dest.js': ['a.js', 'b.js'] }