1 | const path = require('path');
|
2 |
|
3 | const { uploadFile } = require('./api/fileManager');
|
4 | const { walk } = require('./lib/walk');
|
5 | const { logger } = require('./logger');
|
6 | const { createIgnoreFilter } = require('./ignoreRules');
|
7 | const escapeRegExp = require('./lib/escapeRegExp');
|
8 | const { convertToUnixPath } = require('./path');
|
9 | const {
|
10 | ApiErrorContext,
|
11 | logApiUploadErrorInstance,
|
12 | isFatalError,
|
13 | } = require('./errorHandlers');
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | async function uploadFolder(portalId, src, dest, { cwd }) {
|
23 | const regex = new RegExp(`^${escapeRegExp(src)}`);
|
24 | const files = await walk(src);
|
25 |
|
26 | const filesToUpload = files.filter(createIgnoreFilter(cwd));
|
27 |
|
28 | const len = filesToUpload.length;
|
29 | for (let index = 0; index < len; index++) {
|
30 | const file = filesToUpload[index];
|
31 | const relativePath = file.replace(regex, '');
|
32 | const destPath = convertToUnixPath(path.join(dest, relativePath));
|
33 | logger.debug('Attempting to upload file "%s" to "%s"', file, destPath);
|
34 | try {
|
35 | await uploadFile(portalId, file, destPath);
|
36 | logger.log('Uploaded file "%s" to "%s"', file, destPath);
|
37 | } catch (error) {
|
38 | logger.error('Uploading file "%s" to "%s" failed', file, destPath);
|
39 | if (isFatalError(error)) {
|
40 | throw error;
|
41 | }
|
42 | logApiUploadErrorInstance(
|
43 | error,
|
44 | new ApiErrorContext({
|
45 | portalId,
|
46 | request: destPath,
|
47 | payload: file,
|
48 | })
|
49 | );
|
50 | }
|
51 | }
|
52 | }
|
53 |
|
54 | module.exports = {
|
55 | uploadFolder,
|
56 | };
|