This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathget.js
69 lines (63 loc) · 1.61 KB
/
get.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'use strict'
const Command = require('ronin').Command
const debug = require('debug')
const utils = require('../../utils')
const log = debug('cli:files')
log.error = debug('cli:files:error')
var fs = require('fs')
const pathj = require('path')
module.exports = Command.extend({
desc: 'Download IPFS objects',
options: {},
run: (path, options) => {
let dir
let filepath
let ws
if (!path) {
throw new Error("Argument 'path' is required")
}
if (!options) {
options = {}
dir = process.cwd()
} else {
if (options.slice(-1) !== '/') {
options += '/'
}
dir = options
}
utils.getIPFS((err, ipfs) => {
if (err) {
throw err
}
ipfs.files.get(path, (err, data) => {
if (err) {
throw err
}
data.on('file', (data) => {
if (data.path.lastIndexOf('/') === -1) {
filepath = data.path
if (data.dir === false) {
ws = fs.createWriteStream(pathj.join(dir, data.path))
data.stream.pipe(ws)
} else {
try {
fs.mkdirSync(pathj.join(dir, data.path))
} catch (err) {
throw err
}
}
} else {
filepath = data.path.substring(0, data.path.lastIndexOf('/') + 1)
try {
fs.mkdirSync(pathj.join(dir, filepath))
} catch (err) {
throw err
}
ws = fs.createWriteStream(pathj.join(dir, data.path))
data.stream.pipe(ws)
}
})
})
})
}
})