forked from SalesforceCommerceCloud/b2c-crm-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_sfTemplateCreator.js
More file actions
61 lines (46 loc) · 2.12 KB
/
_sfTemplateCreator.js
File metadata and controls
61 lines (46 loc) · 2.12 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
'use strict';
// Initialize constants
const fs = require('fs');
const path = require('path');
const config = require('config');
// Include the helper library to retrieve the environment details
const createSFTemplateInstance = require('./_common/_createSFTemplateInstance');
/**
* @function _sfTemplateCreator
* @description Attempts to create a version of the given template file, based on the environment
* definition from the given command object.
*
* @param {Object} environmentDef Represents the environment configuration details
* @param {String} templateFolder Represents the parent folder where templates reside
* @param {String} templateSuffix Represents the suffix to apply to a given template file
* @param {String} fileSuffix Represents the suffix to apply to a given file (template instance) being rendered
* @param {Function} transformator Represents the function used to transform the template to an instance
* @returns {Promise} Returns the result of the template-creation request
*/
module.exports = (environmentDef, templateFolder, templateSuffix, fileSuffix, transformator) => new Promise((resolve, reject) => {
const output = {};
try {
// Build out the template path so we can read-in the template as a string
const templatePath = path.join(
config.get('paths.source.dx.templates').toString(),
templateFolder,
templateSuffix);
// Read in the template file
let templateFileAsString = fs.readFileSync(templatePath, 'utf8');
// Transform the template with the environment definition values
templateFileAsString = transformator(templateFileAsString);
// Create and write the template instance in question
const filePath = createSFTemplateInstance(
templateFolder,
fileSuffix,
templateFileAsString);
// Default the output properties
output.success = true;
// Seed the filePath and contents properties
output.filePath = filePath;
output.fileContents = templateFileAsString;
resolve(output);
} catch (e) {
reject(e);
}
});