Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i get the path from where gulp was run?

Tags:

node.js

gulp

I'm building a frontend workflow with gulp where i need to be able to build multiple separate designs but they need to share some common settings.

To clarify, i have a structure like this:

  • [Project root]
    • Templates
      • Designs
        • MyDesign1
          • Assets
            • js
              • .js
            • scss
              • .scss
          • gulpfile.js
        • MyDesign2
          • Assets
            • js
              • .js
            • scss
              • .scss
          • gulpfile.js
        • [More designs can be added later]
    • .eslintrc (Shared)
    • sftp-config.json (Shared)
I'd like to be able to call gulp in templates/designs/mydesign1 or templates/designs/mydesign2 and have acess to the data inside [Project root]/sftp-config.json and [Project root]/.eslintrc and any other file that might exist outside of the folder that gulp was run from.

Is that possible?

like image 301
Bødlen Avatar asked Jun 02 '15 11:06

Bødlen


1 Answers

I found a solution to my problem, and it seems to work pretty well for my needs:

By using process.cwd() i can get the folder that gulp ran in, and then i can use path.resolve('../') to step back in my folders until i hit my project root folder.

var gulpRanInThisFolder = process.cwd();
var rootDir = path.resolve('../','../','../','../');
var designFolderName = pkg.name;
var sftpConfigPath =  path.join(rootDir, '/sftp-config.json'); 
like image 128
Bødlen Avatar answered Oct 24 '22 02:10

Bødlen