-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappPhpSyntax.js
62 lines (56 loc) · 1.55 KB
/
appPhpSyntax.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
#!/usr/bin/env node
var fs = require('fs');
var path = require('path');
// Lectura de parametros pasados e inicializamos variables requeridas
var options = process.argv.slice(2);
var dir;
var ext = ['html','js','py','php'];
var regexp = /(<\?)(\W|$)/g;
//Asignamos la ruta del directoria según los parametros o se asigna la actual
if ( options.length >= 1 ) {
dir = options[0];
}else{
dir = '.';
}
//Llamada inical a la lectura del directorio
readDir(dir+path.sep);
//Lectura del directorio
function readDir (dirpath) {
fs.readdir(dirpath, function(err, files){
if ( files.length > 0 ) {
files.forEach(function(file){
fs.stat(dirpath+file,function(err,stats){
if ( stats && stats.isDirectory() ) {
readDir(dirpath+file+path.sep);
}
if( stats && stats.isFile() ){
var type_ = file.split('.');
if (ext.indexOf(type_[type_.length - 1]) >= 0) {
readfile(dirpath+file);
}
}
});
});
}
});
}
//Lectura del archivo permitido
function readfile (filepath) {
var data = fs.readFileSync(filepath, {encoding: 'utf-8'});
var lines = data.split('\n'),
flag = false;
for (var i = lines.length - 1; i >= 0; i--) {
var re = lines[i].match(regexp);
if ( re ) {
lines[i] = lines[i].replace(regexp,re[0].trim()+"php ","gi");
flag = true;
}
}
if (flag) {
data = lines.join('\n');
fs.writeFile(filepath,data,function(err){
if (err) throw err;
console.log(filepath+" Saved!");
});
}
}