Grunt-shell saves output command as a variable

I use Grunt and Grunt-shell to create / deploy my Javascript project.

I want to get the last git -commit number and save it as a variable, but I can not determine how to do it. I tried to have a callback and set a global variable. This variable is used inside the function, but not from inside another block.

grunt.initConfig({ ... shell: { getGitCommitNo: { command: 'git rev-parse --short HEAD', options: { callback: function (err, stdout, stderr, cb) { global['gitCommitNo'] = stdout; grunt.log.ok(global.gitCommitNo); cb(); } } }, philTest: { command: 'echo Git Commit No: ' + global.gitCommitNo }, ... } 

Output:

 >> Starting deployment process for version 1.1 in dev environment Running "shell:getGitCommitNo" (shell) task bfc82a9 >> bfc82a9 Running "shell:printTest" (shell) task Git Commit No: undefined Done, without errors. 

Can anyone suggest how I can save the command line output for a variable that can be used?

+6
source share
1 answer

Found that I can actually do this using the config variable (instead of the global one) inside the callback. (The note below the line also deletes the new line).

 grunt.config.set('gitCommitNo', stdout.replace('\n', '')); 

This can then be obtained using:

 <%=gitCommitNo%> 
+10
source

Source: https://habr.com/ru/post/982256/


All Articles