( projectDir: string, input: T, authToken?: string, ipfs?: IPFSHTTPClientLite )
| 98 | |
| 99 | /* Recursively finds all FileReferences in an object and replaces the files with IPFS references */ |
| 100 | async function replaceFileReferences<T extends Record<string, any>>( |
| 101 | projectDir: string, |
| 102 | input: T, |
| 103 | authToken?: string, |
| 104 | ipfs?: IPFSHTTPClientLite |
| 105 | ): Promise<T> { |
| 106 | if (Array.isArray(input)) { |
| 107 | return (await Promise.all( |
| 108 | input.map((val) => replaceFileReferences(projectDir, val, authToken, ipfs)) |
| 109 | )) as unknown as T; |
| 110 | } else if (typeof input === 'object' && input !== null) { |
| 111 | if (input instanceof Map) { |
| 112 | input = mapToObject(input) as T; |
| 113 | } |
| 114 | if (isFileReference(input)) { |
| 115 | const filePath = path.resolve(projectDir, input.file); |
| 116 | const content = fs.readFileSync(path.resolve(projectDir, input.file)); |
| 117 | input.file = await uploadFile({content: content.toString(), path: filePath}, authToken, ipfs).then( |
| 118 | (cid) => `ipfs://${cid}` |
| 119 | ); |
| 120 | } |
| 121 | const keys = Object.keys(input).filter((key) => key !== '_deployment') as unknown as (keyof T)[]; |
| 122 | await Promise.all( |
| 123 | keys.map(async (key) => { |
| 124 | // this is the loop |
| 125 | input[key] = await replaceFileReferences(projectDir, input[key], authToken, ipfs); |
| 126 | }) |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | return input; |
| 131 | } |
| 132 | |
| 133 | const fileMap = new Map<string | fs.ReadStream, Promise<string>>(); |
| 134 |
no test coverage detected