-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
114 lines (96 loc) · 3.59 KB
/
index.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
102
103
104
105
106
107
108
109
110
111
112
113
114
/*!
* manage-htaccess
*
* Copyright (c) 2016, kroko / Reinis Adovics.
* Licensed under the MIT License.
*/
'use strict';
// custom functions for attributes
// attributes function always expects string to manipulate and whole attributes object to work with
// this is for port changing
// function attributesRemoveEmptyLines (string, attributes) {
// // just pass through, illustrates the concept
// return string;
// }
var attributesPortRewrite = function (string, attributes) {
if (('port' in attributes)) {
// seach for port in string that is assumed to look like this
// RewriteRule ^abc\/def/(.+) http://dev.warp.lv:3334/path/to/abc/def/$1 [P]
// we could do it better, right now just search for first :NUMBER nad replace it
var newString = string.replace(/:\d+/g, ':' + attributes.port);
return newString;
}
return string;
};
/**
* Htaccess managing function
* Reference:
* @param {Array} Array containing objects that describe tags, their state
* @param {String} Path to .htaccess file
* @param {String} Special string (.htaccess comment) that is used for enabling/disabling blocks
* @return
*/
function manageHtaccess (conf, htaccessPath, specialChars) {
htaccessPath = typeof htaccessPath !== 'undefined' ? htaccessPath : '.htaccess';
specialChars = typeof specialChars !== 'undefined' ? specialChars : '#%!';
if (conf.length < 1) {
console.warn('manage-htaccess', '\x1b[36m', 'configuration is empty', '\x1b[0m');
}
var fs = require('fs');
var file;
try {
file = fs.readFileSync(htaccessPath).toString();
} catch (e) {
console.error('manage-htaccess', '\x1b[36m', 'ERR!', '\x1b[0m', e);
return;
}
file = file.split('\n');
var specialCharsValid = (specialChars.indexOf('#') === 0) ? specialChars : '#' + specialChars;
for (var cIdx = 0; cIdx < conf.length; ++cIdx) {
if (!('tag' in conf[cIdx]) || !('enabled' in conf[cIdx])) {
console.error('manage-htaccess', '\x1b[36m', 'ERR!', '\x1b[0m', 'configuration must have tag and enabled keys, skipping');
continue;
}
var tag = conf[cIdx].tag;
var enabled = conf[cIdx].enabled;
var tagOpen = specialCharsValid + '<' + tag + '>';
var tagClose = specialCharsValid + '</' + tag + '>';
var attributes = {};
if ('attributes' in conf[cIdx]) {
attributes = conf[cIdx].attributes;
console.log('manage-htaccess', '\x1b[36m', tag, 'has attributes', '\x1b[0m');
}
var pingPong = true;
var lastOpenLineIdx = -1;
for (var lIdx = 0; lIdx < file.length; ++lIdx) {
// file[fIdx] = attributesRemoveEmptyLines(file[lIdx], attributes);
if (pingPong) {
if (file[lIdx].indexOf(tagOpen) >= 0) {
lastOpenLineIdx = lIdx;
pingPong = !pingPong;
}
} else {
if (file[lIdx].indexOf(tagClose) >= 0) {
for (var fIdx = lastOpenLineIdx + 1; fIdx < lIdx; ++fIdx) {
if (enabled) {
// remove first occurance of specialChars
file[fIdx] = file[fIdx].replace(specialChars, '');
file[fIdx] = attributesPortRewrite(file[fIdx], attributes);
} else {
if (file[fIdx].indexOf(specialChars) < 0) {
// comment out only if specialChar is not already there
file[fIdx] = specialChars + file[fIdx];
file[fIdx] = attributesPortRewrite(file[fIdx], attributes);
}
}
}
pingPong = !pingPong;
lastOpenLineIdx = -1;
}
}
}
}
var output = file.join('\n');
fs.writeFileSync(htaccessPath, output);
}
module.exports = manageHtaccess;