nodejs复制文件夹及其下的所有文件
背景代码
背景
假期本想使用nodejs写一个微信小程序转qq小程序的转换工具,写完复制部分之后,发现微信转qq不需要什么操作只需要修改下APPID就行(如果有特殊的部分还是需要进行特殊处理的)。现在把复制的代码放上来,方便大家在针对文件做转换的时候 可以拿来使用。
代码
let fs
= require("fs");
let path
= require("path");
const { exit
} = require("process");
let root
= "";
let outRoot
= "./output";
let transRule
= {
".wxml": ".qml",
".wxss": ".qss",
".wxs": ".qs",
};
function readDir(dir
, outDir
, name
) {
let file
= path
.join(dir
, name
);
if (fs
.statSync(file
).isDirectory()) {
let outFile
= path
.join(outDir
, name
);
if (name
) {
if (!fs
.existsSync(outFile
)) {
fs
.mkdirSync(outFile
);
}
}
fs
.readdirSync(file
).forEach((item
) => {
readDir(file
, outFile
, item
);
});
} else {
let outFile
= path
.join(outDir
, name
);
let pathForamte
= path
.parse(outFile
);
let ext
= transRule
[pathForamte
.ext
];
if (ext
) {
pathForamte
.ext
= ext
;
pathForamte
.base
= pathForamte
.name
+ ext
;
}
let newFilePath
= path
.format(pathForamte
);
fs
.writeFileSync(newFilePath
, transferContent(file
));
}
}
function transferContent(file
) {
let fileContent
= fs
.readFileSync(file
);
let pathForamte
= path
.parse(file
);
if (pathForamte
.ext
== ".wxml") {
} else if (pathForamte
.ext
== ".json") {
if (pathForamte
.base
== "project.config.json") {
let content
= JSON.parse(fileContent
.toString());
content
.qqappid
= "";
delete content
.appid
return JSON.stringify(content
)
}
}
return fileContent
;
}
readDir(root
, outRoot
, "");
转载请注明原文地址:https://blackberry.8miu.com/read-8555.html