add mkdirSyncRecursive() #7

Closed
opened 2011-10-16 04:37:15 -07:00 by neerolyte · 7 comments
neerolyte commented 2011-10-16 04:37:15 -07:00 (Migrated from github.com)

It'd be useful to have a mkdirSyncRecursive() in wrench that allowed creation of 'foo/bar' regardless of whether 'foo' existed ahead of time.

It'd be useful to have a mkdirSyncRecursive() in wrench that allowed creation of 'foo/bar' regardless of whether 'foo' existed ahead of time.
ryanmcgrath commented 2011-10-16 06:23:04 -07:00 (Migrated from github.com)

I'm certainly down to have one, but I don't have the free time at the moment to spare on getting this in (as simple as it actually may be). If anyone wants to fork/pull request, I'm happy to review and bring it in/push to NPM; otherwise, probably a week or two away.

I'm certainly down to have one, but I don't have the free time at the moment to spare on getting this in (as simple as it actually may be). If anyone wants to fork/pull request, I'm happy to review and bring it in/push to NPM; otherwise, probably a week or two away.
neerolyte commented 2011-10-17 01:55:10 -07:00 (Migrated from github.com)

I'll write a patch at some point if this becomes annoying enough :)

For now I figured it was just better to have the issue logged here, rather than randomly somewhere else (I wasn't expecting you to just write me a patch).

Cheers,
Dave

I'll write a patch at some point if this becomes annoying enough :) For now I figured it was just better to have the issue logged here, rather than randomly somewhere else (I wasn't expecting you to just write me a patch). Cheers, Dave
nherment commented 2011-10-20 01:50:37 -07:00 (Migrated from github.com)

Here is my implementation of the function. Feel free to use it however you want.

var mkDirSyncRecursive = function mkDirSyncRecursive(path, mode) {
try {
console.log("Creating dir: "+path);
fs.mkdirSync(path, mode);
console.log("...created");
} catch(err) {
if(err.code == "ENOENT") {
console.log("path does not exists yet: "+path);
var slashIdx = path.lastIndexOf("/");
if(slashIdx > 0) {
var parentPath = path.substring(0, slashIdx);
mkDirSyncRecursive(parentPath, mode);
mkDirSyncRecursive(path, mode);
} else {
console.log("No more parent directory. Ending here with error." + err);
throw err;
}
} else if(err.code == "EEXIST") {
console.log("Directory already exists.");
return;
} else {
console.log(err);
throw err;
}
}
}

Here is my implementation of the function. Feel free to use it however you want. var mkDirSyncRecursive = function mkDirSyncRecursive(path, mode) { try { console.log("Creating dir: "+path); fs.mkdirSync(path, mode); console.log("...created"); } catch(err) { if(err.code == "ENOENT") { console.log("path does not exists yet: "+path); var slashIdx = path.lastIndexOf("/"); if(slashIdx > 0) { var parentPath = path.substring(0, slashIdx); mkDirSyncRecursive(parentPath, mode); mkDirSyncRecursive(path, mode); } else { console.log("No more parent directory. Ending here with error." + err); throw err; } } else if(err.code == "EEXIST") { console.log("Directory already exists."); return; } else { console.log(err); throw err; } } }
ryanmcgrath commented 2011-10-20 01:57:21 -07:00 (Migrated from github.com)

Thanks! Will get this in later tonight.

Thanks! Will get this in later tonight.
neerolyte commented 2011-10-20 21:35:11 -07:00 (Migrated from github.com)

I missed tagging this commit with the issue number: 6e7dcba6a00ae433a86d6035827b27f86a735d74

I missed tagging this commit with the issue number: 6e7dcba6a00ae433a86d6035827b27f86a735d74
ryanmcgrath commented 2011-10-20 21:38:13 -07:00 (Migrated from github.com)

Ah, you guys rock! Will get all this into the mainline repo soon; doing an apartment shuffle at the moment that's screwing with my coding time. >_<;

Ah, you guys rock! Will get all this into the mainline repo soon; doing an apartment shuffle at the moment that's screwing with my coding time. >_<;
ryanmcgrath commented 2011-10-23 00:56:33 -07:00 (Migrated from github.com)

Hey! Thanks again for all of this; I've pulled it in and published a new version. Need to find time to write tests, augh...

Hey! Thanks again for all of this; I've pulled it in and published a new version. Need to find time to write tests, augh...
This repository is archived. You cannot comment on issues.
No milestone
No project
No assignees
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: code/wrench-js#7
No description provided.