Grunt - 获取当前调用文件夹,而不是gruntfile当前文件夹
如果我在某个文件夹/ foo中安装了Grunt,但是当前的文件夹是/ foo / bar / baz,并且我在当前文件夹中运行了grunt sometask,我该如何获得Grunt(或NodeJS) )来确定我目前的路径?也就是说,如何以编程方式获取当我调用grunt时所在的文件夹?
If I have Grunt installed in some folder /foo, but my current folder is /foo/bar/baz, and I run "grunt sometask" from within my current folder, how can I get Grunt (or NodeJS for that matter) to determine my current path? That is to say, how can I programmatically GET the folder I was in when I called grunt?
当我使用process.cwd()时,得到gruntfile,即foo,这不是我想要的。
When I use process.cwd(), I get the path of the gruntfile, ie, "foo", which is not what I want.
我不需要在Grunt中专门做这件事,任何基于nodejs的解决方案都可以工作。
I don't have to do this in Grunt specifically, any nodejs-based solution would work.
根据源代码:
According to the source code:
默认情况下,所有文件路径是相对于Gruntfile
By default, all file paths are relative to the Gruntfile
而且,voilá,这行代码显示了grunt实际上是如何将当前目录更改为Gruntfile的路径:
And, voilá, this line of code shows how grunt actually changes the current directory to the path of the Gruntfile:
process.chdir(grunt.option('base') || path.dirname(gruntfile));
然而,选项 - base
是就是这样。查看文档: http://gruntjs.com/api/grunt.file
However, option --base
is there for just that. See docs: http://gruntjs.com/api/grunt.file
如果您不需要从 Gruntfile内执行操作,只需运行一个脚本来捕获 process.cwd()
然后
If you don't need to do it from inside the Gruntfile, simply run a script that captures the process.cwd()
and then execs
grunt.
请参阅: https://www.npmjs.com/package/exec
var exec = require('exec');
process.cwd(); // Will have your current path
exec(['grunt', 'mytask'], function(err, out, code) {
if (err instanceof Error)
throw err;
process.stderr.write(err);
process.stdout.write(out);
process.exit(code);
});