Currently, I am working on looping over objects within a key:value array in JavaScript to use the string.prototype.replace method for paths to JS files in GulpJS concat tasks.
The objective is to generate a list of paths that GULP can utilize, but they require processing before that. I am replacing the 'placeholder' values in the paths array with their corresponding substitution values from the subs array through a loop until all paths are converted to literal form.
An issue arises in identifying the paths that remain unprocessed by the loop, such as '../test4' (literal paths), so I can handle them separately after completion.
This is the initial progress with some sample data for demonstration:
function updatePaths(substitutionsArray, pathArray) {
var updatedPaths = [];
pathArray.forEach(function (path, index, array) {
console.log('Processing line ' + (index + 1) + '... ' + path);
Object.keys(substitutionsArray).forEach(function (placeholder) {
var substitution = substitutionsArray[placeholder];
var fixedPath = path.replace(placeholder, substitution);
if (fixedPath == path) {
console.log('No replacement found in path ' + path + ' for placeholder ' + placeholder);
} else {
console.log('Replacement discovered in path ' + path + ' for placeholder ' + placeholder + ', resulting in ' + fixedPath);
updatedPaths.push(fixedPath);
}
}
)
});
console.log(updatedPaths);
return updatedPaths;
}
var substitutions = {
"@COMMON_JS@": "/test1",
"@COMMON_ROOT@": "/test2",
"@COMPONENT_ROOT@": "/test3",
"@COMPONENT_WEB_ROOT@": "/components"
};
var filepaths = ['@COMMON_JS@/test1', '@COMMON_ROOT@/test2', '@COMPONENT_ROOT@/test3', '../test4', '../../test5'];
output = updatePaths(substitutions, filepaths);
console.log(output);
return;
If you have any recommendations on how to approach this task more effectively, please let me know.
Thanks in advance - Dave