From 4cf4456635dc550df6e9dfc8b2e8665b67368453 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Tue, 3 Jul 2012 14:46:45 -0700 Subject: [PATCH] Add fs.makeTree --- spec/stdlib/fs-spec.coffee | 8 ++++++++ src/stdlib/fs.coffee | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/spec/stdlib/fs-spec.coffee b/spec/stdlib/fs-spec.coffee index a47509374..f5abc6c38 100644 --- a/spec/stdlib/fs-spec.coffee +++ b/spec/stdlib/fs-spec.coffee @@ -54,6 +54,14 @@ describe "fs", -> it "returns an empty string for paths without an extension", -> expect(fs.extension("a/b.not-extension/a-dir")).toBe '' + describe "makeTree(path)", -> + beforeEach -> + fs.remove("/tmp/a") if fs.exists("/tmp/a") + + it "creates all directories in path including any missing parent directories", -> + fs.makeTree("/tmp/a/b/c") + expect(fs.exists("/tmp/a/b/c")).toBeTruthy() + describe ".traverseTree(path, fn)", -> fixturesDir = null diff --git a/src/stdlib/fs.coffee b/src/stdlib/fs.coffee index 2763e9b41..4e1516fb9 100644 --- a/src/stdlib/fs.coffee +++ b/src/stdlib/fs.coffee @@ -93,6 +93,14 @@ module.exports = makeDirectory: (path) -> $native.makeDirectory(path) + # Creates the directory specified by "path" including any missing parent + # directories. + makeTree: (path) -> + return unless path + if not @exists(path) + @makeTree(@directory(path)) + @makeDirectory(path) + traverseTree: (rootPath, fn) -> recurse = null prune = -> recurse = false