OLD | NEW |
1 "use strict"; | 1 "use strict"; |
2 | 2 |
3 let data = new Map(); | 3 let data = new Map(); |
4 | 4 |
5 exports.IO = { | 5 exports.IO = { |
6 // Non-public API, for tests only | 6 // Non-public API, for tests only |
7 _getFileContents(fileName) | 7 _getFileContents(fileName) |
8 { | 8 { |
9 if (data.has(fileName)) | 9 if (data.has(fileName)) |
10 return data.get(fileName).contents; | 10 return data.get(fileName).contents; |
(...skipping 12 matching lines...) Expand all Loading... |
23 return data.get(fileName).lastModified; | 23 return data.get(fileName).lastModified; |
24 return 0; | 24 return 0; |
25 }, | 25 }, |
26 _setModifiedTime(fileName, lastModified) | 26 _setModifiedTime(fileName, lastModified) |
27 { | 27 { |
28 if (data.has(fileName)) | 28 if (data.has(fileName)) |
29 data.get(fileName).lastModified = lastModified; | 29 data.get(fileName).lastModified = lastModified; |
30 }, | 30 }, |
31 | 31 |
32 // Public API | 32 // Public API |
33 writeToFile(fileName, generator) | 33 async writeToFile(fileName, generator) |
34 { | 34 { |
35 return Promise.resolve().then(() => | 35 data.set(fileName, { |
36 { | 36 lastModified: Date.now(), |
37 data.set(fileName, { | 37 contents: Array.from(generator) |
38 lastModified: Date.now(), | |
39 contents: Array.from(generator) | |
40 }); | |
41 }); | 38 }); |
42 }, | 39 }, |
43 readFromFile(fileName, listener) | 40 async readFromFile(fileName, listener) |
44 { | 41 { |
45 return Promise.resolve().then(() => | 42 if (!data.has(fileName)) |
| 43 throw new Error("File doesn't exist"); |
| 44 |
| 45 let lines = data.get(fileName).contents; |
| 46 for (let line of lines) |
| 47 listener(line); |
| 48 }, |
| 49 async copyFile(fromName, toName) |
| 50 { |
| 51 if (!data.has(fromName)) |
| 52 throw new Error("File doesn't exist"); |
| 53 |
| 54 if (fromName == toName) |
| 55 throw new Error("Cannot copy file to itself"); |
| 56 |
| 57 data.set(toName, data.get(fromName)); |
| 58 }, |
| 59 async renameFile(fromName, toName) |
| 60 { |
| 61 await this.copyFile(fromName, toName); |
| 62 await this.removeFile(fromName); |
| 63 }, |
| 64 async removeFile(fileName) |
| 65 { |
| 66 if (!data.has(fileName)) |
| 67 throw new Error("File doesn't exist"); |
| 68 |
| 69 data.delete(fileName); |
| 70 }, |
| 71 async statFile(fileName) |
| 72 { |
| 73 if (data.has(fileName)) |
46 { | 74 { |
47 if (!data.has(fileName)) | 75 return { |
48 throw new Error("File doesn't exist"); | 76 exists: true, |
| 77 lastModified: data.get(fileName).lastModified |
| 78 }; |
| 79 } |
49 | 80 |
50 let lines = data.get(fileName).contents; | 81 return { |
51 for (let line of lines) | 82 exists: false, |
52 listener(line); | 83 lastModified: 0 |
53 }); | 84 }; |
54 }, | |
55 copyFile(fromName, toName) | |
56 { | |
57 return Promise.resolve().then(() => | |
58 { | |
59 if (!data.has(fromName)) | |
60 throw new Error("File doesn't exist"); | |
61 if (fromName == toName) | |
62 throw new Error("Cannot copy file to itself"); | |
63 | |
64 data.set(toName, data.get(fromName)); | |
65 }); | |
66 }, | |
67 renameFile(fromName, toName) | |
68 { | |
69 return this.copyFile(fromName, toName).then(() => this.removeFile(fromName))
; | |
70 }, | |
71 removeFile(fileName) | |
72 { | |
73 return Promise.resolve().then(() => | |
74 { | |
75 if (!data.has(fileName)) | |
76 throw new Error("File doesn't exist"); | |
77 | |
78 data.delete(fileName); | |
79 }); | |
80 }, | |
81 statFile(fileName) | |
82 { | |
83 return Promise.resolve().then(() => | |
84 { | |
85 if (data.has(fileName)) | |
86 { | |
87 return { | |
88 exists: true, | |
89 lastModified: data.get(fileName).lastModified | |
90 }; | |
91 } | |
92 return { | |
93 exists: false, | |
94 lastModified: 0 | |
95 }; | |
96 }); | |
97 } | 85 } |
98 }; | 86 }; |
OLD | NEW |