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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/info.js ('k') | lib/prefs.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/io.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/lib/io.js
@@ -0,0 +1,168 @@
+//
+// No direct file system access, using FileSystem API
+//
+
+var IO = exports.IO =
+{
+ _getFileEntry: function(file, create, successCallback, errorCallback)
+ {
+ if (file instanceof FakeFile)
+ file = file.path;
+ else if ("spec" in file)
+ file = file.spec;
+
+ // Remove directory path - we operate on a single directory in Chrome
+ file = file.replace(/^.*[\/\\]/, "");
+
+ // We request a gigabyte of space, just in case
+ (window.requestFileSystem || window.webkitRequestFileSystem)(window.PERSISTENT, 1024*1024*1024, function(fs)
+ {
+ fs.root.getFile(file, {create: create}, function(fileEntry)
+ {
+ successCallback(fs, fileEntry);
+ }, errorCallback);
+ }, errorCallback);
+ },
+
+ lineBreak: "\n",
+
+ resolveFilePath: function(path)
+ {
+ return new FakeFile(path);
+ },
+
+ readFromFile: function(file, decode, listener, callback, timeLineID)
+ {
+ if ("spec" in file && /^defaults\b/.test(file.spec))
+ {
+ // Code attempts to read the default patterns.ini, we don't have that.
+ // Make sure to execute first-run actions instead.
+ callback(null);
+ if (localStorage.currentVersion)
+ seenDataCorruption = true;
+ delete localStorage.currentVersion;
+ return;
+ }
+
+ this._getFileEntry(file, false, function(fs, fileEntry)
+ {
+ fileEntry.file(function(file)
+ {
+ var reader = new FileReader();
+ reader.onloadend = function()
+ {
+ if (reader.error)
+ callback(reader.error);
+ else
+ {
+ var lines = reader.result.split(/[\r\n]+/);
+ for (var i = 0; i < lines.length; i++)
+ listener.process(lines[i]);
+ listener.process(null);
+ callback(null);
+ }
+ };
+ reader.readAsText(file);
+ }, callback);
+ }, callback);
+ },
+
+ writeToFile: function(file, encode, data, callback, timeLineID)
+ {
+ this._getFileEntry(file, true, function(fs, fileEntry)
+ {
+ fileEntry.createWriter(function(writer)
+ {
+ var executeWriteOperation = function(op, nextOperation)
+ {
+ writer.onwriteend = function()
+ {
+ if (writer.error)
+ callback(writer.error);
+ else
+ nextOperation();
+ }.bind(this);
+
+ op();
+ }.bind(this);
+
+ executeWriteOperation(writer.truncate.bind(writer, 0), function()
+ {
+ var blob;
+ try
+ {
+ blob = new Blob([data.join(this.lineBreak) + this.lineBreak], {type: "text/plain"});
+ }
+ catch (e)
+ {
+ if (!(e instanceof TypeError))
+ throw e;
+
+ // Blob wasn't a constructor before Chrome 20
+ var builder = new (window.BlobBuilder || window.WebKitBlobBuilder);
+ builder.append(data.join(this.lineBreak) + this.lineBreak);
+ blob = builder.getBlob("text/plain");
+ }
+ executeWriteOperation(writer.write.bind(writer, blob), callback.bind(null, null));
+ }.bind(this));
+ }.bind(this), callback);
+ }.bind(this), callback);
+ },
+
+ copyFile: function(fromFile, toFile, callback)
+ {
+ // Simply combine read and write operations
+ var data = [];
+ this.readFromFile(fromFile, false, {
+ process: function(line)
+ {
+ if (line !== null)
+ data.push(line);
+ }
+ }, function(e)
+ {
+ if (e)
+ callback(e);
+ else
+ this.writeToFile(toFile, false, data, callback);
+ }.bind(this));
+ },
+
+ renameFile: function(fromFile, newName, callback)
+ {
+ this._getFileEntry(fromFile, false, function(fs, fileEntry)
+ {
+ fileEntry.moveTo(fs.root, newName, function()
+ {
+ callback(null);
+ }, callback);
+ }, callback);
+ },
+
+ removeFile: function(file, callback)
+ {
+ this._getFileEntry(file, false, function(fs, fileEntry)
+ {
+ fileEntry.remove(function()
+ {
+ callback(null);
+ }, callback);
+ }, callback);
+ },
+
+ statFile: function(file, callback)
+ {
+ this._getFileEntry(file, false, function(fs, fileEntry)
+ {
+ fileEntry.getMetadata(function(metadata)
+ {
+ callback(null, {
+ exists: true,
+ isDirectory: fileEntry.isDirectory,
+ isFile: fileEntry.isFile,
+ lastModified: metadata.modificationTime.getTime()
+ });
+ }, callback);
+ }, callback);
+ }
+};
« 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