-
Notifications
You must be signed in to change notification settings - Fork 0
/
files-write.js
73 lines (66 loc) · 1.71 KB
/
files-write.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
70
71
72
73
'use strict'
const assert = require('power-assert')
const Promise = require('bluebird')
const _ = require('lodash')
const TYPES = require('../types')
const utils = require('../utils')
// files write
// ===========
//
// This tests the `ipfs files write` command.
//
// The execution consists of
//
// 1. Running `ipfs files mkdir <dir>` for all needed directories
// 2. Running `ipfs files write <path> <content>` for all the files
//
// The Validation consists of
//
// 1. For all files
// * Running `ipfs files read <path>`
// * Assert the result is equal to the content added
function filesToDirs (files) {
return _(files)
.map('path')
.sort()
.flatMap((file) => {
// '/my/file/path.exe'
return _(file.split('/'))
// ['', 'my', 'file', 'path.exe']
.compact()
// ['my', 'file', 'path.exe']
.dropRight()
// ['my', 'file']
.reduce((acc, val) => {
acc.push((_.last(acc) || '') + '/' + val)
return acc
}, [])
// ['/my', '/my/file']
})
.uniq()
.value()
}
module.exports = {
cmd: 'files write',
args: [TYPES.TREE_NESTED_1],
parallel: true,
exec (ipfs, tree) {
const files = tree[0]
const dirs = filesToDirs(files)
return Promise
.mapSeries(dirs, (dir) => ipfs.files.mkdir(dir))
.then(() => Promise.map(files, (file) => {
return ipfs.files.write(file.path, new Buffer(file.content), {create: true})
}))
.then(() => files)
},
validator (ipfs, args, out) {
const files = args[0]
return Promise
.map(files, (file) => ipfs.files.read(file.path))
.map(utils.collect)
.map((res, i) => {
assert(res.toString() === files[i].content)
})
}
}