mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 01:53:26 +00:00
Remove write operations from virtual fs
This commit is contained in:
+1
-54
@@ -1,13 +1,10 @@
|
||||
var _ = require('lodash');
|
||||
var path = require('path');
|
||||
var Buffer = require('buffer').Buffer;
|
||||
var destroy = require('destroy');
|
||||
var os = require('os');
|
||||
|
||||
var Promise = require('../utils/promise');
|
||||
|
||||
/*
|
||||
A filesystem is an interface to read/write files
|
||||
A filesystem is an interface to read files
|
||||
GitBook can works with a virtual filesystem, for example in the browser.
|
||||
*/
|
||||
|
||||
@@ -36,11 +33,6 @@ FS.prototype.stat = function(filename) {
|
||||
// To implement for each fs
|
||||
};
|
||||
|
||||
// Write a file and returns a promise
|
||||
FS.prototype.write = function(filename, buffer) {
|
||||
// To implement for each fs
|
||||
};
|
||||
|
||||
// List files/directories in a directory
|
||||
FS.prototype.readdir = function(folder) {
|
||||
// To implement for each fs
|
||||
@@ -49,11 +41,6 @@ FS.prototype.readdir = function(folder) {
|
||||
// These methods don't require to be redefined, by default it uses .exists, .read, .write, .list
|
||||
// For optmization, it can be redefined:
|
||||
|
||||
// Allocate a new temporary directory
|
||||
FS.prototype.tmpdir = function() {
|
||||
return path.resolve(os.tmpdir(), 'gitbook_tmp_'+Date.now());
|
||||
};
|
||||
|
||||
// List files in a directory
|
||||
FS.prototype.listFiles = function(folder) {
|
||||
return this.readdir(folder)
|
||||
@@ -96,46 +83,6 @@ FS.prototype.readAsString = function(filename) {
|
||||
});
|
||||
};
|
||||
|
||||
// Write a stream to a file and returns a promise
|
||||
FS.prototype.writeStream = function(filename, stream) {
|
||||
var bufs = [];
|
||||
var d = Promise.defer();
|
||||
|
||||
var cleanup = function() {
|
||||
destroy(stream);
|
||||
stream.removeAllListeners();
|
||||
};
|
||||
|
||||
stream.on('data', function(d) {
|
||||
bufs.push(d);
|
||||
});
|
||||
|
||||
stream.on('error', function(err) {
|
||||
cleanup();
|
||||
|
||||
d.reject(err);
|
||||
});
|
||||
|
||||
stream.on('end', function(){
|
||||
cleanup();
|
||||
|
||||
var buf = Buffer.concat(bufs);
|
||||
d.resolve(buf);
|
||||
});
|
||||
|
||||
return d.promise;
|
||||
};
|
||||
|
||||
// Copy a file
|
||||
FS.prototype.copy = function(from, to) {
|
||||
var that = this;
|
||||
|
||||
return this.read(from)
|
||||
.then(function(buf) {
|
||||
return that.write(to, buf);
|
||||
});
|
||||
};
|
||||
|
||||
// Find a file in a folder (case incensitive)
|
||||
// Return the real filename
|
||||
FS.prototype.findFile = function findFile(root, filename) {
|
||||
|
||||
@@ -26,19 +26,6 @@ NodeFS.prototype.stat = function(filename) {
|
||||
return fs.stat(filename);
|
||||
};
|
||||
|
||||
// Write a file and returns a promise
|
||||
NodeFS.prototype.write = function(filename, buffer) {
|
||||
var folder = path.dirname(filename);
|
||||
return Promise()
|
||||
.then(function() {
|
||||
if (!folder) return;
|
||||
return fs.mkdirp(folder);
|
||||
})
|
||||
.then(function() {
|
||||
return fs.writeFile(filename, buffer);
|
||||
});
|
||||
};
|
||||
|
||||
// List files in a directory
|
||||
NodeFS.prototype.readdir = function(folder) {
|
||||
return fs.readdir(folder)
|
||||
|
||||
Reference in New Issue
Block a user