From 7534f1c01be09cfe19a2968f072ddbd6b101a6b5 Mon Sep 17 00:00:00 2001 From: bsystem64 Date: Thu, 20 Aug 2020 14:07:25 -0300 Subject: [PATCH 1/2] add dest dir for download add dest as param for download --- src/dcp.js | 2 +- src/lib/dat-cp.js | 24 ++++++++++++++++++++++++ src/receive.js | 7 ++++++- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/dcp.js b/src/dcp.js index d078a8b..24f99e4 100644 --- a/src/dcp.js +++ b/src/dcp.js @@ -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..6341bd4 100644 --- a/src/lib/dat-cp.js +++ b/src/lib/dat-cp.js @@ -135,6 +135,17 @@ export default class DatCp { }) } + async setDownloadDest(path){ + try{ + await fs.lstat(path); + } + catch(err){ + await this.downloadDestPathPrompt(path); + } + + process.chdir(path); + } + download() { return new Promise((resolve) => { const abort = setTimeout(() => { @@ -260,6 +271,19 @@ export default class DatCp { return proceed } + async downloadDestPathPrompt(path) { + const answer = await prompt( + `\nThe path ${path} dont exist, would you like create it? [Y/n] ` + ) + const proceed = ['yes', 'y', ''].includes(answer.trim().toLowerCase()) + if (proceed) { + return await fs.mkdir(path); + } + process.exit(1) + + } + + readdir(path) { return new Promise((resolve, reject) => { this.dat.archive.readdir(path, async (err, paths) => { diff --git a/src/receive.js b/src/receive.js index e0f3b9f..c61f6a4 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) { From d2947c12e3e2ce738b76f354c42e5e7fd287a1ba Mon Sep 17 00:00:00 2001 From: bsystem64 Date: Fri, 21 Aug 2020 16:30:16 -0300 Subject: [PATCH 2/2] fix break dest dir if use the option --skip-prompt --- README.md | 4 ++-- src/dcp.js | 2 +- src/lib/dat-cp.js | 27 ++++++--------------------- src/lib/fs.js | 2 +- src/receive.js | 8 ++++---- test/integration.js | 22 ++++++++++++++++++++++ 6 files changed, 36 insertions(+), 29 deletions(-) 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 24f99e4..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] `) }) diff --git a/src/lib/dat-cp.js b/src/lib/dat-cp.js index 6341bd4..cacbaa4 100644 --- a/src/lib/dat-cp.js +++ b/src/lib/dat-cp.js @@ -135,15 +135,13 @@ export default class DatCp { }) } - async setDownloadDest(path){ - try{ - await fs.lstat(path); - } - catch(err){ - await this.downloadDestPathPrompt(path); + async setDownloadDest(path) { + try { + await fs.lstat(path) + } catch (err) { + await fs.mkdir(path) } - - process.chdir(path); + process.chdir(path) } download() { @@ -271,19 +269,6 @@ export default class DatCp { return proceed } - async downloadDestPathPrompt(path) { - const answer = await prompt( - `\nThe path ${path} dont exist, would you like create it? [Y/n] ` - ) - const proceed = ['yes', 'y', ''].includes(answer.trim().toLowerCase()) - if (proceed) { - return await fs.mkdir(path); - } - process.exit(1) - - } - - readdir(path) { return new Promise((resolve, reject) => { this.dat.archive.readdir(path, async (err, paths) => { 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 c61f6a4..185ef81 100644 --- a/src/receive.js +++ b/src/receive.js @@ -3,15 +3,15 @@ import DatCp from './lib/dat-cp' export default async function receive(args, options) { - let key = args[0]; - let path = args[1] ? args[1] : '.'; + 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.setDownloadDest(path) await datCpDryRun.download() if (options.dryRun || datCpDryRun.files === 0) { @@ -27,7 +27,7 @@ export default async function receive(args, 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']) + }) + })