我如何从命令行获得一个grunt项目的版本? [英] How do I get a grunt project's version from command line?

查看:229
本文介绍了我如何从命令行获得一个grunt项目的版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找将我的grunt项目与TeamCity持续集成服务器集成在一起。我需要一种方法来使TeamCity生成的包名称包含项目的版本号,该版本号位于项目根目录的package.json中。我似乎无法找到提供此功能的咕噜指令,并且正在使用grep。有没有从命令行得到一个grunt项目的版本的非文件的方式?

解决方案

所以在Grunt,你可以做这样的事情:

  module.exports = function(grunt){
grunt.initConfig({
pkg:grunt .file.readJSON('package.json'),
asciify:{
project:{
text:'<%= pkg.name%>',
options :{
font:'speed',
log:true
}
}
}
});
};

它会将package.json中的名称传递给任何任务,在这种情况下, grunt-asciify



关于在命令行上获取项目版本的问题,只需在项目的根目录下运行 npm version ,它将返回如下所示的内容:

  grunt-available-tasks(master)$ npm版本
{http_parser:'1.0',
node: '0.10.21',
v8:'3.14.5.9',
ares:'1.9.0-DEV',
uv:'0.10.18',
zlib: '1.2.3',
模块:'11',
openssl:'1.0.1e',
npm:'1.3.11',
'grunt-available-tasks ':'0.3.7'}

最后一个是项目名称和版本。



编辑:关于您的评论,试试这个:

  module.exports =函数(gru nt){
grunt.registerTask('version','将当前版本记录到控制台',function(){
console.log(grunt.file.readJSON('package.json')) 。版);
});
}

编辑2:所以我 不高兴因为Grunt在做任何事情时的冗长而与Grunt合作。如果你想要一些只记录版本和其他任何东西的东西,那么创建一个名为 version.js 或者你的根目录下的任何文件在 package.json 旁边。在该文件中粘贴:

  module.exports = function(){
console.log(require('。 /package.json').version);
}();

节点version.js 。我喜欢这种方法最好,因为它比Grunt更快,而且它也更小。所以,享受!

I'm looking to integrate my grunt project with TeamCity continuous integration server. I need a way to make the package name that TeamCity generates contain the project's version number, that is found in package.json in the root of the project. I can't seem to find a grunt command that provides this, and am resorting to using grep. Is there an undocumented way of getting a grunt project's version from the command line?

解决方案

So in Grunt you can do something like this:

module.exports = function(grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        asciify: {
            project: {
                text: '<%= pkg.name %>',
                options: {
                    font: 'speed',
                    log: true
                }
            }
        }
    });
};

which will pass the name in package.json to any task, in this case, grunt-asciify.

There is also something that does what you ask with regard to getting the version of the project on the command line, simply run npm version in the root of your project, which will return something like this:

grunt-available-tasks (master) $ npm version
{ http_parser: '1.0',
  node: '0.10.21',
  v8: '3.14.5.9',
  ares: '1.9.0-DEV',
  uv: '0.10.18',
  zlib: '1.2.3',
  modules: '11',
  openssl: '1.0.1e',
  npm: '1.3.11',
  'grunt-available-tasks': '0.3.7' }

The last one being the project name and version.

Edit: In regard to your comment, try this:

module.exports = function(grunt) {
    grunt.registerTask('version', 'Log the current version to the console.', function() {
        console.log(grunt.file.readJSON('package.json').version);
    });
}

Edit 2: So I wasn't happy with the Grunt one because of Grunt's verbosity when doing anything. If you want something that will just log the version and nothing else whatsoever, then create a new file called version.js or whatever in your root directory next to package.json. In that file paste this:

module.exports = function() {
    console.log(require('./package.json').version);
}();

And call it with node version.js. I like this approach the best because it's faster than the Grunt one and it's also smaller. So, enjoy!

这篇关于我如何从命令行获得一个grunt项目的版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆