diff --git a/lib/commands/beam.js b/lib/commands/beam.js index c38efef..e4dc3d0 100644 --- a/lib/commands/beam.js +++ b/lib/commands/beam.js @@ -1,80 +1,88 @@ -import Hyperbeam from 'hyperbeam' -import randomWords from 'random-words' -import chalk from 'chalk' - -const FULL_USAGE = ` - The beam command is a general-purpose tool for sending data over the network - according to a secret passphrase. You choose a phrase (try to make it hard-ish - to guess!) and then share the phrase with your recipient. The phrase is only - good for 30-60 minutes. - -On the sending device: - - cat hello.txt | hyp beam "for bob roberts" - -On the receiving device: - - hyp beam "for bob roberts" > ./hello.txt - -This can be really useful for sharing hyper keys between devices. For instance: - - > hyp sync ./my-folder - Creating new hyperdrive... - Source: my-folder/ - Target: hyper://f7145e1bbc0d17705861e996b47422e0ca50a80db9441249bd721ff426b79f2a/ - Begin sync? [y/N] y - Syncing... - Synced - > echo "hyper://f7145e1bbc0d17705861e996b47422e0ca50a80db9441249bd721ff426b79f2a/" \\ - | hyp beam "nobody can guess" -` - -export default { - name: 'beam', - description: 'Send a stream of data over the network.', - usage: { - simple: '[passphrase]', - full: FULL_USAGE - }, - command: async function (args) { - var phrase = args._[0] ? args._.join(' ') : randomWords(3).join(' ') - const beam = new Hyperbeam(phrase) - - if (!args._[0]) { - console.error('[hyperbeam] Generated passphrase:') - console.error('') - console.error(' ', chalk.bold(phrase)) - console.error('') - } - - beam.on('remote-address', function ({ host, port }) { - if (!host) console.error('[hyperbeam] Could not detect remote address') - else console.error('[hyperbeam] Joined the DHT - remote address is ' + host + ':' + port) - if (port) console.error('[hyperbeam] Network is holepunchable \\o/') - }) - - beam.on('connected', function () { - console.error('[hyperbeam] Success! Encrypted tunnel established to remote peer') - }) - - beam.on('end', () => beam.end()) - - process.stdin.pipe(beam).pipe(process.stdout) - process.stdin.unref() - - process.once('SIGINT', () => { - if (!beam.connected) closeASAP() - else beam.end() - }) - - function closeASAP () { - console.error('[hyperbeam] Shutting down beam...') - - const timeout = setTimeout(() => process.exit(1), 2000) - beam.destroy() - beam.on('close', function () { - clearTimeout(timeout) - }) - } - } -} +import Hyperbeam from 'hyperbeam' +import randomWords from 'random-words' +import chalk from 'chalk' +import clipboardy from 'clipboardy' + +const FULL_USAGE = ` + The beam command is a general-purpose tool for sending data over the network + according to a secret passphrase. You choose a phrase (try to make it hard-ish + to guess!) and then share the phrase with your recipient. The phrase is only + good for 30-60 minutes. + +On the sending device: + + cat hello.txt | hyp beam "for bob roberts" + +On the receiving device: + + hyp beam "for bob roberts" > ./hello.txt + +This can be really useful for sharing hyper keys between devices. For instance: + + > hyp sync ./my-folder + Creating new hyperdrive... + Source: my-folder/ + Target: hyper://f7145e1bbc0d17705861e996b47422e0ca50a80db9441249bd721ff426b79f2a/ + Begin sync? [y/N] y + Syncing... + Synced + > echo "hyper://f7145e1bbc0d17705861e996b47422e0ca50a80db9441249bd721ff426b79f2a/" \\ + | hyp beam "nobody can guess" + + +You can copy the generated passphrase with the --copy option + +` + +export default { + name: 'beam', + description: 'Send a stream of data over the network.', + usage: { + simple: '[passphrase]', + full: FULL_USAGE + }, + command: async function (args) { + var phrase = args._[0] ? args._.join(' ') : randomWords(3).join(' ') + const beam = new Hyperbeam(phrase) + + if (!args._[0]) { + console.error('[hyperbeam] Generated passphrase:\n') + console.error(' ', chalk.bold(phrase)) + if (process.argv.indexOf("--copy") != -1){ + console.error("Copying passphrase to clipboard") + clipboardy.writeSync(phrase) +} + console.error('') + } + + beam.on('remote-address', function ({ host, port }) { + if (!host) console.error('[hyperbeam] Could not detect remote address') + else console.error('[hyperbeam] Joined the DHT - remote address is ' + host + ':' + port) + if (port) console.error('[hyperbeam] Network is holepunchable \\o/') + }) + + beam.on('connected', function () { + console.error('[hyperbeam] Success! Encrypted tunnel established to remote peer') + }) + + beam.on('end', () => beam.end()) + + process.stdin.pipe(beam).pipe(process.stdout) + process.stdin.unref() + + process.once('SIGINT', () => { + if (!beam.connected) closeASAP() + else beam.end() + }) + + function closeASAP () { + console.error('[hyperbeam] Shutting down beam...') + + const timeout = setTimeout(() => process.exit(1), 2000) + beam.destroy() + beam.on('close', function () { + clearTimeout(timeout) + }) + } + } +} diff --git a/lib/commands/drive/sync.js b/lib/commands/drive/sync.js index c5e974b..35ba95b 100644 --- a/lib/commands/drive/sync.js +++ b/lib/commands/drive/sync.js @@ -1,6 +1,7 @@ import { join as joinPath } from 'path' import chokidar from 'chokidar' import mkdirp from 'mkdirp' +import clipboardy from 'clipboardy' import dft from 'diff-file-tree' import chalk from 'chalk' import debounce from 'p-debounce' @@ -14,7 +15,7 @@ import { parseHyperUrl } from '../../urls.js' const SYNC_INTERVAL = 1000 const FULL_USAGE = ` Options: - + --copy - Copy the generated hyper:// URI to your clipboard. --no-add - Don't include additions to the target location. --no-overwrite - Don't include overwrites to the target location. --no-delete - Don't include deletions to the target location. @@ -27,7 +28,8 @@ Examples: hyp drive sync ./local-folder hyper://1234..af/remote-folder --no-delete hyp drive sync hyper://1234..af/ hyper://fedc..21/ hyp drive sync hyper://1234...af/ - hyp drive sync ./local-folder + hyp drive sync ./local-folder + hyp drive sync ./local-folder --copy ` export default { @@ -55,6 +57,12 @@ export default { console.error(chalk.bold(`Source: ${leftArgs.raw}`)) console.error(chalk.bold(`Target: ${rightArgs.raw}`)) + if (process.argv.indexOf("--copy") != -1 && !args._[1]){ + console.error("Copied to the url to your clipboard\n") + clipboardy.writeSync(rightArgs.raw) + } + + var ok = true if(!args.yes){ @@ -63,7 +71,7 @@ export default { defaultValue: false }) } - + if (!rightArgs.isHyper) mkdirp.sync(rightArgs.path) if (!ok) process.exit(0) console.error(live ? 'Live syncing (Ctrl+c to exit)...' : 'Syncing...') diff --git a/package.json b/package.json index e7465b6..d2186c9 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "binary-extensions": "^2.1.0", "bytes": "^3.1.0", "chalk": "^1.1.3", + "clipboardy": "^2.3.0", "chokidar": "^3.4.3", "concat-stream": "^2.0.0", "diff-file-tree": "^2.5.1",