OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 var IO = exports.IO = | 18 function readFileAsync(fileName) |
| 19 { |
| 20 return new Promise((resolve, reject) => |
| 21 { |
| 22 _fileSystem.read(fileName, (result) => |
| 23 { |
| 24 if (result.error) |
| 25 return reject(result.error); |
| 26 resolve(result); |
| 27 }); |
| 28 }); |
| 29 }; |
| 30 |
| 31 function writeFileAsync(fileName, content) |
| 32 { |
| 33 return new Promise((resolve, reject) => |
| 34 { |
| 35 _fileSystem.write(fileName, content, (error) => |
| 36 { |
| 37 if (error) |
| 38 return reject(error); |
| 39 resolve(); |
| 40 }); |
| 41 }); |
| 42 }; |
| 43 |
| 44 exports.IO = |
19 { | 45 { |
20 lineBreak: "\n", | 46 lineBreak: "\n", |
21 | 47 |
22 resolveFilePath: function(path) | 48 readFromFile(fileName, listener) |
23 { | 49 { |
24 return new FakeFile(_fileSystem.resolve(path)); | 50 return readFileAsync(fileName).then((result) => |
25 }, | |
26 | |
27 readFromFile: function(file, listener, callback, timeLineID) | |
28 { | |
29 _fileSystem.read(file.path, function(result) | |
30 { | 51 { |
31 if (result.error) | 52 let lines = result.content.split(/[\r\n]+/); |
32 callback(result.error); | 53 for (let line of lines) |
33 else | 54 listener(line); |
34 { | |
35 var lines = result.content.split(/[\r\n]+/); | |
36 for (var i = 0; i < lines.length; i++) | |
37 listener.process(lines[i]); | |
38 listener.process(null); | |
39 callback(null); | |
40 } | |
41 }); | 55 }); |
42 }, | 56 }, |
43 | 57 |
44 writeToFile: function(file, data, callback, timeLineID) | 58 writeToFile(fileName, generator) |
45 { | 59 { |
46 let content = Array.from(data).join(this.lineBreak) + this.lineBreak; | 60 let content = Array.from(generator).join(this.lineBreak) + this.lineBreak; |
47 _fileSystem.write(file.path, content, callback); | 61 return writeFileAsync(fileName, content); |
48 }, | 62 }, |
49 | 63 |
50 copyFile: function(fromFile, toFile, callback) | 64 copyFile(fromFileName, toFileName) |
51 { | 65 { |
52 // Simply combine read and write operations | 66 return readFileAsync(fromFileName).then(content => writeFileAsync(toFileName
, content)); |
53 var data = []; | |
54 this.readFromFile(fromFile, { | |
55 process: function(line) | |
56 { | |
57 if (line !== null) | |
58 data.push(line); | |
59 } | |
60 }, function(e) | |
61 { | |
62 if (e) | |
63 callback(e); | |
64 else | |
65 this.writeToFile(toFile, data, callback); | |
66 }.bind(this)); | |
67 }, | 67 }, |
68 | 68 |
69 renameFile: function(fromFile, newName, callback) | 69 renameFile(fromFileName, newNameFile) |
70 { | 70 { |
71 _fileSystem.move(fromFile.path, newName, callback); | 71 return new Promise((resolve, reject) => |
| 72 { |
| 73 _fileSystem.move(fromFileName, newNameFile, (error) => |
| 74 { |
| 75 if (error) |
| 76 return reject(error); |
| 77 resolve(); |
| 78 }); |
| 79 }); |
72 }, | 80 }, |
73 | 81 |
74 removeFile: function(file, callback) | 82 removeFile(fileName) |
75 { | 83 { |
76 _fileSystem.remove(file.path, callback); | 84 return new Promise((resolve, reject) => |
| 85 { |
| 86 _fileSystem.remove(fileName, (error) => |
| 87 { |
| 88 if (error) |
| 89 return reject(error); |
| 90 resolve(); |
| 91 }); |
| 92 }); |
77 }, | 93 }, |
78 | 94 |
79 statFile: function(file, callback) | 95 statFile(fileName, callback) |
80 { | 96 { |
81 _fileSystem.stat(file.path, function(result) | 97 return new Promise((resolve, reject) => |
82 { | 98 { |
83 callback(result.error, result); | 99 _fileSystem.stat(fileName, (result) => |
| 100 { |
| 101 if (result.error) |
| 102 return reject(result.error); |
| 103 resolve(result); |
| 104 }); |
84 }); | 105 }); |
85 } | 106 } |
86 }; | 107 }; |
OLD | NEW |