OLD | NEW |
| 1 let data = {}; |
| 2 |
1 // | 3 // |
2 // Fake nsIFile implementation for our I/O | 4 // Fake nsIFile implementation for our I/O |
3 // | 5 // |
4 function FakeFile(path) | 6 function FakeFile(path) |
5 { | 7 { |
6 this.path = path; | 8 this.path = path; |
7 } | 9 } |
8 FakeFile.prototype = | 10 FakeFile.prototype = |
9 { | 11 { |
10 get leafName() | 12 get leafName() |
11 { | 13 { |
12 return this.path; | 14 return this.path; |
13 }, | 15 }, |
14 set leafName(value) | 16 set leafName(value) |
15 { | 17 { |
16 this.path = value; | 18 this.path = value; |
17 }, | 19 }, |
18 append: function(path) | 20 append: function(path) |
19 { | 21 { |
20 this.path += path; | 22 this.path += path; |
21 }, | 23 }, |
| 24 exists: function() |
| 25 { |
| 26 return this.path in data; |
| 27 }, |
| 28 get contents() |
| 29 { |
| 30 return (data[this.path] || {}).contents; |
| 31 }, |
| 32 set contents(value) |
| 33 { |
| 34 data[this.path] = {lastModified: Date.now()}; |
| 35 return data[this.path].contents = value; |
| 36 }, |
| 37 get lastModifiedTime() |
| 38 { |
| 39 return (data[this.path] || {}).lastModified; |
| 40 }, |
| 41 set lastModifiedTime(value) |
| 42 { |
| 43 return (data[this.path] || {}).lastModified = value; |
| 44 }, |
22 clone: function() | 45 clone: function() |
23 { | 46 { |
24 return new FakeFile(this.path); | 47 return new FakeFile(this.path); |
25 }, | 48 }, |
26 get parent() | 49 get parent() |
27 { | 50 { |
28 return {create: function() {}}; | 51 return {create: function() {}}; |
29 }, | 52 }, |
30 normalize: function() {} | 53 normalize: function() {} |
31 }; | 54 }; |
32 | 55 |
33 | |
34 exports.IO = { | 56 exports.IO = { |
| 57 lineBreak: "\n", |
35 resolveFilePath: function(path) | 58 resolveFilePath: function(path) |
36 { | 59 { |
37 return new FakeFile(path); | 60 return new FakeFile(path); |
38 }, | 61 }, |
39 statFile: function(path) | 62 writeToFile: function(file, generator, callback) |
40 { | 63 { |
41 return true; | 64 Promise.resolve().then(() => |
42 } | 65 { |
| 66 let data = []; |
| 67 for (let line of generator) |
| 68 data.push(line); |
| 69 file.contents = data.join("\n") + "\n"; |
| 70 }).then(() => callback(null)).catch(e => callback(e)); |
| 71 }, |
| 72 readFromFile: function(file, listener, callback) |
| 73 { |
| 74 Promise.resolve().then(() => |
| 75 { |
| 76 if (!data.hasOwnProperty(file.path)) |
| 77 throw new Error("File doesn't exist"); |
| 78 |
| 79 let lines = file.contents.split("\n"); |
| 80 if (lines.length && lines[lines.length - 1] == "") |
| 81 lines.pop(); |
| 82 for (let line of lines) |
| 83 listener.process(line); |
| 84 listener.process(null); |
| 85 }).then(() => callback(null)).catch(e => callback(e)); |
| 86 }, |
| 87 copyFile: function(from, to, callback) |
| 88 { |
| 89 Promise.resolve().then(() => |
| 90 { |
| 91 if (!data.hasOwnProperty(from.path)) |
| 92 throw new Error("File doesn't exist"); |
| 93 if (from.path == to.path) |
| 94 throw new Error("Cannot copy file to itself"); |
| 95 |
| 96 to.contents = from.contents; |
| 97 }).then(() => callback(null)).catch(e => callback(e)); |
| 98 }, |
| 99 renameFile: function(from, newName, callback) |
| 100 { |
| 101 Promise.resolve().then(() => |
| 102 { |
| 103 if (!data.hasOwnProperty(from.path)) |
| 104 throw new Error("File doesn't exist"); |
| 105 if (from.path == newName) |
| 106 throw new Error("Cannot move file to itself"); |
| 107 |
| 108 data[newName] = data[from.path]; |
| 109 delete data[from.path]; |
| 110 }).then(() => callback(null)).catch(e => callback(e)); |
| 111 }, |
| 112 removeFile: function(file, callback) |
| 113 { |
| 114 Promise.resolve().then(() => |
| 115 { |
| 116 if (!data.hasOwnProperty(file.path)) |
| 117 throw new Error("File doesn't exist"); |
| 118 |
| 119 delete data[file.path]; |
| 120 }).then(() => callback(null)).catch(e => callback(e)); |
| 121 }, |
| 122 statFile: function(file, callback) |
| 123 { |
| 124 Promise.resolve().then(() => |
| 125 { |
| 126 if (file.exists()) |
| 127 { |
| 128 return { |
| 129 exists: true, |
| 130 isDirectory: false, |
| 131 isFile: true, |
| 132 lastModified: file.lastModifiedTime |
| 133 }; |
| 134 } |
| 135 else |
| 136 { |
| 137 return { |
| 138 exists: false, |
| 139 isDirectory: false, |
| 140 isFile: false, |
| 141 lastModified: 0 |
| 142 }; |
| 143 } |
| 144 }).then(result => callback(null, result)).catch(e => callback(e)); |
| 145 }, |
43 }; | 146 }; |
OLD | NEW |