-
Notifications
You must be signed in to change notification settings - Fork 36
/
gulpUtil.js
32 lines (30 loc) · 1.06 KB
/
gulpUtil.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
const path = require('path');
const fs = require('fs');
function replacePathsWithContents(obj) {
const promises = [];
function traverseAndReplace(obj) {
for (let key in obj) {
if (typeof obj[key] === 'string') {
const filePath = path.join(__dirname, obj[key]);
promises.push(
new Promise((resolve, reject) => {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
return reject(err);
}
obj[key] = data;
resolve();
});
})
);
} else if (typeof obj[key] === 'object') {
traverseAndReplace(obj[key]);
}
}
}
traverseAndReplace(obj);
return Promise.all(promises).catch(err => {
console.error('Error reading file:', err);
});
}
module.exports = { replacePathsWithContents };