diff --git a/README.md b/README.md index c327d6c..a6836ff 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ Example: Receive files on host B: - > dcp + > dcp [dest] ``` ### Sending files @@ -108,7 +108,7 @@ Pass an arbitrary set of files or directories to `dcp` to be copied. Copy the ge Invoke `dcp` with the generated public key to receive the copied files. ```bash -> dcp [-n] [-v] [--skip-prompt] +> dcp [-n] [-v] [--skip-prompt] [dest] ``` * Use `-n`/`--dry-run` to see what files will be received diff --git a/src/dcp.js b/src/dcp.js index d078a8b..3f10bc2 100644 --- a/src/dcp.js +++ b/src/dcp.js @@ -14,7 +14,7 @@ program.on('--help', function() { `) console.log(' Receive files on host B:') console.log(` - > dcp + > dcp [dest] `) }) @@ -38,7 +38,7 @@ if (program.verbose) { } if (program.args[0].length === 64) { - receive(program.args[0], program) + receive(program.args, program) } else { send(program.args, program) } diff --git a/src/lib/dat-cp.js b/src/lib/dat-cp.js index e0897de..cacbaa4 100644 --- a/src/lib/dat-cp.js +++ b/src/lib/dat-cp.js @@ -135,6 +135,15 @@ export default class DatCp { }) } + async setDownloadDest(path) { + try { + await fs.lstat(path) + } catch (err) { + await fs.mkdir(path) + } + process.chdir(path) + } + download() { return new Promise((resolve) => { const abort = setTimeout(() => { diff --git a/src/lib/fs.js b/src/lib/fs.js index 01707ee..9ac95b9 100644 --- a/src/lib/fs.js +++ b/src/lib/fs.js @@ -24,7 +24,7 @@ function lstat(path) { function mkdir(path) { return new Promise((resolve, reject) => { - fs.mkdir(path, (err) => { + fs.mkdir(path, {recursive: true}, (err) => { if (err) { return reject(err) } diff --git a/src/receive.js b/src/receive.js index e0f3b9f..185ef81 100644 --- a/src/receive.js +++ b/src/receive.js @@ -1,12 +1,17 @@ import Dat from './lib/dat' import DatCp from './lib/dat-cp' -export default async function receive(key, options) { +export default async function receive(args, options) { + + let key = args[0] + let path = args[1] ? args[1] : '.' + const dat = await Dat({key, sparse: true}) if (!options.skipPrompt) { const datCpDryRun = new DatCp(dat, {...options, dryRun: true}) + await datCpDryRun.setDownloadDest(path) await datCpDryRun.download() if (options.dryRun || datCpDryRun.files === 0) { @@ -22,7 +27,7 @@ export default async function receive(key, options) { } const datCpDownload = new DatCp(dat, {...options}) - + await datCpDownload.setDownloadDest(path) await datCpDownload.download() if (options.skipPrompt || datCpDownload.files > 30) { diff --git a/test/integration.js b/test/integration.js index dc78bfd..578039e 100644 --- a/test/integration.js +++ b/test/integration.js @@ -114,4 +114,26 @@ describe('Integration', function() { expect(getOutFiles('complex')).to.deep.equal(['hello.txt']) }) + it('should save the file in the dest path', async function () { + this.timeout(10000) + const key = await spawnSend('test/fixtures/simple/hello.txt') + await spawnRcv(`--skip-prompt ${key} dir1`) + + expect(getOutFiles('dir1')).to.deep.equal(['hello.txt']) + }) + + it('should create the dest path and save files/dir', async function () { + this.timeout(10000) + const key = await spawnSend('test/fixtures/dirs -r') + await spawnRcv(`${key} --skip-prompt root/test/`) + + expect(getOutFiles()).to.deep.equal(['root']) + expect(getOutFiles('root')).to.deep.equal(['test']) + expect(getOutFiles('root/test')).to.deep.equal(['dirs']) + expect(getOutFiles('root/test/dirs')).to.deep.equal(['dir1', 'dir2']) + expect(getOutFiles('root/test/dirs/dir1')).to.deep.equal(['hello.txt']) + expect(getOutFiles('root/test/dirs/dir2')).to.deep.equal(['dir3', 'foo.txt']) + expect(getOutFiles('root/test/dirs/dir2/dir3')).to.deep.equal(['fizz.txt']) + }) + })