-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy path08.js
147 lines (124 loc) · 6.13 KB
/
08.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import all from 'it-all'
import utils from '../utils'
const validate = async (result, ipfs) => {
// check that right directories are there with no loose files in root
let rootDirectoryContents = await all(ipfs.files.ls('/'))
let rootIsEmpty = rootDirectoryContents.length === 0
let rootContainsOnlySome = rootDirectoryContents.length === 1 && rootDirectoryContents[0].name === 'some'
let someContainsOnlyStuff = null
if (rootContainsOnlySome) {
let someDirectoryContents = await all(ipfs.files.ls('/some'))
someContainsOnlyStuff = someDirectoryContents.length === 1 && someDirectoryContents[0].name === 'stuff'
}
// identify files that should have been moved
let uploadedFiles = window.uploadedFiles || []
let uploadedFilenames = uploadedFiles.map(file => file.name.toString()).sort()
// check whether user returned the contents of /some/stuff
let someStuffFiles = null
let returnedSomeStuffContents = null
let someStuffFilenames = null
let itemsMatch = null
let itemsAreFiles = null
if (!rootIsEmpty && rootContainsOnlySome && someContainsOnlyStuff) {
someStuffFiles = await all(ipfs.files.ls('/some/stuff'))
someStuffFilenames = someStuffFiles.map(file => file.name.toString()).sort()
returnedSomeStuffContents = JSON.stringify(result) === JSON.stringify(someStuffFiles)
// check whether contents of /some/stuff are the right files
itemsMatch = JSON.stringify(someStuffFilenames) === JSON.stringify(uploadedFilenames)
itemsAreFiles = someStuffFiles.every(file => file.type === 'file')
}
if (!result) {
return { fail: 'You forgot to return a result. Did you accidentally edit the return statement?' }
} else if (uploadedFiles.length === 0) {
// Shouldn't happen because you can't hit submit without uploading files
return { fail: 'Oops! You forgot to upload files to work with :(' }
} else if (result instanceof Error && result.message === 'Unexpected token const') {
return {
fail: 'Oops! Looks like you forgot to assign a value to `filesToMove` or `filepathsToMove`',
overrideError: true
}
} else if (result instanceof Error && result.message === 'await is only valid in async function') {
return {
fail: "Oops! `await` is only valid in an async function. Perhaps you ran `file.mv` multiple times and didn't wrap it in a single async function? See our suggestion for passing in an array so you can make a single call to `files.mv`.",
overrideError: true
}
} else if (result instanceof Error && result.message === 'ipfs.mv is not a function') {
return {
fail: 'Oops! Did you type `ipfs.mv` instead of `ipfs.files.mv`?',
overrideError: true
}
} else if (utils.validators.isAsyncIterable(result)) {
return {
fail: utils.validationMessages.VALUE_IS_ASYNC_ITERABLE_ALL
}
} else if (rootIsEmpty) {
return { fail: 'Your root directory is empty. Did you accidentally move the `some/stuff` directory? Remember to test whether each item is a file (`type === \'file\'`) before moving it.' }
} else if (result instanceof Error && result.code === utils.ipfs.errorCodes.ERR_INVALID_PATH) {
return {
fail: 'Invalid path. Did you use just the file name when attempting to move each file? Remember to start the path with a leading `/`.',
overrideError: true
}
} else if (!returnedSomeStuffContents) {
return { fail: 'It looks like you returned something other than the contents of the `/some/stuff` directory. Did you accidentally edit the return statement?' }
} else if (!rootContainsOnlySome) {
return {
fail: 'Your root directory should now contain only your `/some` directory, but something else is there.',
logDesc: "Here's what's in your root directory:",
log: rootDirectoryContents.map(utils.format.ipfsObject)
}
} else if (!someContainsOnlyStuff) {
return {
fail: 'Your `/some` directory should now contain only your `/stuff` directory, but something else is there.',
logDesc: "Here's what's in your `/some` directory:",
log: (await all(ipfs.files.ls('/some'))).map(utils.format.ipfsObject)
}
} else if (!itemsAreFiles) {
return { fail: 'Uh oh. It looks like your `/some/stuff` directory contains a directory. It should only include files.' }
} else if (!itemsMatch) {
return { fail: "Uh oh. It looks the contents of your `/some/stuff` directory don't match your uploaded files." }
} else if (itemsMatch && itemsAreFiles) {
return {
success: utils.validationMessages.SUCCESS,
logDesc: 'This is the data that is now in your `/some/stuff` directory in IPFS:',
log: someStuffFiles.map(utils.format.ipfsObject)
}
}
}
const code = `/* global ipfs, all */
const run = async (files) => {
await Promise.all(files.map(f => ipfs.files.write('/' + f.name, f, { create: true })))
await ipfs.files.mkdir('/some/stuff', { parents: true })
const rootDirectoryContents = await all(ipfs.files.ls('/'))
const filepathsToMove = // create an array of the paths of the files to be moved (excluding directories)
// move all the files in filepathsToMove into /some/stuff
const someStuffDirectoryContents = await all(ipfs.files.ls('/some/stuff'))
return someStuffDirectoryContents
}
return run
`
const solution = `/* global ipfs, all */
const run = async (files) => {
await Promise.all(files.map(f => ipfs.files.write('/' + f.name, f, { create: true })))
await ipfs.files.mkdir('/some/stuff', { parents: true })
const rootDirectoryContents = await all(ipfs.files.ls('/'))
const filepathsToMove = rootDirectoryContents.filter(file => file.type === 'file').map(file => '/' + file.name)
await ipfs.files.mv(filepathsToMove, '/some/stuff')
// // alternatively, wrapping multiple mv calls into a single async function with await:
// const filesToMove = rootDirectoryContents.filter(item => item.type === 'file')
// await Promise.all(filesToMove.map(file => {
// return ipfs.files.mv('/' + file.name, '/some/stuff')
// }))
const someStuffDirectoryContents = await all(ipfs.files.ls('/some/stuff'))
return someStuffDirectoryContents
}
return run
`
const options = {
overrideErrors: true
}
export default {
validate,
code,
solution,
options
}