-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from joehand/dat.json
Dat.json
- Loading branch information
Showing
4 changed files
with
69 additions
and
11 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
var fs = require('fs') | ||
var datKeyAs = require('dat-key-as') | ||
var path = require('path') | ||
|
||
module.exports = { | ||
read: function (dat, cb) { | ||
// dat.json | ||
// reads to dat.meta if exists | ||
// creates dat.json if not exists with title = dir name | ||
// (TODO: move to module & validate dat.json) | ||
fs.readFile(datJsonFile(dat), 'utf8', function (err, body) { | ||
if (err) return cb(err) | ||
var meta | ||
try { | ||
meta = JSON.parse(body) | ||
} catch (e) { | ||
return cb(new Error('Error reading the dat.json file.')) | ||
} | ||
cb(null, meta) | ||
}) | ||
}, | ||
create: function (dat, cb) { | ||
dat.meta = {title: path.basename(dat.path), description: ''} | ||
if (dat.key) dat.meta.url = 'dat://' + datKeyAs.str(dat.key) | ||
fs.writeFile(datJsonFile(dat), JSON.stringify(dat.meta), cb) | ||
} | ||
} | ||
|
||
function datJsonFile (dat) { | ||
return path.join(dat.path, 'dat.json') | ||
} |
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
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