Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

add dest dir for download #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Example:

Receive files on host B:

> dcp <generated public key>
> dcp <generated public key> [dest]
```

### Sending files
Expand All @@ -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] <generated public key>
> dcp [-n] [-v] [--skip-prompt] <generated public key> [dest]
```

* Use `-n`/`--dry-run` to see what files will be received
Expand Down
4 changes: 2 additions & 2 deletions src/dcp.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ program.on('--help', function() {
`)
console.log(' Receive files on host B:')
console.log(`
> dcp <generated public key>
> dcp <generated public key> [dest]
`)
})

Expand All @@ -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)
}
Expand Down
9 changes: 9 additions & 0 deletions src/lib/dat-cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ export default class DatCp {
})
}

async setDownloadDest(path) {
Copy link
Owner

@tom-james-watson tom-james-watson Sep 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think instead of needing to make a separate call to this, there should just be an optional dest property in the options passed to the constructor which causes this method to be called.

try {
await fs.lstat(path)
} catch (err) {
await fs.mkdir(path)
}
process.chdir(path)
}

download() {
return new Promise((resolve) => {
const abort = setTimeout(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
9 changes: 7 additions & 2 deletions src/receive.js
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is causing a couple of issues:

  • If you do something like npm run cli -- <key> out -n, you end up with a out/out/file. It only works as expected if you provide the --skip-prompt flag.
  • A dry run is still creating the out directory. A dry run should make no changes to the filesystem.

I don't think this call actually does anything anyway - I would just remove it.

await datCpDryRun.download()

if (options.dryRun || datCpDryRun.files === 0) {
Expand All @@ -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) {
Expand Down
22 changes: 22 additions & 0 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
})

})