Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
chore: convert all commands return resolvable promises
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: achingbrain <alex@achingbrain.net>
  • Loading branch information
achingbrain committed Jun 6, 2018
1 parent 25a5465 commit fd22958
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 104 deletions.
12 changes: 7 additions & 5 deletions src/cli/cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ module.exports = {
hash
} = argv

return ipfs.files.cp(source, dest, {
parents,
format,
hashAlg: hash
})
argv.resolve(
ipfs.files.cp(source, dest, {
parents,
format,
hashAlg: hash
})
)
}
}
4 changes: 3 additions & 1 deletion src/cli/flush.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module.exports = {
ipfs
} = argv

return ipfs.files.flush(path || FILE_SEPARATOR, {})
argv.resolve(
ipfs.files.flush(path || FILE_SEPARATOR, {})
)
}
}
66 changes: 34 additions & 32 deletions src/cli/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,45 +30,47 @@ module.exports = {
long
} = argv

return ipfs.files.ls(path || FILE_SEPARATOR)
.then(files => {
if (long) {
const table = []
const lengths = {}

files.forEach(link => {
const row = {
name: `${link.name}`,
hash: `${link.hash}`,
size: `${link.size}`
}

Object.keys(row).forEach(key => {
const value = row[key]

lengths[key] = lengths[key] > value.length ? lengths[key] : value.length
argv.resolve(
ipfs.files.ls(path || FILE_SEPARATOR)
.then(files => {
if (long) {
const table = []
const lengths = {}

files.forEach(link => {
const row = {
name: `${link.name}`,
hash: `${link.hash}`,
size: `${link.size}`
}

Object.keys(row).forEach(key => {
const value = row[key]

lengths[key] = lengths[key] > value.length ? lengths[key] : value.length
})

table.push(row)
})

table.push(row)
})
table.forEach(row => {
let line = ''

table.forEach(row => {
let line = ''
Object.keys(row).forEach(key => {
const value = row[key]

Object.keys(row).forEach(key => {
const value = row[key]
line += value.padEnd(lengths[key])
line += '\t'
})

line += value.padEnd(lengths[key])
line += '\t'
print(line)
})

print(line)
})
return
}

return
}

files.forEach(link => print(link.name))
})
files.forEach(link => print(link.name))
})
)
}
}
14 changes: 8 additions & 6 deletions src/cli/mkdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ module.exports = {
flush
} = argv

return ipfs.files.mkdir(path, {
parents,
cidVersion,
hash,
flush
})
argv.resolve(
ipfs.files.mkdir(path, {
parents,
cidVersion,
hash,
flush
})
)
}
}
10 changes: 6 additions & 4 deletions src/cli/mv.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ module.exports = {
recursive
} = argv

return ipfs.files.mv(source, dest, {
parents,
recursive
})
argv.resolve(
ipfs.files.mv(source, dest, {
parents,
recursive
})
)
}
}
44 changes: 23 additions & 21 deletions src/cli/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,30 @@ module.exports = {
length
} = argv

return new Promise((resolve, reject) => {
waterfall([
(cb) => ipfs.files.readPullStream(path, {
offset,
length
}, cb),
(stream, cb) => {
pull(
stream,
through(buffer => {
print(buffer, false)
}),
collect(cb)
)
}
], (error) => {
if (error) {
return reject(error)
}
argv.resolve(
new Promise((resolve, reject) => {
waterfall([
(cb) => ipfs.files.readPullStream(path, {
offset,
length
}, cb),
(stream, cb) => {
pull(
stream,
through(buffer => {
print(buffer, false)
}),
collect(cb)
)
}
], (error) => {
if (error) {
return reject(error)
}

resolve()
resolve()
})
})
})
)
}
}
8 changes: 5 additions & 3 deletions src/cli/rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ module.exports = {
recursive
} = argv

return ipfs.files.rm(path, {
recursive
})
argv.resolve(
ipfs.files.rm(path, {
recursive
})
)
}
}
38 changes: 20 additions & 18 deletions src/cli/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,27 @@ Type: <type>`,
withLocal
} = argv

return ipfs.files.stat(path, {
withLocal
})
.then((stats) => {
if (hash) {
return print(stats.hash)
}
argv.resolve(
ipfs.files.stat(path, {
withLocal
})
.then((stats) => {
if (hash) {
return print(stats.hash)
}

if (size) {
return print(stats.size)
}
if (size) {
return print(stats.size)
}

print(format
.replace('<hash>', stats.hash)
.replace('<size>', stats.size)
.replace('<cumulsize>', stats.cumulativeSize)
.replace('<childs>', stats.blocks)
.replace('<type>', stats.type)
)
})
print(format
.replace('<hash>', stats.hash)
.replace('<size>', stats.size)
.replace('<cumulsize>', stats.cumulativeSize)
.replace('<childs>', stats.blocks)
.replace('<type>', stats.type)
)
})
)
}
}
30 changes: 16 additions & 14 deletions src/cli/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,21 @@ module.exports = {
flush
} = argv

return ipfs.files.write(path, process.stdin, {
offset,
length,
create,
truncate,
rawLeaves,
cidVersion,
hashAlg,
format,
parents,
progress,
strategy,
flush
})
argv.resolve(
ipfs.files.write(path, process.stdin, {
offset,
length,
create,
truncate,
rawLeaves,
cidVersion,
hashAlg,
format,
parents,
progress,
strategy,
flush
})
)
}
}

0 comments on commit fd22958

Please sign in to comment.