|
1 | | -import archiver, { ArchiverError } from 'archiver'; |
2 | 1 | import fs from 'fs'; |
3 | | -import path from 'path'; |
4 | | -import os from 'os'; |
| 2 | +import request from 'request'; |
5 | 3 | import log from './../log'; |
6 | | - |
7 | | -const fsPromises = fs.promises; |
8 | | -interface UploadSettings { |
9 | | - cypress_path: string; |
10 | | -} |
| 4 | +import { IConfig } from './config'; |
11 | 5 |
|
12 | 6 | export default class Uploader { |
13 | | - private settings: UploadSettings; |
| 7 | + private config: IConfig; |
14 | 8 |
|
15 | | - constructor(settings: UploadSettings) { |
16 | | - this.settings = settings; |
| 9 | + constructor(config: IConfig) { |
| 10 | + this.config = config; |
17 | 11 | } |
18 | 12 |
|
19 | | - public async start(): Promise<string> { |
| 13 | + public async start(zipFile: string): Promise<string> { |
20 | 14 | return new Promise((resolve, reject) => { |
21 | | - let tempZipFile: string | undefined; |
22 | | - |
23 | | - try { |
24 | | - tempZipFile = path.join(os.tmpdir(), 'upload.zip'); |
25 | | - const output = fs.createWriteStream(tempZipFile); |
26 | | - const archive = archiver('zip', { |
27 | | - zlib: { level: 9 }, |
28 | | - }); |
29 | | - |
30 | | - archive.on('warning', (err: ArchiverError) => { |
31 | | - if (err.code === 'ENOENT') { |
32 | | - log.warn(err); |
| 15 | + const requestOptions = { |
| 16 | + method: 'POST', |
| 17 | + uri: 'https://api.testingbot.com/v1/storage', |
| 18 | + auth: { |
| 19 | + user: this.config.auth.key, |
| 20 | + pass: this.config.auth.secret, |
| 21 | + sendImmediately: true, |
| 22 | + }, |
| 23 | + formData: { |
| 24 | + file: fs.createReadStream(zipFile), |
| 25 | + }, |
| 26 | + }; |
| 27 | + |
| 28 | + request(requestOptions, function (error, response) { |
| 29 | + if (error) { |
| 30 | + return reject(error); |
| 31 | + } |
| 32 | + let responseBody = null; |
| 33 | + if (response) { |
| 34 | + if (response.body && typeof response.body === 'string') { |
| 35 | + response.body = JSON.parse(response.body); |
| 36 | + } |
| 37 | + if (response.statusCode.toString().substring(0, 1) === '2') { |
| 38 | + responseBody = response.body; |
33 | 39 | } else { |
34 | | - reject(err); |
| 40 | + return reject(response.body); |
35 | 41 | } |
36 | | - }); |
37 | | - |
38 | | - // listen for all archive data to be written |
39 | | - // 'close' event is fired only when a file descriptor is involved |
40 | | - output.on('close', () => { |
41 | | - resolve(tempZipFile); |
42 | | - }); |
43 | | - |
44 | | - archive.on('error', (err: ArchiverError) => { |
45 | | - reject(err); |
46 | | - }); |
47 | | - |
48 | | - archive.pipe(output); |
49 | | - |
50 | | - const allowedFileTypes = [ |
51 | | - 'js', |
52 | | - 'json', |
53 | | - 'txt', |
54 | | - 'ts', |
55 | | - 'feature', |
56 | | - 'features', |
57 | | - ]; |
58 | | - allowedFileTypes.forEach((fileType) => { |
59 | | - archive.glob(`**/*.${fileType}`, { |
60 | | - cwd: this.settings.cypress_path, |
61 | | - matchBase: true, |
62 | | - ignore: [ |
63 | | - 'node_modules/**', |
64 | | - 'package-lock.json', |
65 | | - 'package.json', |
66 | | - 'testingbot-package.json', |
67 | | - ], |
68 | | - }); |
69 | | - }); |
70 | | - |
71 | | - archive.finalize(); |
72 | | - } catch (e) { |
73 | | - reject(e); |
74 | | - } finally { |
75 | | - if (tempZipFile) { |
76 | | - fsPromises.unlink(tempZipFile); |
77 | 42 | } |
78 | | - } |
| 43 | + |
| 44 | + resolve(responseBody); |
| 45 | + }); |
79 | 46 | }); |
80 | 47 | } |
81 | 48 | } |
0 commit comments