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 1 commit
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
2 changes: 1 addition & 1 deletion src/dcp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions src/lib/dat-cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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) => {
Expand Down
7 changes: 6 additions & 1 deletion 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);
await datCpDryRun.download()

if (options.dryRun || datCpDryRun.files === 0) {
Expand Down