From 1a97504de9cd46739284aa56a4fa00440d1b949a Mon Sep 17 00:00:00 2001 From: karissa Date: Mon, 19 Dec 2016 13:52:59 -0800 Subject: [PATCH 1/2] add dat.json file support in create command --- lib/commands/create.js | 38 +++++++++++++++++++++++++++----------- lib/dat-json.js | 31 +++++++++++++++++++++++++++++++ tests/create.js | 2 ++ tests/helpers/index.js | 9 +++++++++ 4 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 lib/dat-json.js diff --git a/lib/commands/create.js b/lib/commands/create.js index cde1a90..6546373 100644 --- a/lib/commands/create.js +++ b/lib/commands/create.js @@ -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', @@ -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() + 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 () { diff --git a/lib/dat-json.js b/lib/dat-json.js new file mode 100644 index 0000000..b872fef --- /dev/null +++ b/lib/dat-json.js @@ -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') +} diff --git a/tests/create.js b/tests/create.js index 1609eda..157f1df 100644 --- a/tests/create.js +++ b/tests/create.js @@ -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 }) diff --git a/tests/helpers/index.js b/tests/helpers/index.js index aa0a4fe..396c438 100644 --- a/tests/helpers/index.js +++ b/tests/helpers/index.js @@ -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 @@ -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() From 51fc98339ddc8d2e23289a6c2ebbde5cf30c390b Mon Sep 17 00:00:00 2001 From: karissa Date: Mon, 19 Dec 2016 13:58:24 -0800 Subject: [PATCH 2/2] add todo --- lib/commands/create.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/commands/create.js b/lib/commands/create.js index 6546373..ea86b29 100644 --- a/lib/commands/create.js +++ b/lib/commands/create.js @@ -51,7 +51,7 @@ function create (opts) { if (dat.owner) { datJson.read(dat, function (err, body) { - if (!err) return importFiles() + 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)