Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #library('file_system_node'); | 5 #library('file_system_node'); |
| 6 | 6 |
| 7 #import('file_system.dart'); | 7 #import('file_system.dart'); |
| 8 #import('lib/node/node.dart'); | 8 #import('lib/node/node.dart'); |
| 9 | 9 |
| 10 /** File system implementation using nodejs api's (for self-hosted compiler). */ | 10 /** File system implementation using nodejs api's (for self-hosted compiler). */ |
| 11 class NodeFileSystem implements FileSystem { | 11 class NodeFileSystem implements FileSystem { |
| 12 void writeString(String outfile, String text) { | 12 void writeString(String outfile, String text) { |
| 13 fs.writeFileSync(outfile, text); | 13 fs.writeFileSync(outfile, text); |
| 14 } | 14 } |
| 15 | 15 |
| 16 String readAll(String filename) { | 16 String readAll(String filename) { |
| 17 return fs.readFileSync(filename, 'utf8'); | 17 return fs.readFileSync(filename, 'utf8'); |
| 18 } | 18 } |
| 19 | 19 |
| 20 bool fileExists(String filename) { | 20 bool fileExists(String filename) { |
| 21 return path.existsSync(filename); | 21 return path.existsSync(filename); |
| 22 } | 22 } |
| 23 | |
| 24 void createDirectory(String path, [bool recursive = false]) { | |
| 25 if (!recursive) { | |
| 26 fs.mkdirSync(path); | |
| 27 return; | |
| 28 } | |
| 29 | |
| 30 // See how much of the path already exists and how much we need to create. | |
| 31 var parts = path.split('/'); | |
|
Jacob
2011/11/16 19:59:30
var --> final
Bob Nystrom
2011/11/17 20:44:21
Done.
| |
| 32 var existing = '.'; | |
| 33 var part; | |
| 34 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
| |
| 35 var subpath = joinPaths(existing, parts[part]); | |
| 36 | |
| 37 try { | |
| 38 var stat = fs.statSync(subpath); | |
| 39 | |
| 40 if (stat.isDirectory()) { | |
| 41 existing = subpath; | |
| 42 } else { | |
| 43 throw 'Cannot create directory $path because $existing exists and ' + | |
| 44 'is not a directory.'; | |
| 45 } | |
| 46 } catch (e) { | |
| 47 // Ugly hack. We only want to catch ENOENT exceptions from fs.statSync | |
| 48 // which means the path we're trying doesn't exist. Since this is coming | |
| 49 // from node, we can't check the exception's type. | |
| 50 if (e.toString().indexOf('ENOENT', 0) != -1) break; | |
| 51 | |
| 52 // Re-throw any other exceptions. | |
| 53 throw e; | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 // Create the remaining directories. | |
| 58 for (; part < parts.length; part++) { | |
| 59 existing = joinPaths(existing, parts[part]); | |
| 60 fs.mkdirSync(existing); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 void removeDirectory(String path, [bool recursive = false]) { | |
| 65 if (recursive) { | |
| 66 // Remove the contents first. | |
| 67 for (var file in fs.readdirSync(path)) { | |
| 68 var subpath = joinPaths(path, file); | |
| 69 var stat = fs.statSync(subpath); | |
| 70 | |
| 71 if (stat.isDirectory()) { | |
| 72 // Recurse into subdirectories. | |
| 73 removeDirectory(subpath, recursive: true); | |
| 74 } else if (stat.isFile()) { | |
| 75 // Try to remove the file. | |
| 76 fs.unlinkSync(subpath); | |
| 77 } | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 fs.rmdirSync(path); | |
| 82 } | |
| 23 } | 83 } |
| OLD | NEW |