* Generic function used by readFile and readFileAssets
(filepath: string, encodingOrOptions: ?string, command: Function)
| 145 | * Generic function used by readFile and readFileAssets |
| 146 | */ |
| 147 | function readFileGeneric(filepath: string, encodingOrOptions: ?string, command: Function) { |
| 148 | var options = { |
| 149 | encoding: 'utf8' |
| 150 | }; |
| 151 | |
| 152 | if (encodingOrOptions) { |
| 153 | if (typeof encodingOrOptions === 'string') { |
| 154 | options.encoding = encodingOrOptions; |
| 155 | } else if (typeof encodingOrOptions === 'object') { |
| 156 | options = encodingOrOptions; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return command(normalizeFilePath(filepath)).then((b64) => { |
| 161 | var contents; |
| 162 | |
| 163 | if (options.encoding === 'utf8') { |
| 164 | contents = utf8.decode(base64.decode(b64)); |
| 165 | } else if (options.encoding === 'ascii') { |
| 166 | contents = base64.decode(b64); |
| 167 | } else if (options.encoding === 'base64') { |
| 168 | contents = b64; |
| 169 | } else { |
| 170 | throw new Error('Invalid encoding type "' + String(options.encoding) + '"'); |
| 171 | } |
| 172 | |
| 173 | return contents; |
| 174 | }); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Generic function used by readDir and readDirAssets |
no test coverage detected