Chromium Code Reviews| Index: frog/file_system_node.dart |
| diff --git a/frog/file_system_node.dart b/frog/file_system_node.dart |
| index 33c2be162d9a2fe736e1c70c0b19af00e3ba5223..dc6df3ec23ff18212bfdcf2ba87747ba8aee3047 100644 |
| --- a/frog/file_system_node.dart |
| +++ b/frog/file_system_node.dart |
| @@ -20,4 +20,64 @@ class NodeFileSystem implements FileSystem { |
| bool fileExists(String filename) { |
| return path.existsSync(filename); |
| } |
| + |
| + void createDirectory(String path, [bool recursive = false]) { |
| + if (!recursive) { |
| + fs.mkdirSync(path); |
| + return; |
| + } |
| + |
| + // See how much of the path already exists and how much we need to create. |
| + var parts = path.split('/'); |
|
Jacob
2011/11/16 19:59:30
var --> final
Bob Nystrom
2011/11/17 20:44:21
Done.
|
| + var existing = '.'; |
| + var part; |
| + for (part = 0; part < parts.length; part++) { |
|
Jacob
2011/11/16 19:59:30
for (part in parts)
parts[part] --> part
Bob Nystrom
2011/11/17 20:44:21
I'm using that same `part` index below to continue
|
| + var subpath = joinPaths(existing, parts[part]); |
| + |
| + try { |
| + var stat = fs.statSync(subpath); |
| + |
| + if (stat.isDirectory()) { |
| + existing = subpath; |
| + } else { |
| + throw 'Cannot create directory $path because $existing exists and ' + |
| + 'is not a directory.'; |
| + } |
| + } catch (e) { |
| + // Ugly hack. We only want to catch ENOENT exceptions from fs.statSync |
| + // which means the path we're trying doesn't exist. Since this is coming |
| + // from node, we can't check the exception's type. |
| + if (e.toString().indexOf('ENOENT', 0) != -1) break; |
| + |
| + // Re-throw any other exceptions. |
| + throw e; |
| + } |
| + } |
| + |
| + // Create the remaining directories. |
| + for (; part < parts.length; part++) { |
| + existing = joinPaths(existing, parts[part]); |
| + fs.mkdirSync(existing); |
| + } |
| + } |
| + |
| + void removeDirectory(String path, [bool recursive = false]) { |
| + if (recursive) { |
| + // Remove the contents first. |
| + for (var file in fs.readdirSync(path)) { |
| + var subpath = joinPaths(path, file); |
| + var stat = fs.statSync(subpath); |
| + |
| + if (stat.isDirectory()) { |
| + // Recurse into subdirectories. |
| + removeDirectory(subpath, recursive: true); |
| + } else if (stat.isFile()) { |
| + // Try to remove the file. |
| + fs.unlinkSync(subpath); |
| + } |
| + } |
| + } |
| + |
| + fs.rmdirSync(path); |
| + } |
| } |