UNPKG

1.57 kBJavaScriptView Raw
1const path = require('path');
2
3const { uploadFile } = require('./api/fileManager');
4const { walk } = require('./lib/walk');
5const { logger } = require('./logger');
6const { createIgnoreFilter } = require('./ignoreRules');
7const escapeRegExp = require('./lib/escapeRegExp');
8const { convertToUnixPath } = require('./path');
9const {
10 ApiErrorContext,
11 logApiUploadErrorInstance,
12 isFatalError,
13} = require('./errorHandlers');
14
15/**
16 *
17 * @param {number} portalId
18 * @param {string} src
19 * @param {string} dest
20 * @param {object} options
21 */
22async 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
54module.exports = {
55 uploadFolder,
56};