Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created October 25, 2011 22:03
Show Gist options
  • Save Raynos/1314493 to your computer and use it in GitHub Desktop.
Save Raynos/1314493 to your computer and use it in GitHub Desktop.
var stack = new Stack(
function (path) {
// default return value
this.return = path;
// after three actions
var cb = after(3, (function (a,b,c) {
// if all succeed go next
if (a && b && c) {
this.next();
} else {
// short circuit to end
this.floor();
}
}).bind(this));
path.exists(path + ".js", function (exists) {
cb (!exists);
});
path.exists(path, cb);
fs.stat(path, function (err, stats) {
cb(stats.isDirectory());
});
},
function (path) {
// check path exist
path.exists(path + "/main.js", function (exists) {
if (exists) {
// set return and short circuit
this.return = path + "/main":
this.floor();
} else {
this.next();
}
});
},
function (path) {
path.exists(path + "/index.js", function (exists) {
if (exists) {
this.return = path + "/index";
this.floor();
}
});
}
);
function extendPath(path, cb) {
// flow path through stack
stack.handle({
data: [path],
floor: function () {
// at end of stack send return into cb.
cb(this.return);
}
});
}
function _extendPath(_, path) {
// test three async conditions
if (!_exists(_, path + ".js") && _exists(_, path) &&
fs.stat(path, _).isDirectory()) {
// if either exists return it
if (_exists(_, path + "/main.js"))
return path + "/main";
else if (_exists(_, path + "/index.js"))
return path + "/index";
}
// just return path
return path;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment