diff options
author | Joe Hand <joe@joeahand.com> | 2017-05-31 10:55:05 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-31 10:55:05 -0700 |
commit | d1399d3ea9d3473d9986d1e90aef59de1025c687 (patch) | |
tree | 6ad82a494d20848b22c9422a9c26e32dc396fdfe /docs/cookbook/using-fs.md | |
parent | 079395f55df778af0fc87bad8730ca62462201c8 (diff) | |
download | dat-docs-d1399d3ea9d3473d9986d1e90aef59de1025c687.tar.gz dat-docs-d1399d3ea9d3473d9986d1e90aef59de1025c687.zip |
SLEEP + general docs update + more introduction content (#57)
* big docs update
* fix sleep link
* separate sustainability header
* better into and reword
* clean up terms and other places
* add intro, overview, troubleshooting. update more content
* minor change to force new deploy
* more words
* more updates and some pictures
* wordsmith intro to be more about dat uniqueness
* syncing & publishing
* add link files description
* add new gifs
* change overview to remove tutorial
* add wip tutorial content
* update build with dat
* add http file
Diffstat (limited to 'docs/cookbook/using-fs.md')
-rw-r--r-- | docs/cookbook/using-fs.md | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/docs/cookbook/using-fs.md b/docs/cookbook/using-fs.md new file mode 100644 index 0000000..39e2816 --- /dev/null +++ b/docs/cookbook/using-fs.md @@ -0,0 +1,58 @@ +# Using the Hyperdrive FS + +[Hyperdrive](https://github.com/mafintosh/hyperdrive), the core file system module in dat, exposes an API that mimics the Node fs API. This allows you to create modules that act on hyperdrive or a regular fs with the same API. We have several modules that we make use of this custom fs, such as [mirror-folder](https://github.com/mafintosh/mirror-folder). + +Mirror folder can copy a regular directory to another regular directory: + +```js +var mirror = require('mirror-folder') + +mirror('/source', '/dest', function (err) { + console.log('mirror complete') +}) +``` + +You can also copy a folder to an archive (this is how we do importing in Dat): + +```js +var archive = hyperdrive('/dir') +mirror('/source', {name: '/', fs: archive}, function (err) { + console.log('mirror complete') +}) +``` + +### Creating Custom FS Modules + +To create a module that uses a custom fs, you can default to the regular `fs` but also accept `fs` as an argument. For example, to print a file you could write this function: + +```js +function printFile(file, fs) { + if (!fs) fs = require('fs') + + fs.readFile(file, 'utf-8', function (err, data) { + console.log(data) + }) +} +``` + +Then you could use this to print a file from a regular fs: + +```js +printFile('/data/hello-world.txt') +``` + +Or from a hyperdrive archive: + +```js +var archive = hyperdrive('/data') +printFile('/hello-world.txt', archive) // pass archive as the fs! +``` + +## Modules! + +See more examples of custom-fs modules: + +* [mirror-folder](https://github.com/mafintosh/mirror-folder) - copy files from one fs to another fs (regular or custom) +* [count-files](https://github.com/joehand/count-files) - count files in regular or custom fs. +* [ftpfs](https://github.com/maxogden/ftpfs) - custom fs for FTPs +* [bagit-fs](https://github.com/joehand/bagit-fs) - custom fs module for the BagIt spec. |