gpt4 book ai didi

javascript - Grunt - 获取当前调用文件夹,而不是 gruntfile 当前文件夹

转载 作者:搜寻专家 更新时间:2023-10-31 23:00:42 25 4
gpt4 key购买 nike

如果我在某个文件夹/foo 中安装了 Grunt,但我当前的文件夹是/foo/bar/baz,并且我从当前文件夹中运行“grunt sometask”,我如何才能获得 Grunt(或 NodeJS) ) 来确定我当前的路径?也就是说,如何以编程方式获取调用 grunt 时所在的文件夹?

当我使用 process.cwd() 时,我得到了 gruntfile 的路径,即“foo”,这不是我想要的。

我不必专门在 Grunt 中执行此操作,任何基于 nodejs 的解决方案都可以。

最佳答案

根据source code :

By default, all file paths are relative to the Gruntfile

然后,this line of code展示了 grunt 如何实际将当前目录更改为 Gruntfile 的路径:

process.chdir(grunt.option('base') || path.dirname(gruntfile));

但是,选项 --base 就是为此而存在的。请参阅文档:http://gruntjs.com/api/grunt.file

如果您不需要从内部 Gruntfile 执行此操作,只需运行捕获process.cwd() 的脚本,然后运行execs 咕噜声。

参见: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);
});

关于javascript - Grunt - 获取当前调用文件夹,而不是 gruntfile 当前文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28755625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com