-
Notifications
You must be signed in to change notification settings - Fork 222
/
Copy pathtroubleshooting.js
42 lines (40 loc) · 1.16 KB
/
troubleshooting.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
const fs = require('fs');
const path = require('path');
const diff = require('./diff');
/**
* Format troubleshooting message
* This function reads the troubleshooting file and replaces kewords
* by data providen in `data` object
*
* @param {String} file Path to troubleshooting file:
* @see ../i18n/troubleshooting/
* @param {Object} data Object with corresponding data
* @return {String}
*/
const format = (file, data) => fs.readFileSync(file, 'utf8')
// Replace breaking characters
.replace(/'/g, "'")
.replace(/"/g, '"')
.replace(/</g, '<')
.replace(/>/g, '>')
// Inject data
.replace(/%solution%/g, data.solution)
.replace(/%attempt%/g, data.attempt)
.replace(/%diff%/g, diff(data.attempt, data.solution))
.replace(/%filename%/, data.filename);
/**
* Compose fail message
* @param {Object} data Object with data that's needed for formating
* @return {Array}
*/
module.exports = function troubleshooting(data) {
return [{
text: format(data.troubleshooting, data),
type: 'md',
}, {
text: '---',
type: 'md',
}, {
file: path.join(__dirname, '..', 'i18n', 'footer', '{lang}.md'),
}];
};