-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy path_b2cDeployData.js
More file actions
81 lines (71 loc) · 2.95 KB
/
_b2cDeployData.js
File metadata and controls
81 lines (71 loc) · 2.95 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
75
76
77
78
79
80
81
'use strict';
const fs = require('fs'),
path = require('path'),
moment = require('moment'),
// Initialize constants
config = require('config'),
// Include local libraries
fsAPI = require('../../lib/_common/fs'),
// Include B2C Commerce API functions
b2cAuthenticate = require('../apis/ci/_authenticate'),
deploymentAPI = require('../apis/ci');
/**
* @function _b2cDeployData
* @description This function is used to deploy the metadata to the B2C Commerce instance.
* environment -- leveraging SFCC-CI's API to do the work.
*
* @param {Object} environmentDef Represents the already-validated environment details to use when performing the actions
* @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 output properties collected during deployment
*/
module.exports = (environmentDef, pathScope, pathElement) => new Promise(async (resolve, reject) => {
// Roll-up the validation results to a single object
const output = {
apiCalls: {
authenticate: {},
upload: {},
import: {}
},
outputDisplay: {
authenticate: {}
}
};
// Ensure the zip file exists
const archiveName = fsAPI.getDeployArchiveName(environmentDef, pathElement);
const archivePath = path.join(fsAPI.getDeployPath(pathScope, pathElement), archiveName);
if (!fs.existsSync(archivePath)) {
reject(`${config.get('errors.b2c.cannotFindMetadataArchive')}`);
return;
}
// Authenticate first
try {
// Audit the authorization token for future rest requests
output.apiCalls.authenticate.authToken = await b2cAuthenticate(environmentDef);
output.outputDisplay.authenticate.authToken = output.apiCalls.authenticate.authToken;
} catch (e) {
reject(`${config.get('errors.b2c.unableToAuthenticate')}`);
return;
}
// Then upload the archive to the B2C Commerce instance
try {
output.apiCalls.upload = await deploymentAPI.upload(environmentDef, archivePath, output.apiCalls.authenticate.authToken);
} catch (e) {
reject(`${config.get('errors.b2c.unableToUploadMetadataArchive')}`);
return;
}
// Then import the archive to the B2C Commerce instance
try {
output.apiCalls.import = await deploymentAPI.import(environmentDef, archiveName, output.apiCalls.authenticate.authToken);
output.outputDisplay.import = [
output.apiCalls.import.job_id,
output.apiCalls.import.status,
moment(new Date(output.apiCalls.import.end_time)).format('MM/DD/YY HH:MM:SS'),
moment(new Date(output.apiCalls.import.end_time)).format('MM/DD/YY HH:MM:SS')
];
} catch (e) {
reject(`${config.get('errors.b2c.unableToImportMetadataArchive')}`);
return;
}
resolve(output);
});