背景

用gulp写任务来代替bash脚本的时候,偶然发现一个问题。在非home目录的时候gulp-auto全部显示为No gulpfile found。例如,

1
2
$ gulp-auto test
[10:43:52] No gulpfile found

分析

不使用alias的gulp-auto,会得到同样的结果。

1
2
$ gulp --glupfile ~/automatic/gulpfile.js -task test
[10:47:05] No gulpfile found

也就是说,是gulp本身没有找到这个gulpfile。
然后去home目录试试,是没有问题的,

1
2
3
4
5
6
7
8
9
10
$ gulp --gulpfile /home/orzorc/automatic/gulpfile.js -task test
[10:49:08] Working directory changed to ~/automatic
[10:49:08] Using gulpfile ~/automatic/gulpfile.js
[10:49:08] Starting 'default'...
[10:49:08] Finished 'default' after 39 ms
$ gulp --gulpfile /home/orzorc/automatic/test.js
[10:49:08] Using gulpfile ~/automatic/test.js
[10:49:08] Starting 'default'...
running test
[10:49:08] Finished 'default' after 83 μs

但是发现这里有一句,

Working directory changed to ~/automatic

也就是说gulp根据–gulpfile的参数改变了working directory,那么猜测应该有参数能够改变gulp的working directory,然后果然找到了

  • --gulpfile <gulpfile path> will manually set path of gulpfile. Useful if you have multiple gulpfiles. This will set the CWD to the gulpfile directory as well
  • --cwd <dir path> will manually set the CWD. The search for the gulpfile, as well as the relativity of all requires will be from here

所以,不用指定gulpfile了,直接指定cwd就行。

解决方法

替换之前的gulp-auto的alias为

1
alias gulp-auto='gulp --cwd ~/automatic -task '