-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
149 lines (139 loc) · 4.23 KB
/
gulpfile.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
145
146
147
148
149
import gulp from "gulp";
import phpMinify from "@cedx/gulp-php-minify";
import fs from "fs";
import path from "path";
import UglifyPHP from "uglify-php";
import glob from "glob";
const assets = [
'./image/default.png',
'./image/default-admin.png',
'./image/default-admin-bad.png',
'./image/console.png',
'./image/server.png',
'./errores.txt'
];
const nodb = process.argv.slice(2).find((v)=>v.indexOf("--nodb") !== -1) !== undefined;
const minify = process.argv.slice(2).find((v)=>v.indexOf("--min") !== -1) !== undefined;
function wait(time) {
return new Promise((resolve)=>setTimeout(resolve, time));
}
function copyRecursiveSync(src, dest) {
var exists = fs.existsSync(src);
var stats = exists && fs.statSync(src);
var isDirectory = exists && stats.isDirectory();
if (isDirectory) {
fs.mkdirSync(dest);
fs.readdirSync(src).forEach(function(childItemName) {
copyRecursiveSync(path.join(src, childItemName),
path.join(dest, childItemName));
});
} else {
fs.copyFileSync(src, dest);
}
}
function compressPhp(finish) {
const dirs = [
"./**/*.php",
"!node_modules/**/*.php",
"!libs/**/*.php",
"!build/**/*.php",
"!test.php"
];
let dirBuild = (minify)? ".tmp_compress": "build";
if (nodb) dirs.push("!scripts/database.php");
return gulp.src(dirs, { read: false })
.pipe(phpMinify({ binary: "C:\\xampp\\php\\php.exe" }))
//.pipe(phpMinify({ mode: "safe" }))
.pipe(gulp.dest(dirBuild))
.on('end', finish);
}
function copyFiles() {
copyRecursiveSync("./libs", "./build/libs");
console.log('Copiando: "./libs" => "./build/libs"');
assets.forEach((v)=>{
const dest = `./build/${v.replace('./', '')}`;
console.log(`Copiando: ${v} => ${dest}`);
copyRecursiveSync(v, dest);
});
}
function clearBuild() {
return new Promise(async(resolve)=>{
if (fs.existsSync("./build")) {
fs.rmdirSync("./build", { force: true, recursive: true });
await wait(2000);
}
if (fs.existsSync("./.tmp_compress")) {
fs.rmdirSync("./.tmp_compress", { force: true, recursive: true });
await wait(2000);
}
fs.mkdirSync("./build");
fs.mkdirSync("./build/image");
fs.mkdirSync("./build/tmp_files");
fs.mkdirSync("./build/removes");
fs.mkdirSync("./build/olds");
await wait(500);
resolve();
});
}
async function minifyAll() {
const options = {
excludes: [
'$GLOBALS',
'$_SERVER',
'$_GET',
'$_POST',
'$_FILES',
'$_REQUEST',
'$_SESSION',
'$_ENV',
'$_COOKIE',
'$php_errormsg',
'$HTTP_RAW_POST_DATA',
'$http_response_header',
'$argc',
'$argv',
'$this'
],
minify: {
replace_variables: false,
remove_whitespace: true,
remove_comments: true,
minify_html: false
}
}
const existFolder = (path)=>{
if (!fs.existsSync(path)) {
fs.mkdirSync(path);
}
};
glob("./.tmp_compress/**/*.php", async(err, files)=>{
if (err) return console.log('Error');
for (let i = 0; i < files.length; i++) {
const file = files[i];
// Check dir
const checkDir = file.slice(0, file.lastIndexOf('/') + 1).replace('.tmp_compress', 'build');
existFolder(checkDir);
const nameFile = file.slice(file.lastIndexOf('/') + '/'.length, file.length);
console.log(`Minify: ${nameFile}`);
await UglifyPHP.minify(file, {
...options,
output: file.replace('.tmp_compress', 'build')
});
}
await wait(1000);
fs.rmdirSync("./.tmp_compress", { force: true, recursive: true });
});
}
(async()=>{
console.log("Limpiando...");
await clearBuild();
console.log("Copiando archivos...");
copyFiles();
console.log("Procesando...");
compressPhp(()=>{
if (minify) {
console.log("Minify...");
minifyAll();
}
});
})();