-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbff.js
More file actions
125 lines (105 loc) · 3.44 KB
/
bff.js
File metadata and controls
125 lines (105 loc) · 3.44 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
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
122
123
124
125
const fs = require('fs');
const sh = require('shelljs');
const crypto = require('crypto');
const pingWebHook = require('./build/pingWebHook');
const fetchBlogPosts = require('./build/fetchBlogPosts');
const fetchFooter = require('./build/fetchFooter');
const fetchNavbar = require('./build/fetchNavbar');
const fetchNavtopicsdropdown = require('./build/fetchNavtopicsdropdown');
const delay = 1000;
const runtime = {
pm: [''],
};
if (process.env.PM_TECH) {
sh.exec('mkdir -p public');
Object.keys(runtime).forEach((key) => {
if (runtime[key][0]) {
const fileBuffer = fs.readFileSync(runtime[key][0]);
const hashSum = crypto.createHash('sha1');
const ext = runtime[key][0]
.split('/')
.pop()
.split('.')
.pop();
hashSum.update(fileBuffer);
const hex = hashSum.digest('hex');
runtime[key].push(`_${hex}.${ext}`);
sh.exec(`cp ${runtime[key][0]} public/${runtime[key][1]}`);
}
});
}
const prefetch = async () => {
sh.exec('mkdir -p bff-data');
await pingWebHook();
fetchBlogPosts();
fetchFooter();
fetchNavbar();
fetchNavtopicsdropdown();
let runtimeScript = '';
if (process.env.PM_TECH_RT) {
sh.config.silent = true;
runtimeScript = sh.exec('cat build/runtime.js').stdout;
sh.config.silent = false;
sh.exec('mkdir -p public');
Object.keys(runtime).forEach((key) => {
if (runtime[key][0]) {
const fileBuffer = fs.readFileSync(runtime[key][0]);
const hashSum = crypto.createHash('sha1');
const ext = runtime[key][0]
.split('/')
.pop()
.split('.')
.pop();
hashSum.update(fileBuffer);
const hex = hashSum.digest('hex');
runtime[key].push(`_${hex}.${ext}`);
setTimeout(() => {
sh.exec(`cp ${runtime[key][0]} public/${runtime[key][1]}`);
}, delay);
}
});
}
const UACode = 'G-CX7P9K6W67';
const GTMCode = 'GTM-M42M5N';
const googleTagManager = `(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','${GTMCode}');`;
const script = (process.env.PM_TECH_RT
&& `
${runtimeScript}
setTimeout(function(){
var dnt = (parseInt(navigator.doNotTrack) === 1 || parseInt(window.doNotTrack) === 1 || parseInt(navigator.msDoNotTrack) === 1 || navigator.doNotTrack === "yes");
window.pmt('log', ['navigator.doNotTrack: ' + dnt]);
if(!dnt) {
${googleTagManager}
window.pmt('log', ['attached googletagmanager: ' + '${GTMCode}']);
var d = 1000, int;
var int = setInterval(function(){
if (window.ga) {
var sitename = document.location.hostname;
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
window.gtag = gtag;
gtag('js', new Date());
gtag('config', '${UACode}');
window.pmt('ga', ['${UACode}', sitename]);
window.pmt('log', ['initialized GA: ' + sitename + ' (' + '${UACode}' + ')']);
window._iaq = window._iaq || {};
clearInterval(int);
}
}, d);
}
}, 1000);
`)
|| `
console.info('Postman OSS');
`;
fs.writeFile('bff.json', JSON.stringify({ script }), (err) => {
if (err) {
throw err;
}
});
};
prefetch();