-
Notifications
You must be signed in to change notification settings - Fork 25
/
createSettingsFileTransformer.js
121 lines (111 loc) · 3.74 KB
/
createSettingsFileTransformer.js
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
121
/***************************************************************************************
* (c) 2021 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
****************************************************************************************/
var isPlainObject = require('is-plain-object');
var objectAssign = require('@adobe/reactor-object-assign');
function isArrayReference(str) {
return (
typeof str === 'string' &&
str.indexOf('[') !== -1 &&
str.indexOf(']') !== -1
);
}
function sanitizeArrayKey(pathStrSegment) {
return pathStrSegment.substr(
0,
// the name goes up to the start of the array bracket: 'someArrayName[]'
pathStrSegment.indexOf('[')
);
}
/**
* Recursive function to loop through settings and look for the setting to transform,
* which is the final entry within the pathSegments array. Alters settings in-place.
* @param {Array} pathSegments
* @param {Object} settings
* @param {Function} decorateWithDynamicHost
*/
function traverseIntoSettings(pathSegments, settings, decorateWithDynamicHost) {
// nothing to do
if (!pathSegments.length || !isPlainObject(settings)) {
return;
}
var currentKey = pathSegments[0];
// base case
if (pathSegments.length === 1) {
if (
settings.hasOwnProperty(currentKey) &&
typeof settings[currentKey] === 'string'
) {
settings[currentKey] = decorateWithDynamicHost(settings[currentKey]);
}
return;
}
// still more work to do
var remainingPathSegments = pathSegments.slice(1);
if (isArrayReference(currentKey)) {
// 'someArrayReference[]' --> 'someArrayReference'
currentKey = sanitizeArrayKey(currentKey);
var settingsValue = settings[currentKey];
if (Array.isArray(settingsValue)) {
settingsValue.forEach(function (arrayEntryObject) {
return traverseIntoSettings(
remainingPathSegments,
arrayEntryObject,
decorateWithDynamicHost
);
});
}
} else {
// object case
return traverseIntoSettings(
remainingPathSegments,
settings[currentKey],
decorateWithDynamicHost
);
}
}
/**
* Returns a function that when called will attempt to traverse the passed in
* settings object to each file path and transform a relative URL to an absolute
* URL.
*
* @param isDynamicEnforced
* @param decorateWithDynamicHost
* @returns {(function(*=, *=, *=): (*))|*}
*/
module.exports = function (isDynamicEnforced, decorateWithDynamicHost) {
return function (settings, filePaths, moduleReferencePath) {
if (
!isDynamicEnforced ||
!isPlainObject(settings) ||
!Object.keys(settings).length ||
!Array.isArray(filePaths) ||
!filePaths.length
) {
return;
}
// pull out the file paths by the module's reference path and loop over each urlPath
filePaths.forEach(function (filePathString) {
// isExternal is not emitted for extensions that use the fileTransform, however CustomCode
// emits an explicit true or false.
if (settings.isExternal === false) {
return;
}
// modify the object in place
traverseIntoSettings(
filePathString.split('.'),
settings,
decorateWithDynamicHost
);
});
return;
};
};