Use a global variable to set the output path to Grunt

I have several grunt tasks, and I'm trying to use global variables in these tasks, and I'm having problems.

I wrote some custom tasks that set the correct output path depending on the type of assembly. This seems to be correct.

// Set Mode (local or build) grunt.registerTask("setBuildType", "Set the build type. Either build or local", function (val) { // grunt.log.writeln(val + " :setBuildType val"); global.buildType = val; }); // SetOutput location grunt.registerTask("setOutput", "Set the output folder for the build.", function () { if (global.buildType === "tfs") { global.outputPath = MACHINE_PATH; } if (global.buildType === "local") { global.outputPath = LOCAL_PATH; } if (global.buildType === "release") { global.outputPath = RELEASE_PATH; } if (grunt.option("target")) { global.outputPath = grunt.option("target"); } grunt.log.writeln("Output folder: " + global.outputPath); }); grunt.registerTask("globalReadout", function () { grunt.log.writeln(global.outputPath); }); 

So, I'm trying to reference global.outputPath in the next task and errors.

If I call grunt test from the command line, it gives the correct path without problems.

However, if I have such a task: clean: {release: {src: global.outputPath}}

It throws the following error: Warning: Cannot call method 'indexOf' of undefined Use --force to continue.

Also, my constants in the setOutput task are set at the top of my Gruntfile.js

Any thoughts? Am I something wrong here?

+6
source share
3 answers

So, I was on the right track. The problem is that the module is exported before these global variables are set, so they are all undefined in subsequent tasks defined in the initConfig () task.

The solution I came up with, although it might be better, is to overwrite the value of grunt.option.

I have an option for my task --target

Working solution

as follows:

 grunt.registerTask("setOutput", "Set the output folder for the build.", function () { if (global.buildType === "tfs") { global.outputPath = MACHINE_PATH; } if (global.buildType === "local") { global.outputPath = LOCAL_PATH; } if (global.buildType === "release") { global.outputPath = RELEASE_PATH; } if (grunt.option("target")) { global.outputPath = grunt.option("target"); } grunt.option("target", global.outputPath); grunt.log.writeln("Output path: " + grunt.option("target")); }); 

And the task defined in initConfig () looks like this:

 clean: { build: { src: ["<%= grunt.option(\"target\") %>"] } } 

Feel free to call if you have a better solution. Otherwise, maybe this can help someone else.

+13
source

I have a way to do this, which allows you to specify the output path using values ​​like --dev . While it works very well, I really like it. Thought I'd share it as someone else might like.

  # Enum for target switching behavior TARGETS = dev: 'dev' dist: 'dist' # Configurable paths and globs buildConfig = dist: "dist" dev: '.devServer' timestamp: grunt.template.today('mm-dd_HHMM') grunt.initConfig cfg: buildConfig cssmin: crunch: options: report: 'min' files: "<%= grunt.option('target') %>/all-min.css": "/**/*.css" # Set the output path for built files. # Most tasks will key off this so it is a prerequisite setPath = -> if grunt.option 'dev' grunt.option 'target', buildConfig.dev else if grunt.option 'dist' grunt.option 'target', "#{buildConfig.dist}/#{buildConfig.timestamp}" else # Default path grunt.option 'target', buildConfig.dev grunt.log.writeln "Output path set to: `#{grunt.option 'target'}`" grunt.log.writeln "Possible targets:" grunt.log.writeln target for target of TARGETS setPath() 

With this setting, you can run commands like:

 grunt cssmin --dist #sent to dist target grunt cssmin --dev #sent to dev target grunt cssmin --dev #sent to default target (dev) 
+4
source

This is an old question, I just thought about throwing my 5 cents.

If you need a configuration variable accessible from any task, just define it in your main (the one you always load) the configuration file as follows:

 module.exports = function(grunt) { // // Common project configuration // var config = { pkg: grunt.file.readJSON('package.json'), options: // for 'project' { dist: { outputPath: '<%= process.cwd() %>/lib', }, dev: { outputPath: '<%= process.cwd() %>/build', }, }, } grunt.config.merge( config ) } 

Then you can simply access the following values:

  • in the configuration file

... my_thingie: [ ends_up_here: '<%= options.dev.outputPath %>/bundle', ], ...

  • in tasks

// as raw value grunt.config.data.options.dist.outputPath // after (eventual) templates have been processed grunt.config('options.dist.outputPath')

I used the options key here to be in agreement with the convention, but you can use anything if you don’t remember to not register a task named 'options' or whatever you used for the key :)

0
source

All Articles