| 6 | * Command implementation of the `saveRecordingScreen` command. |
| 7 | */ |
| 8 | export async function uploadFile ( |
| 9 | this: WebdriverIO.Browser, |
| 10 | localPath: string |
| 11 | ): Promise<string> { |
| 12 | /** |
| 13 | * parameter check |
| 14 | */ |
| 15 | if (typeof localPath !== 'string') { |
| 16 | throw new Error('number or type of arguments don\'t agree with uploadFile command') |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * check if command is available |
| 21 | */ |
| 22 | if (typeof this.file !== 'function') { |
| 23 | throw new Error(`The uploadFile command is not available in ${(this.capabilities as WebdriverIO.Capabilities).browserName}`) |
| 24 | } |
| 25 | |
| 26 | const zipData: Uint8Array[] = [] |
| 27 | const source = fs.createReadStream(localPath) |
| 28 | |
| 29 | return new Promise((resolve, reject) => { |
| 30 | archiver('zip') |
| 31 | .on('error', (err: Error) => reject(err)) |
| 32 | .on('data', (data: Uint8Array) => zipData.push(data)) |
| 33 | .on('end', () => ( |
| 34 | this.file(Buffer.concat(zipData).toString('base64')) |
| 35 | .then((localPath) => resolve(localPath), reject) |
| 36 | )) |
| 37 | .append(source, { name: path.basename(localPath) }) |
| 38 | .finalize() |
| 39 | }) |
| 40 | } |