Babel 在 git 钩子中不工作

Babel not working in git hook

我想在服务器上推送后对一些项目文件进行babel。 因此我使用了一个 post-receive 钩子来调用下面的行

/usr/bin/gulp babel --branch $branch --path $i

其中 $branch 是 devel 并且 $i 是 server/server.js

这是我的 gulp 文件:

var gulp = require('gulp'),
    gprint = require('gulp-print'),
    notify = require('gulp-notify'),
    babel = require('gulp-babel'),
    gWatch = require('gulp-watch');
var argv = require('yargs').argv;


gulp.task('default', ['watch-js', 'watch-all']);

// run with gulp babel --branch devel --path server/server.js
gulp.task('babel', function() {
    console.log('argv path: ',argv.path);
    console.log('argv branch: ',argv.branch);
    if (argv.path.indexOf('server/') == 0) {
        return gulp.src(argv.path, { base: './' })
                .pipe(babel()) 
                .pipe(gulp.dest('../wikaike-'+argv.branch+'-deployed'))
                .pipe(gprint(function(filePath){ return "File processed: " + filePath; }));
    } else {
        return;   
    }
});

如果我运行这样的命令我得到这个输出:

[18:35:11] Using gulpfile /var/www/wikaike-devel/gulpfile.js
[18:35:11] Starting 'babel'...
argv path:  server/server.js
argv branch:  devel
[18:35:15] File processed: ../wikaike-devel-deployed/server/server.js
[18:35:15] Finished 'babel' after 4.36 s

但如果我推动我得到这个:

remote: [18:47:20] Using gulpfile /var/www/wikaike-devel/gulpfile.js
remote: [18:47:20] Starting 'babel'...
remote: argv path:  server/server.js
remote: argv branch:  devel
remote: abort: 0��� ������[=14=]������
remote: 
remote: ==== Stack trace ============================================
remote: 
remote: Security context: 0x3733462d <JS Object>#0#
remote:     1: /* anonymous */ [/var/www/wikaike-devel/node_modules/is-utf8/is-utf8.js:~2] (this=0x373346d5 <JS Global Object>#1#,bytes=0x57528e71 <a Buffer>#2#)
remote:     2: /* anonymous */(aka /* anonymous */) [/var/www/wikaike-devel/node_modules/vinyl-fs/node_modules/strip-bom/index.js:9] (this=0x37308091 <undefined>,arg=0x57528e71 <a Buffer>#2#)
remote:     3: /* anonymous */(aka /* anonymous */) [/var/www/wikaike-devel/node_modules/vinyl-fs/lib/src/getContents/bufferFile.js:11] (this=0x37308091 <undefined>,err=0x37308081 <null>,data=0x57528e71 <a Buffer>#2#)
remote:     4: /* anonymous */ [fs.js:266] (this=0x373346d5 <JS Global Object>#1#,er=0x37308081 <null>)
remote:     5: /* anonymous */ [/var/www/wikaike-devel/node_modules/vinyl-fs/node_modules/graceful-fs/graceful-fs.js:102] (this=0x373346d5 <JS Global Object>#1#,er=0x37308081 <null>)
remote:     7: oncomplete [fs.js:107] (this=0x575292e9 <an Object>#3#)
remote:     8: arguments adaptor frame: 1->0
remote: 
remote: ==== Details ================================================
remote: 
remote: [1]: /* anonymous */ [/var/www/wikaike-devel/node_modules/is-utf8/is-utf8.js:~2] (this=0x373346d5 <JS Global Object>#1#,bytes=0x57528e71 <a Buffer>#2#) {
remote:   // stack-allocated locals
remote:   var i = 11629
remote:   // expression stack (top to bottom)
remote:   [02] : 0
remote:   [01] : 2019638
remote: --------- s o u r c e   c o d e ---------
remote: function (bytes)?{?    var i = 0;?    while(i < bytes.length)?    {?        if(     (// ASCII?                    bytes[i] == 0x09 ||?                    bytes[i] == 0x0A ||?                    bytes[i] == 0x0D ||?                    (0x20 <= bytes[i] && bytes[i] <= 0x7E)?                )?          ) {?    ...
remote: 
remote: -----------------------------------------
remote: }
remote: 
remote: [2]: /* anonymous */(aka /* anonymous */) [/var/www/wikaike-devel/node_modules/vinyl-fs/node_modules/strip-bom/index.js:9] (this=0x37308091 <undefined>,arg=0x57528e71 <a Buffer>#2#) {
remote:   // expression stack (top to bottom)
remote:   [02] : 0x57528e71 <a Buffer>#2#
remote:   [01] : 0x373346d5 <JS Global Object>#1#
remote:   [00] : 0x44fecad1 <JS Function>#4#
remote: --------- s o u r c e   c o d e ---------
remote: function (arg) {??if (typeof arg === 'string') {???return arg.replace(/^\ufeff/g, '');??}???if (Buffer.isBuffer(arg) && isUtf8(arg) &&???arg[0] === 0xef && arg[1] === 0xbb && arg[2] === 0xbf) {???return arg.slice(3);??}???return arg;?}
remote: -----------------------------------------
remote: }
remote: 
remote: [3]: /* anonymous */(aka /* anonymous */) [/var/www/wikaike-devel/node_modules/vinyl-fs/lib/src/getContents/bufferFile.js:11] (this=0x37308091 <undefined>,err=0x37308081 <null>,data=0x57528e71 <a Buffer>#2#) {
remote:   // expression stack (top to bottom)
remote:   [03] : 0x57528e71 <a Buffer>#2#
remote:   [02] : 0x37308091 <undefined>
remote:   [01] : 0x44fecaf5 <JS Function>#5#
remote:   [00] : 0x57524f79 <a File>#6#
remote: --------- s o u r c e   c o d e ---------
remote: function (err, data) {?    if (err) {?      return cb(err);?    }?    file.contents = stripBom(data);?    cb(null, file);?  }
remote: -----------------------------------------
remote: }
remote: 
remote: [4]: /* anonymous */ [fs.js:266] (this=0x373346d5 <JS Global Object>#1#,er=0x37308081 <null>) {
remote:   // expression stack (top to bottom)
remote:   [03] : 0x57528e71 <a Buffer>#2#
remote:   [02] : 0x37308081 <null>
remote:   [01] : 0x37308091 <undefined>
remote:   [00] : 0x57527ca9 <JS Function>#7#
remote: --------- s o u r c e   c o d e ---------
remote: function (er) {?      if (size === 0) {?        // collected the data into the buffers list.?        buffer = Buffer.concat(buffers, pos);?      } else if (pos < size) {?        buffer = buffer.slice(0, pos);?      }??      if (encoding) buffer = buffer.toString(encoding);?      return callback(er, buffer);?...
remote: 
remote: -----------------------------------------
remote: }
remote: 
remote: [5]: /* anonymous */ [/var/www/wikaike-devel/node_modules/vinyl-fs/node_modules/graceful-fs/graceful-fs.js:102] (this=0x373346d5 <JS Global Object>#1#,er=0x37308081 <null>) {
remote:   // expression stack (top to bottom)
remote:   [02] : 0x37308081 <null>
remote:   [01] : 0x373346d5 <JS Global Object>#1#
remote:   [00] : 0x57529081 <JS Function>#8#
remote: --------- s o u r c e   c o d e ---------
remote: function (er) {?    onclose()?    cb(er)?  }
remote: ---------------/var/opt/gitlab/git-data/repositories/root/wikaike.git/hooks/deploy-hook: line 42: 26701 Bus error               /usr/bin/gulp babel --branch $branch --path $i

简而言之:如果开始使用我的 post-receive 钩子,Babel 将无法工作。但是脚本在服务器上运行(从 git 用户开始)

知道发生了什么吗?

要检查的两个要素(当命令在命令行中工作但不作为 Git 挂钩时)是:

  • 执行脚本的用户
  • 环境变量(最值得注意的是 $PATH

如果 $PATH 不同,可能是某些工具缺失,或者与预期的版本不同。