-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove.js
73 lines (63 loc) · 1.85 KB
/
remove.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
/* eslint-disable no-await-in-loop */
const { create } = require('./create')
const debug = require('debug')('ara-filesystem:remove')
const {
join,
resolve
} = require('path')
/**
* Removes one or more files to the AFS
* @param {Object} opts
* @param {String} opts.did
* @param {String} opts.password
* @param {Object} [opts.keyringOpts]
* @param {Array} opts.paths
* @return {Object}
*/
async function remove(opts) {
if (!opts || 'object' !== typeof opts) {
throw new TypeError('Expecting opts object.')
} else if ('string' !== typeof opts.did || !opts.did) {
throw new TypeError('Expecting non-empty did.')
} else if ('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 {
did, password, paths, keyringOpts
} = opts
let afs
try {
({ afs } = await create({ did, password, keyringOpts }))
} catch (err) {
throw err
}
await removeAll(paths)
async function removeAll(files) {
for (const path of files) {
try {
await afs.access(path)
const nestedFiles = await afs.readdir(path)
debug('%s removed from afs', path)
await afs.unlink(path)
if (nestedFiles.length > 0) {
const src = resolve(path)
for (let i = 0; i < nestedFiles.length; i++) {
const file = nestedFiles[i]
nestedFiles[i] = join(src, file)
nestedFiles[i] = nestedFiles[i].replace(process.cwd(), '.')
}
await removeAll(nestedFiles)
}
} catch (err) {
debug('%s does not exist', path)
}
}
}
return afs
}
module.exports = {
remove
}