-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·101 lines (81 loc) · 3.21 KB
/
server.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
#!/usr/bin/env node
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var fs = require('fs');
var migrate = require('./src/migration').migrate;
var program = require('commander');
var argv = require('minimist')(process.argv.slice(2));
var optionsMap = require('./src/options-map').options;
program.option('-p, --port <number>', 'server port')
.option('-t, --tag-manager', 'add google tag manager to page')
.action(function(cmd, options){
console.log('exec "%s" using %s mode', cmd, options.exec_mode);
});
program.parse(process.argv);
if (!process.argv.slice(2).length) {
program.outputHelp()
} else {
init();
}
function init() {
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.get('/', function (req, res) {
var normalPath = path.normalize(__dirname + '/index.html');
// html content
var content = fs.readFileSync(normalPath, 'utf-8');
// add tag manager
if (argv.t) {
content = addTagManager(content);
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(content);
res.end();
});
app.post('/migrate', function (req, res) {
var code;
for (option in optionsMap) {
if (optionsMap.hasOwnProperty(option)) {
if (req.body[option]) {
addProcessArgv(option);
} else if (option === 'path' || option === 'replacer' || option === 'bundle') {
removeProcessArgv(option);
}
}
}
try {
code = migrate(req.body.code);
} catch (err) {
code = {};
code.code = req.body.code;
code.error = true;
}
// response
res.send(code);
function addProcessArgv(opt) {
if (!~process.argv.indexOf(optionsMap[opt])) {
process.argv.push(optionsMap[opt], req.body[opt]);
} else {
process.argv[process.argv.indexOf(optionsMap[opt]) + 1] = req.body[opt];
}
}
function removeProcessArgv(opt) {
var pos = process.argv.indexOf(optionsMap[opt]);
if (~pos) {
process.argv.splice(pos, 2);
}
}
});
app.listen(argv.p, function () {
console.log('Start server on port: ' + argv.p);
});
}
function addTagManager(code) {
var tagManager = "<script>(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', 'GTM-5B8NXZ');</script>"
var _tagManager = '<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-5B8NXZ" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>';
code = code.replace('</head>', tagManager + '</head>');
code = code.replace('<body>', '<body>' + _tagManager);
return code
}