Skip to content

Instantly share code, notes, and snippets.

@Diullei
Created June 2, 2015 20:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Diullei/cbb6e7b9b10235f7c483 to your computer and use it in GitHub Desktop.
Save Diullei/cbb6e7b9b10235f7c483 to your computer and use it in GitHub Desktop.
This code will generate a task.json file from your Gruntfile.js and will allow you to run grunt tasks from inside VSCode
/* ****************************************************************************
This code will generate a task.json file from your Gruntfile.js and will
allow you to run grunt tasks from inside VSCode
****************************************************************************
1) Put this file in the same directory of your gruntfile.js (NOTE: must be
the app root directory) run the following command line:
node gt.js
The '.settings/task.json' file will be generated.
2) Goto VSCode press Ctrl+Shift+P and type:
Run Tasks
select it and press Enter.
This will list all available tasks from your Gruntfile.
TIP: You can execute this file always you update your Gruntfile.js
************************************************************************* */
var path = require('path');
var fs = require('fs');
var mkdirSync = function (path) {
try {
fs.mkdirSync(path);
} catch(e) {
if ( e.code != 'EEXIST' ) throw e;
}
}
function getAllTasks(gruntFilePath) {
var data = fs.readFileSync(gruntFilePath);
var str = data.toString();
var regx = /grunt[|\n|\r|\s|\t]*\.[|\n|\r|\s|\t]*registerTask\([|\n|\r|\s|\t]*['|"]([a-zA-Z0-9:_-]+)['|"]/igm;
var match = null;
var result = [];
while (match = regx.exec(str)) {
result.push(match[1]);
}
return result;
}
var file = './Gruntfile.js';
if (fs.existsSync(file)) {
var task = {
version: "0.1.0",
command: "grunt",
isShellCommand: true,
args: [],
tasks: []
};
var gruntTasks = getAllTasks(file);
gruntTasks.forEach(function (gt) {
task.tasks.push({
taskName: gt,
args: [],
isBuildCommand: true,
problemMatcher: []
});
});
mkdirSync('.settings');
var generate = function() {
var taskJson = JSON.stringify(task, null, 4);
fs.writeFile(".settings/tasks.json", taskJson, function(err) {
if(err) {
console.log(err);
} else {
console.log("The file was .settings/tasks.json saved!");
}
});
}
if(!fs.existsSync('.settings/tasks.json')) {
generate();
} else {
if(process.argv[2] != '--force') {
console.log('\033[31mThere is already a file named .settings/tasks.json. Use --fore to overwrite this file.\033[0m')
} else {
generate();
}
}
} else {
console.log('\033[31mGruntfile.js not found\033[0m');
process.exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment