-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy path_b2cDeployCode.js
More file actions
120 lines (103 loc) · 4.28 KB
/
_b2cDeployCode.js
File metadata and controls
120 lines (103 loc) · 4.28 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
'use strict';
const fs = require('fs');
const path = require('path');
// Initialize constants
const config = require('config');
// Include local libraries
const fsAPI = require('../../lib/_common/fs');
const common = require('../../lib/cli-api/_common');
// Include B2C Commerce API functions
const b2cAuthenticate = require('../apis/ci/_authenticate');
const deploymentAPI = require('../apis/ci');
const codeVersions = require('../apis/ci/code-versions');
/**
* @function _b2cDeployCode
* @description This function is used to deploy the code 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 deployment result
*/
module.exports = (environmentDef, pathScope, pathElement) =>
new Promise(async (resolve, reject) => {
// Roll-up the validation results to a single object
const output = {
apiCalls: {
authenticate: {},
deploy: {},
codeVersionActivate: {},
codeVersionGet: {}
},
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.cannotFindCodeArchive')}`);
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')}: ${e}`);
return;
}
// Then deploy the archive to the B2C Commerce instance
try {
output.apiCalls.deploy = await deploymentAPI.deploy(
environmentDef,
archivePath,
output.apiCalls.authenticate.authToken
);
} catch (e) {
console.log(e);
reject(`${config.get('errors.b2c.unableToDeployCodeArchive')}: ${e}`);
return;
}
// Then activate the code version
try {
output.apiCalls.codeVersionActivate.activationResult = await codeVersions.activate(
environmentDef,
output.apiCalls.authenticate.authToken,
output.apiCalls.deploy
);
output.outputDisplay.codeVersionActivate = output.apiCalls.codeVersionActivate.activationResult;
} catch (e) {
console.log(e);
reject(`${config.get('errors.b2c.unableToActivateCodeVersion')}: ${e}`);
return;
}
// Finally get the code version details
try {
const getDetailResults = await codeVersions.getDetail(
environmentDef,
output.apiCalls.authenticate.authToken,
output.apiCalls.deploy
);
output.apiCalls.codeVersionGet.getDetailResults = common.createCodeVersionSummary([getDetailResults.data])[0];
// Prepare the data to be displayed in the output
output.outputDisplay.codeVersionGet = [
output.apiCalls.codeVersionGet.getDetailResults.id,
output.apiCalls.codeVersionGet.getDetailResults.active,
output.apiCalls.codeVersionGet.getDetailResults.lastModificationTime,
output.apiCalls.codeVersionGet.getDetailResults.compatibilityMode,
output.apiCalls.codeVersionGet.getDetailResults.webDavUrl
];
} catch (e) {
reject(`${config.get('errors.b2c.unableToRetrieveCodeVersion')}`);
return;
}
resolve(output);
});