Gulp: working directory before changed to gulpfile.js location

Created on 12 Jun 2014  路  5Comments  路  Source: gulpjs/gulp

scenario:
~/folder/gulpfile.js
~/folder/childfolder1 // no gulpfile.js in this folder
~/folder/childfolder2 // no gulpfile.js in this folder

running gulp under one of the child folder will load ~/folder/gulpfile.js

according to GULP change log 1.2-2.4 (11/12/13)

  • chdir to gulpfile directory before loading it

therefore when gulpfile.js is loaded, process.cwd() == "~/folder"

I want to share same gulpfile.js for all child folders,
my question is , how could the gulpfile knows it originate childfolder ?

Most helpful comment

@yapcheahshen Figured it out! :) Here's how you get the initial directory from Node:

console.log process.env.INIT_CWD #the initial directory
# ~/project/child_folder
console.log process.cwd() #gulpfile.js location
# ~/project

All 5 comments

could I get the child folder in gulpfile.js ?
for example , gulp.cwd() ?

@yapcheahshen To get the cwd in node you use process.cwd()

The process will be chdir'ed to whatever you pass in --cwd so all paths will be relative to there

@yapcheahshen Figured it out! :) Here's how you get the initial directory from Node:

console.log process.env.INIT_CWD #the initial directory
# ~/project/child_folder
console.log process.cwd() #gulpfile.js location
# ~/project

@robert-blankenship : Looking for this for over two hours. You are a life saver. Thanks!

Was this page helpful?
0 / 5 - 0 ratings