Skip to content

Commit

Permalink
Merge pull request #43 from joehand/dat.json
Browse files Browse the repository at this point in the history
Dat.json
  • Loading branch information
Karissa authored Dec 19, 2016
2 parents 8ff5320 + 51fc983 commit 68c02a9
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 11 deletions.
38 changes: 27 additions & 11 deletions lib/commands/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var logger = require('status-logger')
var prettyBytes = require('pretty-bytes')
var Dat = require('dat-node')
var ui = require('../ui')
var datJson = require('../dat-json')

module.exports = {
name: 'create',
Expand Down Expand Up @@ -48,19 +49,34 @@ function create (opts) {
else output[1] = 'Creating link...' + '\n'
if (opts.quiet && dat.key) process.stdout.write(ui.link(dat.key))

// Not importing files. Just create .dat, print info, and exit.
if (!opts.import) return exit()
if (dat.owner) {
datJson.read(dat, function (err, body) {
if (!err) return importFiles() // TODO: if dat.json exists, then what?
if (err.code === 'ENOENT' || !body) {
return datJson.create(dat, function (err) {
if (err) return exit(err)
importFiles()
})
}
return exit(err)
})
}

function importFiles () {
// Not importing files. Just create .dat, print info, and exit.
if (!opts.import) return exit()

output[2] = 'Importing files to archive...'
importStatus = dat.importFiles(function (err) {
if (err) return exit(err)
output[2] = opts.live !== false ? 'File import finished!' : 'Snapshot created!'
output[3] = `Total Size: ${importStatus.fileCount} ${importStatus.fileCount === 1 ? 'file' : 'files'} (${prettyBytes(importStatus.totalSize)})`
output[2] = 'Importing files to archive...'
importStatus = dat.importFiles(function (err) {
if (err) return exit(err)
output[2] = opts.live !== false ? 'File import finished!' : 'Snapshot created!'
output[3] = `Total Size: ${importStatus.fileCount} ${importStatus.fileCount === 1 ? 'file' : 'files'} (${prettyBytes(importStatus.totalSize)})`

if (opts.live !== false) return exit()
if (dat.key) output[1] = ui.link(dat.key) + '\n'
exit()
})
if (opts.live !== false) return exit()
if (dat.key) output[1] = ui.link(dat.key) + '\n'
exit()
})
}
})

function updateProgress () {
Expand Down
31 changes: 31 additions & 0 deletions lib/dat-json.js
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')
}
2 changes: 2 additions & 0 deletions tests/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ test('create - default opts', function (t) {
t.ok(output.match(fileRe), 'total size: files okay')
t.ok(output.match(bytesRe), 'total size: bytes okay')

t.same(help.datJson(fixtures).title, 'fixtures', 'dat.json: has title')

st.kill()
return true
})
Expand Down
9 changes: 9 additions & 0 deletions tests/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var swarm = require('hyperdiscovery')
module.exports.matchLink = matchDatLink
module.exports.isDir = isDir
module.exports.testFolder = newTestFolder
module.exports.datJson = datJson
module.exports.shareFixtures = shareFixtures
module.exports.shareFeed = shareFeed
module.exports.fileList = fileList
Expand Down Expand Up @@ -64,6 +65,14 @@ function matchDatLink (str) {
return key
}

function datJson (filepath) {
try {
return JSON.parse(fs.readFileSync(path.join(filepath, 'dat.json')))
} catch (e) {
return {}
}
}

function isDir (dir) {
try {
return fs.statSync(dir).isDirectory()
Expand Down

0 comments on commit 68c02a9

Please sign in to comment.