Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/io.js

Issue 9846017: Make JavaScript sources compile into the library; convert JavaScript files on the fly (Closed)
Patch Set: Review comments addressed Created March 15, 2013, 3:59 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/info.js ('k') | lib/prefs.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 //
2 // No direct file system access, using FileSystem API
3 //
4
5 var IO = exports.IO =
6 {
7 _getFileEntry: function(file, create, successCallback, errorCallback)
8 {
9 if (file instanceof FakeFile)
10 file = file.path;
11 else if ("spec" in file)
12 file = file.spec;
13
14 // Remove directory path - we operate on a single directory in Chrome
15 file = file.replace(/^.*[\/\\]/, "");
16
17 // We request a gigabyte of space, just in case
18 (window.requestFileSystem || window.webkitRequestFileSystem)(window.PERSISTE NT, 1024*1024*1024, function(fs)
19 {
20 fs.root.getFile(file, {create: create}, function(fileEntry)
21 {
22 successCallback(fs, fileEntry);
23 }, errorCallback);
24 }, errorCallback);
25 },
26
27 lineBreak: "\n",
28
29 resolveFilePath: function(path)
30 {
31 return new FakeFile(path);
32 },
33
34 readFromFile: function(file, decode, listener, callback, timeLineID)
35 {
36 if ("spec" in file && /^defaults\b/.test(file.spec))
37 {
38 // Code attempts to read the default patterns.ini, we don't have that.
39 // Make sure to execute first-run actions instead.
40 callback(null);
41 if (localStorage.currentVersion)
42 seenDataCorruption = true;
43 delete localStorage.currentVersion;
44 return;
45 }
46
47 this._getFileEntry(file, false, function(fs, fileEntry)
48 {
49 fileEntry.file(function(file)
50 {
51 var reader = new FileReader();
52 reader.onloadend = function()
53 {
54 if (reader.error)
55 callback(reader.error);
56 else
57 {
58 var lines = reader.result.split(/[\r\n]+/);
59 for (var i = 0; i < lines.length; i++)
60 listener.process(lines[i]);
61 listener.process(null);
62 callback(null);
63 }
64 };
65 reader.readAsText(file);
66 }, callback);
67 }, callback);
68 },
69
70 writeToFile: function(file, encode, data, callback, timeLineID)
71 {
72 this._getFileEntry(file, true, function(fs, fileEntry)
73 {
74 fileEntry.createWriter(function(writer)
75 {
76 var executeWriteOperation = function(op, nextOperation)
77 {
78 writer.onwriteend = function()
79 {
80 if (writer.error)
81 callback(writer.error);
82 else
83 nextOperation();
84 }.bind(this);
85
86 op();
87 }.bind(this);
88
89 executeWriteOperation(writer.truncate.bind(writer, 0), function()
90 {
91 var blob;
92 try
93 {
94 blob = new Blob([data.join(this.lineBreak) + this.lineBreak], {type: "text/plain"});
95 }
96 catch (e)
97 {
98 if (!(e instanceof TypeError))
99 throw e;
100
101 // Blob wasn't a constructor before Chrome 20
102 var builder = new (window.BlobBuilder || window.WebKitBlobBuilder);
103 builder.append(data.join(this.lineBreak) + this.lineBreak);
104 blob = builder.getBlob("text/plain");
105 }
106 executeWriteOperation(writer.write.bind(writer, blob), callback.bind(n ull, null));
107 }.bind(this));
108 }.bind(this), callback);
109 }.bind(this), callback);
110 },
111
112 copyFile: function(fromFile, toFile, callback)
113 {
114 // Simply combine read and write operations
115 var data = [];
116 this.readFromFile(fromFile, false, {
117 process: function(line)
118 {
119 if (line !== null)
120 data.push(line);
121 }
122 }, function(e)
123 {
124 if (e)
125 callback(e);
126 else
127 this.writeToFile(toFile, false, data, callback);
128 }.bind(this));
129 },
130
131 renameFile: function(fromFile, newName, callback)
132 {
133 this._getFileEntry(fromFile, false, function(fs, fileEntry)
134 {
135 fileEntry.moveTo(fs.root, newName, function()
136 {
137 callback(null);
138 }, callback);
139 }, callback);
140 },
141
142 removeFile: function(file, callback)
143 {
144 this._getFileEntry(file, false, function(fs, fileEntry)
145 {
146 fileEntry.remove(function()
147 {
148 callback(null);
149 }, callback);
150 }, callback);
151 },
152
153 statFile: function(file, callback)
154 {
155 this._getFileEntry(file, false, function(fs, fileEntry)
156 {
157 fileEntry.getMetadata(function(metadata)
158 {
159 callback(null, {
160 exists: true,
161 isDirectory: fileEntry.isDirectory,
162 isFile: fileEntry.isFile,
163 lastModified: metadata.modificationTime.getTime()
164 });
165 }, callback);
166 }, callback);
167 }
168 };
OLDNEW
« no previous file with comments | « lib/info.js ('k') | lib/prefs.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld