【发布时间】:2016-04-11 17:28:38
【问题描述】:
我正在使用 gulp 构建我的 angularjs 应用程序,并希望以某种方式将最新的 git commit hash 注入我的 angular 应用程序,以便在调试生产问题时使用它。
即使只是将它作为全局变量注入到我的 index.html 模板中,那也很棒。
谁能推荐一个好的技术和插件来实现这一点?
【问题讨论】:
标签: angularjs node.js git gulp fs
我正在使用 gulp 构建我的 angularjs 应用程序,并希望以某种方式将最新的 git commit hash 注入我的 angular 应用程序,以便在调试生产问题时使用它。
即使只是将它作为全局变量注入到我的 index.html 模板中,那也很棒。
谁能推荐一个好的技术和插件来实现这一点?
【问题讨论】:
标签: angularjs node.js git gulp fs
我最终使用了 git-rev-sync 和 gulp-replace 并将 {{git}} 放入我的 index.html 源文件中。
var git = require('git-rev-sync');
return gulp.src(pathConfig.src + 'index.html')
.pipe($.replace('{{git}}', git.long()))
.pipe(gulp.dest(pathConfig.dest + 'index.html'));
【讨论】:
注入最新的git commit hash
你可以这样做:
git 在.git/refs/heads/<branch name> 中存储最新的提交哈希。
使用以下代码读取文件的此内容,然后将其放在您需要的任何地方
# load fs for reading /writing files
fs = require("fs"),
// define your task
gulp.task('doSometing', function() {
return gulp.src(dirs.src + '/templates/*.html')
// read the SHA-1 of the latest commit
.pipe(fs.readFile("path/to/.git/refs/heads/<branch name>",
// Default encoding for fs is binary so we have to set it to UTF-8
"utf-8",
// Process the data you have read (SHA-1)
function(err, _data) {
//do something with your data
})
.pipe(gulp.dest('destination/path));
});
【讨论】:
为那些不使用 jQuery 的人跟进 @the-a-train 的回答。
var git = require('git-rev-sync');
var replace = require('git-replace');
return gulp.src(pathConfig.src + 'index.html')
.pipe(replace('{{git}}', git.long()))
.pipe(gulp.dest(pathConfig.dest + 'index.html'));
【讨论】: