问题
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?
回答1:
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##*/}
来源:https://stackoverflow.com/questions/41366507/get-the-path-where-npm-gulp-called-from