Last active
August 29, 2015 14:00
-
-
Save creationix/cf498033db3d6f8c4949 to your computer and use it in GitHub Desktop.
sample of fs-db in node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var dirname = require('path').dirname; | |
var repo = { | |
rootPath: pathJoin(__dirname, "repo.git") | |
}; | |
require('js-git/mixins/fs-db')(repo, { | |
readFile: function (path, callback) { | |
fs.readFile(path, function (err, buffer) { | |
if (err) { | |
if (err.code === "ENOENT") return callback(); | |
return callback(err); | |
} | |
callback(null, buffer); | |
}); | |
}, | |
writeFile: function (path, buffer, callback) { | |
mkdirp(dirname(path), function (err) { | |
if (err) return callback(err); | |
fs.writeFile(path, buffer, callback); | |
}); | |
}, | |
readDir: function (path, callback) { | |
fs.readdir(path, function (err, results) { | |
if (err) { | |
if (err.code === "ENOENT") return callback(); | |
return callback(err); | |
} | |
return callback(null, results); | |
}); | |
}, | |
readChunk: function (path, start, end, callback) { | |
throw "TODO: Implement this to read packfiles" | |
// I didn't need it for my node server since I create the repos. | |
// This should return a Buffer for the file from start to end offsets. | |
} | |
}); | |
function mkdirp(path, callback) { | |
fs.mkdir(path, function (err) { | |
if (err) { | |
if (err.code === "ENOENT") return mkdirp(dirname(path), function (err) { | |
if (err) return callback(err); | |
fs.mkdir(path, callback); | |
}); | |
if (err.code === "EEXIST") return callback(); | |
return callback(err); | |
} | |
callback(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment