jakejs / jake

JavaScript build tool, similar to Make or Rake. Built to work with Node.js.
http://jakejs.com
Apache License 2.0
1.97k stars 190 forks source link

jakelib commands are run from that directory #223

Open OscarGodson opened 11 years ago

OscarGodson commented 11 years ago

Intuitively, while in the root and running say jake build, __dirname would be the in the same place. Right now __dirname is path/to/project/jakelib instead of path/to/project. This means everywhere I have a path setup (atomify-js, browserify, write/readFile, etc) I have to do __dirname + '/../file' to get to the root rather than simply doing ./file.

ben-ng commented 11 years ago

hmm, isn't __dirname supposed to refer to the current file's directory?

OscarGodson commented 11 years ago

Yep. That's what was easiest to fix the path issues AFAIK. Here's a ticket I made somewhere else

https://github.com/Techwraith/atomify/issues/2

Only problem is I started having this issue doing other things like (pwd == __dirname + '/../', just aliased it). I was getting "file doesn't exist" errors in my code below. After changing every path to have the __dirname fix it all started working.

    atomifyjs({
      entry: pwd + 'assets/js/entry.js',
    }, function (err, bundle) {
      if (err) throw err;
      fs.writeFile(pwd + '.tmp/public/js/piggy.js', bundle, function (err) {
        if (err) throw err;
        var result = uglify.minify(pwd + ".tmp/public/js/piggy.js", {outSourceMap: "piggy.js.map"});
        fs.writeFile(pwd + '.tmp/public/js/piggy.min.js', result.code, checkComplete);
        fs.writeFile(pwd + '.tmp/public/js/piggy.js.map', result.map, checkComplete);
      });
    });
ben-ng commented 11 years ago

What i meant is, i think that __dirname pointing to the jakelib dir is expected behavior.

What you seem to want it to do is process.cwd()?

OscarGodson commented 11 years ago

In a project that is /my/project that has /my/project/jakelib

When I'm working on /my/project/jakelib/foo.jake and run jake foo if I have a file path inside like ./bar I expect it to look up /my/project/bar not /my/project/jakelib/bar.

Does that make sense?

mde commented 11 years ago

fs operations are relative to whatever directory the process is running in. From the docs:

"Relative path to filename can be used, remember however that this path will be relative to process.cwd()."

Here's a minimal example. I created a test directory with a jakelib containing a 'zerpderp.jake' file with one defined task. All it does is open a file (itself, in this case), and print out the current working dir, and the contents of the file. You can see it has to be opened as "./.jakelib/zerpderp.jake" because the process is running in "test, " right above "jakelib."

mdes-mbp-2:test mde$ jake -T
jake zerpderp:asdf   # asdf task
mdes-mbp-2:test mde$ cat jakelib/zerpderp.jake
var fs = require('fs');

namespace('zerpderp', function () {

  desc('asdf task');
  task('asdf', function () {
    console.log(process.cwd());
    var stuff = fs.readFileSync('./jakelib/zerpderp.jake').toString();
    console.log(stuff);
  }); 

}); 
mdes-mbp-2:test mde$ jake zerpderp:asdf
/Users/mde/test
var fs = require('fs');

namespace('zerpderp', function () {

  desc('asdf task');
  task('asdf', function () {
    console.log(process.cwd());
    var stuff = fs.readFileSync('./jakelib/zerpderp.jake').toString();
    console.log(stuff);
  }); 

}); 

I'm not sure where your task is running that the current working dir is not your project dir, and I'm not sure why you'd need dirname, but even then, as you've noticed, it points to the directory name of the source script for the currently running code. (This is true even if you mix functions from one required file to another -- dirname and __filename will point to the original one, which is kind of confusing.)

techwraith commented 11 years ago

Yeah, you want process.cwd() for these things @OscarGodson. That will give you the current working directory of the node process.