diff options
-rwxr-xr-x | bin/cmd.js | 27 | ||||
-rw-r--r-- | test/cmd.js | 21 |
2 files changed, 45 insertions, 3 deletions
@@ -1,5 +1,6 @@ #!/usr/bin/env node +var createTorrent = require('create-torrent') var clivas = require('clivas') var cp = require('child_process') var fs = require('fs') @@ -66,6 +67,9 @@ if (command === 'help' || argv.help) { } else if (command === 'info') { torrentId = argv._[1] info(torrentId) +} else if (command === 'create') { + var input = argv._[1] + create(input) } else if (command === 'download') { torrentId = argv._[1] download(torrentId) @@ -102,8 +106,9 @@ function help () { webtorrent download "magnet:?xt=urn:btih:..." --vlc Available commands: - download download a torrent - info show info for a .torrent file or magnet uri + download Download a torrent + create Create a .torrent file + info Show info for a .torrent file or magnet uri Specify torrents as one of the following: * magnet uri @@ -151,7 +156,23 @@ function info (torrentId) { delete parsedTorrent.info delete parsedTorrent.infoBuffer - console.log(JSON.stringify(parsedTorrent, undefined, 2)) + var output = JSON.stringify(parsedTorrent, undefined, 2) + if (argv.out) { + fs.writeFileSync(argv.out, output) + } else { + process.stdout.write(output) + } +} + +function create (input) { + createTorrent(input, function (err, torrent) { + if (err) return errorAndExit(err) + if (argv.out) { + fs.writeFileSync(argv.out, torrent) + } else { + process.stdout.write(torrent) + } + }) } function download (torrentId) { diff --git a/test/cmd.js b/test/cmd.js index 2cd0024..812bc96 100644 --- a/test/cmd.js +++ b/test/cmd.js @@ -78,5 +78,26 @@ test('Command line: webtorrent info magnet_uri', function (t) { }) }) +test('Command line: webtorrent create /path/to/file', function (t) { + t.plan(1) + + var leavesPath = __dirname + '/content/Leaves of Grass by Walt Whitman.epub' + + var child = cp.spawn(CMD, [ 'create', leavesPath ]) + child.on('error', function (err) { + throw err + }) + + var chunks = [] + child.stdout.on('data', function (chunk) { + chunks.push(chunk) + }) + child.stdout.on('end', function () { + var buf = Buffer.concat(chunks) + var parsedTorrent = parseTorrent(new Buffer(buf, 'binary')) + t.deepEqual(parsedTorrent.infoHash, 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36') + }) +}) + // TODO: test 'webtorrent download /path/to/torrent' // TODO: test 'webtorrent download magnet_uri' |