Skip to content
Browse files

Add in recursive ensureDirectory function, to create complex paths on…

… platforms that don't support it by default.

Add test to ensure that complex output paths are created properly using ensureDirectory
Export ensureDirectory with Docco
  • Loading branch information...
1 parent 9fe9d5f commit c053352782a1342bc989b2c69139c9430e2ad360 @justindujardin justindujardin committed Oct 28, 2012
Showing with 49 additions and 14 deletions.
  1. +22 −4 lib/docco.js
  2. +16 −9 src/docco.coffee
  3. +11 −1 test/tests.coffee
View
26 lib/docco.js
@@ -162,9 +162,26 @@
return languages[path.extname(source)];
};
- ensureDirectory = function(dir, callback) {
- return exec("mkdir -p " + dir, function() {
- return callback();
+ ensureDirectory = function(dir, cb, made) {
+ var mode;
+ if (made == null) {
+ made = null;
+ }
+ mode = parseInt('0777', 8);
+ return fs.mkdir(dir, mode, function(er) {
+ if (!er) {
+ return cb(null, made || dir);
+ }
+ if (er.code === 'ENOENT') {
+ return ensureDirectory(path.dirname(dir), function(er, made) {
+ if (er) {
+ return cb(er, made);
+ } else {
+ return ensureDirectory(dir, cb, made);
+ }
+ });
+ }
+ return cb(er, made);
});
};
@@ -276,7 +293,8 @@
resolveSource: resolveSource,
version: version,
defaults: defaults,
- languages: languages
+ languages: languages,
+ ensureDirectory: ensureDirectory
};
for (key in _ref1) {
value = _ref1[key];
View
25 src/docco.coffee
@@ -216,8 +216,14 @@ for ext, l of languages
getLanguage = (source) -> languages[path.extname(source)]
# Ensure that the destination directory exists.
-ensureDirectory = (dir, callback) ->
- exec "mkdir -p #{dir}", -> callback()
+ensureDirectory = (dir, cb, made=null) ->
+ mode = parseInt '0777', 8
+ fs.mkdir dir, mode, (er) ->
+ return cb null, made || dir if not er
+ if er.code == 'ENOENT'
+ return ensureDirectory path.dirname(dir), (er, made) ->
+ if er then cb er, made else ensureDirectory dir, cb, made
+ cb er, made
# Micro-templating, originally by John Resig, borrowed by way of
# [Underscore.js](http://documentcloud.github.com/underscore/).
@@ -321,11 +327,12 @@ resolveSource = (source) ->
# Information about docco, and functions for programatic usage.
exports[key] = value for key, value of {
- run : run
- document : document
- parse : parse
- resolveSource : resolveSource
- version : version
- defaults : defaults
- languages : languages
+ run : run
+ document : document
+ parse : parse
+ resolveSource : resolveSource
+ version : version
+ defaults : defaults
+ languages : languages
+ ensureDirectory: ensureDirectory
}
View
12 test/tests.coffee
@@ -83,7 +83,7 @@ test "single line comment parsing", ->
# *Kick off the first language test*
testNextLanguage languageKeys.slice()
-
+
# **URL references should resolve across sections**
#
# Resolves [Issue 100](https://github.com/jashkenas/docco/issues/100)
@@ -104,3 +104,13 @@ test "url references", ->
contents = fs.readFileSync(outFile).toString()
count = contents.match ///<a\shref="http://www.google.com">Google</a>///g
eq count.length, 2, "find expected (2) resolved url references"
+# **Paths should be recursively created if needed**
+#
+# ensureDirectory should properly create complex output paths.
+test "create complex paths that do not exist", ->
+ outputPath = path.join dataPath, 'complex/path/that/doesnt/exist'
+ exec "rm -rf #{outputPath}", ->
+ Docco.ensureDirectory outputPath, ->
+ equal fs.existsSync(outputPath), true, 'created output path'
+ stat = fs.statSync outputPath
+ equal stat.isDirectory(), true, "target is directory"

0 comments on commit c053352

Please sign in to comment.
Something went wrong with that request. Please try again.