javascript - Use Global Variable to Set Build Output Path in Grunt -


i have couple grunt tasks , trying share global variables across tasks , running issues.

i have written custom tasks set proper output path depending on build type. seems setting things correctly.

// set mode (local or build) grunt.registertask("setbuildtype", "set build type. either build or local", function (val) {   // grunt.log.writeln(val + " :setbuildtype val");   global.buildtype = val; });  // setoutput location grunt.registertask("setoutput", "set output folder 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 reference global.outputpath in subsequent task, , running errors.

if call grunt test command line, outputs correct path no problem.

however, if have task this: clean: { release: { src: global.outputpath } }

it throws following error: warning: cannot call method 'indexof' of undefined use --force continue.

also, constants in setoutput task set @ top of gruntfile.js

any thoughts? doing wrong here?

so, on right path. issue module exports before global variables set, undefined in subsequent tasks defined within initconfig() task.

the solution came with, although, there may better, overwrite grunt.option value.

i have optional option task --target

working solution looks this:

grunt.registertask("setoutput", "set output folder 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 task defined in initconfig() looked this:

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

feel free chime in if have better solution. otherwise, perhaps may else.


Comments