OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 #library('file_system_node'); | |
6 | |
7 #import('file_system.dart'); | |
8 #import('lib/node/node.dart'); | |
9 | |
10 /** File system implementation using nodejs api's (for self-hosted compiler). */ | |
11 class NodeFileSystem implements FileSystem { | |
12 void writeString(String outfile, String text) { | |
13 fs.writeFileSync(outfile, text); | |
14 } | |
15 | |
16 String readAll(String filename) { | |
17 return fs.readFileSync(filename, 'utf8'); | |
18 } | |
19 | |
20 bool fileExists(String filename) { | |
21 return path.existsSync(filename); | |
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 final parts = path.split('/'); | |
32 var existing = '.'; | |
33 var part; | |
34 for (part = 0; part < parts.length; part++) { | |
35 final subpath = joinPaths(existing, parts[part]); | |
36 | |
37 try { | |
38 final 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 (var 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') != -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 (final file in fs.readdirSync(path)) { | |
68 final subpath = joinPaths(path, file); | |
69 final 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 } | |
83 } | |
OLD | NEW |