尝试获取grunt中的git commit ID

Jus*_*808 2 git gruntjs

我想必须将git commit ID作为我创建的zip文件的一部分。我正在尝试使用,grunt-git-rev-parse但没有运气。我的zip文件name-.zip不是name-3A5BC3.zip。如何将git commit ID放入我的文件名?

module.exports = function(grunt) {
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    "git-rev-parse": {
      options: {
        prop: 'git-revision',
        number: 6
      }
    },

    jshint: { 
      options: {
        curly: true,
        eqeqeq: true,
        eqnull: true,
        browser: true,
        globals: {
          jQuery: true
        },
      },
      all: ['gruntfile.js', 'public/javascripts/**/*.js'],
    },

    bower: {
      install: {
        options: {
          install: true,
          copy: false
        }
      }
    },

    jade: {
      compile: {
        options: {
          data: {
            debug: false
          }
        },
        files: {
          "build/html/index.html": ["src/jade/index.jade"],
          "build/html/login.html": ["src/jade/login.jade"]
        }
      }
    },

    stylus: {
      compile: {
        options: {
          urlfunc: 'embedurl'
        },
        files: {
          'build/css/site.css': ['src/stylus/site.styl']
        }
      }
    },

    uglify: {
      options: {
        mangle: false,
        banner: '/*! <%= pkg.name %> <%= grunt.template.today("dd-mm-yyyy") %> */\n'
      },
      dist: {
        files: {
          'build/js/<%= pkg.name %>.min.js': ['public/javascripts/**/*.js',
            'bower_components/angulartics/src/angulartics.js',
            'bower_components/angulartics/src/angulartics-google-analytics.js']
        }
      }
    },

    compress: {
      main: {
        options: {
          archive: "<%= pkg.name %>-<%= grunt.config.get('git-revision') %>.zip",
          mode: 'zip',
          pretty: true
        },
        files: [
          {expand: true, cwd: 'build/', src: ['**/*']}
        ]
      }
    }
  });


  grunt.loadNpmTasks('grunt-bower-task');
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-jade');
  grunt.loadNpmTasks('grunt-contrib-stylus');
  grunt.loadNpmTasks('grunt-contrib-compress');
  grunt.loadNpmTasks('grunt-git-rev-parse');


  grunt.registerTask('test',  ['jshint']);
  grunt.registerTask('build', ['bower', 'jade', 'stylus', 'uglify']);
  grunt.registerTask('pkg', ['git-rev-parse', 'compress:main']);
};
Run Code Online (Sandbox Code Playgroud)

Ben*_*Ben 5

您错过了任务的多任务目标grunt-git-rev-parse,因此它从未运行过。:-)

"git-rev-parse": {
  build: {
    options: {
      prop: 'git-revision',
      number: 6
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这不是您的错,因为有关此特定插件的文档尚不清楚,但是请始终确保无需指定多任务目标即可获得输出。如果不能,则添加一个通常可以解决问题(您始终可以检查源代码以确保它是一个多任务或单个任务)。