-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy path_b2cZip.js
More file actions
74 lines (57 loc) · 2.68 KB
/
_b2cZip.js
File metadata and controls
74 lines (57 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'use strict';
const fsextra = require('fs-extra');
const path = require('path');
// Initialize constants
const config = require('config');
// Include local libraries
const fsAPI = require('../../lib/_common/fs');
/**
* @function _b2cZip
* @description Attempts to create a .zip directory and archive the contents.
*
* @param {Object} [environmentDef] Represents the environment definition arguments passed to this method
* @param {String} pathScope Describes the scope for the deployment folder (sfcc vs sfsc)
* @param {String} pathElement Describes the parent sub-folder to be processed
* @returns {Promise} Returns the results of the zipArchive attempt
*/
module.exports = (environmentDef, pathScope, pathElement) =>
new Promise(async (resolve, reject) => {
// Initialize the output property
const output = {
outputDisplay: [],
archiveName: null,
paths: {
sourcePath: config.get(`paths.source.b2c.${pathElement}`),
targetPath: fsAPI.getDeployPath(pathScope, pathElement)
},
verify: null,
errors: null
};
// Default the initial collection of output paths
output.archiveName = fsAPI.getDeployArchiveName(environmentDef, pathElement);
output.paths.tempRootPath = path.join(config.get('paths.deploy.base'), '_temp');
output.paths.tempPath = path.join(
output.paths.tempRootPath,
output.archiveName.replace(path.extname(output.archiveName),
''));
// Build out the archive path including the file-name
output.paths.fullPath = output.paths.targetPath + output.archiveName;
try {
// Validate the target folder exists
output.verify = await fsAPI.verifyAndCreateFolder(output.paths.targetPath, true);
output.verify = output.verify && await fsAPI.verifyAndCreateFolder(output.paths.tempPath, true);
// Copy the content of the source folder into the target folder
fsextra.copySync(output.paths.sourcePath, output.paths.tempPath);
// Zip the directory results -- and render the results
const success = await fsAPI.zipDirectory(output.paths.tempRootPath, output.paths.fullPath);
// Remove the temporary created folder
fsextra.rmdirSync(output.paths.tempRootPath, {recursive: true});
// Audit the archive processing results
output.outputDisplay.push(output.paths.fullPath);
output.outputDisplay.push(pathElement);
output.outputDisplay.push(success);
resolve(output);
} catch (e) {
reject(e);
}
});