-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.js
124 lines (108 loc) · 2.99 KB
/
add.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* eslint-disable no-await-in-loop */
const debug = require('debug')('ara-filesystem:add')
const mirror = require('mirror-folder')
const ignored = require('./lib/ignore')
const { create } = require('./create')
const isFile = require('is-file')
const mkdirp = require('mkdirp')
const { access } = require('fs')
const pify = require('pify')
const {
join,
basename,
resolve,
relative
} = require('path')
/**
* Adds one or more files to the AFS
* @param {Object} opts
* @param {String} opts.did
* @param {String} opts.password
* @param {Object} [opts.keyringOpts]
* @param {Boolean} opts.force
* @param {Array} opts.paths
* @return {Object}
*/
async function add(opts) {
if (null === opts.did || 'string' !== typeof opts.did || !opts.did) {
throw new TypeError('Expecting non-empty DID.')
} else if (null === opts.password || 'string' !== typeof opts.password || !opts.password) {
throw new TypeError('Password required to continue.')
} else if (null === opts.paths || (!(opts.paths instanceof Array)
&& 'string' !== typeof opts.paths) || 0 === opts.paths.length) {
throw new TypeError('Expecting one or more filepaths to add.')
}
const {
keyringOpts,
password,
force,
did
} = opts
let { paths } = opts
if ('string' === typeof opts.paths) {
paths = [ opts.paths ]
}
let afs
try {
({ afs } = await create({ did, password, keyringOpts }))
} catch (err) {
throw err
}
await mirrorPaths(paths)
debug('full copy complete')
debug(await afs.readdir(afs.HOME))
return afs
async function mirrorPaths(p) {
for (const path of p) {
try {
await pify(access)(resolve(path))
await mirrorPath(path)
} catch (err) {
debug('%s does not exist', path)
}
}
}
async function mirrorPath(path) {
debug(`copy start: ${path}`)
const name = join(afs.HOME, basename(path))
// Check if file
if (!(await pify(isFile)(path)) && !ignore(path)) {
await pify(mkdirp)(name, { fs: afs })
}
// Mirror and log
const progress = mirror({ name: path }, { name, fs: afs }, { keepExisting: true, ignore })
progress.on('put', (src, dst) => {
debug(`adding path ${dst.name}`)
})
progress.on('skip', (src, dst) => {
debug(`skipping path ${dst.name}`)
})
progress.on('ignore', (src, dst) => {
debug(`ignoring path ${dst.name}. Use '--force' to force add file`)
})
progress.on('del', (dst) => {
debug(`deleting path ${dst.name}`)
})
// Await end or error
const error = await new Promise((accept, reject) => progress.once('end', accept).once('error', reject))
if (error) {
debug(`copy error: ${path}: ${error}`)
} else {
debug(`copy complete: ${path}`)
}
}
function ignore(path) {
path = relative('/', path)
if (ignored.ignores(path)) {
if (force) {
debug(`forcing add path ${path}`)
return false
}
return true
}
return false
}
}
module.exports = {
add
}