Open OscarGodson opened 11 years ago
hmm, isn't __dirname
supposed to refer to the current file's directory?
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);
});
});
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()
?
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?
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.)
Yeah, you want process.cwd()
for these things @OscarGodson. That will give you the current working directory of the node process.
Intuitively, while in the root and running say
jake build
,__dirname
would be the in the same place. Right now__dirname
ispath/to/project/jakelib
instead ofpath/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
.