得到npm gulp呼叫的路径 [英] get the path where npm gulp called from

查看:99
本文介绍了得到npm gulp呼叫的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的gulp和npm,我试图编写一个考虑到当前目录的脚本:

  gulp。 task('render',function(){
var folder = process.cwd();
console.info(我被称为:+文件夹);
// renderThisData文件夹+/data.json);
})

它给出了相同的结果:

  gulp.task('render',function(){
var folder = process.env.INIT_CWD;
console.info(我被称为:+文件夹);
// renderThisData(folder +/data.json);
})

其中我的树就像:

  root 
| --- package.json
| --- gulpfile.js
| --- folder1
| | --- data.json
| --- folder2
| | --- data.json
| --- folder3
| | --- data.json

当我从一个特定文件夹内运行脚本时,上面的脚本 - (

  D:\ root \folder1> npm run render 
我从D:\root

中找到一个解决方法,我发现是使用参数:

  npm运行渲染 -  --folder = folder1 

然后在我的代码中读取/使用param值
我想知道是否还有其他方法可以做到这一点?

解决方案

npm run 将永远从你的根目录运行(如果它没有在你的脚本中改变)。 >

您可以使用 process.env.INIT_CWD ,它是从它启动的原始cwd(在版本3.8.1中添加)如果您可以运行 gulp'task'而不是使用 npm 或通过参数 npm run render - --folder = $ {PWD ## * /}


New with gulp and npm, I am trying to write a script that takes into account the current directory:

gulp.task('render', function(){
    var folder = process.cwd();
    console.info("I am called from:" + folder);
    //renderThisData(folder + "/data.json");
})

I tried this one as well, it gives same result:

gulp.task('render', function(){
    var folder = process.env.INIT_CWD;
    console.info("I am called from:" + folder);
    //renderThisData(folder + "/data.json");
})

where my tree is like:

root
|--- package.json
|--- gulpfile.js
|--- folder1
|    |--- data.json
|--- folder2
|    |--- data.json
|--- folder3
|    |--- data.json

When I run the script from inside one particular folder, the above script gives always the path where the gulpfile is :-(

D:\root\folder1>npm run render
I am called from D:\root

One work around i found is to use params:

 npm run render -- --folder=folder1

and then read/use the param value in my code. I am wondering if there are other ways to do it?

解决方案

npm run will always run from your root directory (if it's not changed in your script).

You can use process.env.INIT_CWD which is the original cwd it was launched from (Added in version 3.8.1) if you can run gulp 'task' instead of using npm or pass the folder name by parameter npm run render -- --folder=${PWD##*/}

这篇关于得到npm gulp呼叫的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆