forked from sixem/ivfi-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.helpers.js
executable file
·144 lines (125 loc) · 2.85 KB
/
build.helpers.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const fs = require('fs');
/**
* Strips the opening/closing tags from code.
* @param {*} data
* @param {*} tags
* @returns
*/
const stripOuterTags = (data, tags) =>
{
let lines = data.split('\n');
let modified = false;
for(let i = 0; i < lines.length; i++)
{
if(lines[i].startsWith(tags[0]))
{
lines.splice(i, 1);
modified = true;
break;
}
}
if(modified)
{
for(let i = lines.length - 1; i >= 0; i--)
{
if(lines[i].startsWith(tags[1]))
{
lines.splice(i, 1);
break;
}
}
data = lines.join('\n');
}
return data;
};
exports.exit = (...message) =>
{
console.log(`\nExiting -`, ...message);
process.exit(1);
}
/**
* Reads the JSON data from a path
*
* @param {string} path
*/
exports.readJson = (path) =>
{
let data = false;
try
{
if(fs.existsSync(path))
{
data = JSON.parse(fs.readFileSync(path));
} else {
exports.exit(`File not found: ${path}`);
}
} catch(error) {
exports.exit(`Error reading: ${path}`, error);
data = null;
}
return data;
};
/**
* Extractors for fetching/reading extra build files
*/
exports.extractors = {
filePhp: (path, options = {}) =>
{
let data = null;
try
{
if(fs.existsSync(path))
{
let buffer = fs.readFileSync(path);
data = buffer.toString();
if(options.stripTags)
{
data = stripOuterTags(data, ['<?php', '?>']);
}
} else {
exports.exit(`File not found: ${path}`);
}
} catch(error) {
exports.exit(`Extraction failed @ ${path}`, error);
}
return data;
},
fileCss: (path, options = {}) =>
{
let data = null;
try
{
if(fs.existsSync(path))
{
let buffer = fs.readFileSync(path);
data = buffer.toString();
if(options.minimize)
{
data = data.
replace(/\n/g, '').
replace(/\s\s+/g, ' ').
replace(/"/g, '\\"').
replace(': ', ':').replace('; ', ';').
replace(' }', '}').replace('} ', '}').
replace('{ ', '{').replace(' {', '{');
}
} else {
exports.exit(`File not found: ${path}`);
}
} catch(error) {
exports.exit(`Extraction failed @ ${path}`, error);
}
return data;
}
};
exports.trimPartPath = (path) =>
{
if(path[0] === '/' || path[0] === '\\')
{
path = path.substring(1);
} else if(path.substring(0, 2) === './')
{
path = path.substring(2);
}
return path;
};