@ethersphere/swarm-cli
Version:
CLI tool for Bee
97 lines (74 loc) • 3.23 kB
text/typescript
import { MantarayNode } from '@ethersphere/bee-js'
import chalk from 'chalk'
import fs from 'fs'
import { Argument, LeafCommand, Option } from 'furious-commander'
import { join, parse } from 'path'
import { exit } from 'process'
import { directoryExists } from '../../utils'
import { BzzAddress, makeBzzAddress } from '../../utils/bzz-address'
import { RootCommand } from '../root-command'
export class Download extends RootCommand implements LeafCommand {
public readonly name = 'download'
public readonly description = 'Download manifest content to a folder'
public address!: BzzAddress
public bzzUrl!: string
public destination!: string
public stdout!: boolean
public act!: boolean
public actTimestamp!: string
// required if act is true
public actHistoryAddress!: string
// required if act is true
public actPublisher!: string
public async run(): Promise<void> {
super.init()
// can be already set from other command
if (!this.address) {
this.address = await makeBzzAddress(this.bee, this.bzzUrl)
}
const node = await MantarayNode.unmarshal(this.bee, this.address.hash)
await node.loadRecursively(this.bee)
const nodes = node.collect().filter(x => x.fullPathString.startsWith(this.address.path || ''))
if (nodes.length === 0) {
this.console.error('No files found under the given path')
exit(1)
}
if (this.stdout && nodes.length > 1) {
this.stdout = false
}
for (const node of nodes) {
await this.downloadNode(node, this.address)
}
}
private async downloadNode(node: MantarayNode, address: BzzAddress): Promise<void> {
if (!this.stdout && !this.quiet) {
if (this.curl) {
this.console.log(chalk.dim(node.fullPathString))
} else {
process.stdout.write(chalk.dim(node.fullPathString))
}
}
const parsedForkPath = parse(node.fullPathString)
const data = await this.bee.downloadData(node.targetAddress)
if (this.stdout) {
process.stdout.write(data.toUtf8())
return
}
const destination = this.destination || address.hash
const destinationFolder = join(destination, parsedForkPath.dir)
if (!directoryExists(destinationFolder)) {
await fs.promises.mkdir(destinationFolder, { recursive: true })
}
if (!this.stdout && !this.quiet && !this.curl) {
process.stdout.write(' ' + chalk.green('OK') + '\n')
}
await fs.promises.writeFile(join(destination, node.fullPathString), data.toUint8Array())
}
}