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