私にはいくつかのうなり声のタスクがあり、それらのタスク間でグローバル変数を共有しようとしていますが、問題が発生しています。
ビルドタイプに応じて適切な出力パスを設定するカスタムタスクをいくつか作成しました。これは正しく設定されているようです。
// 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);
});
そのため、後続のタスクでglobal.outputPathを参照しようとすると、エラーが発生します。
grunt test
コマンドラインから呼び出すと、問題なく正しいパスが出力されます。
ただし、次のようなタスクがある場合:clean:{release:{src:global.outputPath}}
次のエラーがスローされます。
Warning: Cannot call method 'indexOf' of undefined Use --force to continue.
また、setOutputタスクの定数は、Gruntfile.jsの先頭に設定されています。
何かご意見は?私はここで何か間違ったことをしていますか?