var
fs=
require(
'fs');
var
walker=
require(
'walker');
var
iconv=
require(
'iconv-lite');
var
cfrom=
'gb2312';
var
cto=
'utf-8';
var
rootpath=
'D:
\\
projections
\\
QOwnNotes
\\
QOwnNotes
\\
src';
walker(
rootpath).
filterDir(
function(
dir,
stat){
if(
/
^
\S
+\.
git
$
/.
test(
dir))
{
console.
log(
'git directory: '+
dir);
return
false;
}
return
true;
}).
on(
'file',
function(
file,
stat){
if(
/
^
\S
+\.
cpp
$
/.
test(
file)||
/
^
\S
+\.
h
$
/.
test(
file)){
var
data=
fs.
readFileSync(
file);
if(
data[
0]==
0xEF&&
data[
1]==
0xBB&&
data[
2]==
0xBF){
console.
log(
cto +
' file: '+
file);
}
else{
console.
log(
cfrom +
' file: '+
file);
var
gb2312=
iconv.
decode(
data,
cfrom);
var
utf8=
iconv.
encode(
gb2312,
cto);
var
bom=
Buffer.
concat([
Buffer.
from([
0xEF,
0xBB,
0xBF],
3),
utf8],
utf8.
length+
3);
fs.
writeFileSync(
file,
bom);
}
}
else{
//console.log('not code: '+file);
}
}).
on(
'error',
function(
err,
entry,
stat){
console.
log(
'error: '+
error+
', '+
entry);
}).
on(
'end',
function(){
console.
log(
'finished.');
});