-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace.js
More file actions
137 lines (63 loc) · 1.77 KB
/
Copy pathreplace.js
File metadata and controls
137 lines (63 loc) · 1.77 KB
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
var fs = require('fs');
var startTime = new Date();
console.log('Starte Skript');
var counter = 0;
var getFileNames = function getFileNames(fileString){
var files = fileString.split("\n");
return files;
};
var openFiles = function openFiles(fileName ,next){
fs.readFile(fileName , 'utf-8' ,function (err, data) {
if (err) console.error(err);
next(fileName, data);
});
};
var replaceString = function replaceString(fileName, data){
if(typeof(data) == 'string' && data.length > 3){
if(process.argv.length > 3){
var repString = (process.argv.length === 4) ? "" : process.argv[4];
var regArg = new RegExp(process.argv[3], 'g');
console.log(regArg);
data = data.replace( regArg, repString);
}
fs.writeFile(fileName, data ,'utf-8',function (err) {
if (err) console.error(err);
console.log(fileName+' saved!');
});
}
};
var stInFunc = function(){
process.stdin.resume(); // stdio
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (chunk) {
getFileNames(chunk).forEach(function(fileName){
//console.log('name ist: '+fileName);
if(fileName !== ''){
counter++;
openFiles(fileName, replaceString);
}
});
});
process.stdin.on('end', function () {
console.log('end');
});
};
var get_time_diff = function(start , end){
return end.getTime() - start.getTime() + 'ms';
};
//ARGUMENTS
if(process.argv.length > 2){
if(process.argv[2] !== '' && process.argv[2] !== 'pipe'){
openFiles(process.argv[2] , replaceString);
}else{
stInFunc();
}
}else{
stInFunc();
}
process.on('exit', function () {
process.nextTick(function () {
console.log('This will not run');
});
console.log(counter +' Dateien in '+get_time_diff(startTime, new Date())+' geändert');
});