![](/img/trans.png)
[英]How to Async combine multiple JSON arrays into 1 with Node.js javascript
[英]How to combine multiple javascript files to 1 file using node.js
大家好,
我創建了一個包含 10 多個 js 文件的大型 JavaScript 項目。 現在我想將所有 js 文件的代碼合並到 1 個文件中。 我用 node.js 創建了一個測試文件,但它什么也沒做,我不知道問題是什么。
var fs = require("fs"); var codes = ["M:/HTML-Projekte/ChabanicSouls/combineFunc/file1.js","M:/HTML-Projekte/ChabanicSouls/combineFunc/file2.js","M:/HTML-Projekte/ChabanicSouls/combineFunc/file3.js"]; var combined = ""; for(let x = 0; x < codes.length; x++) { fs.readFile(codes[x], "UTF-8", function(err, data) { if (err) { throw err; } combined += data; }); } fs.writeFile('M:/HTML-Projekte/ChabanicSouls/combineFunc/ugly.js', combined , function(err) { if(err) { return console.log(err); } console.log("The file was saved!"); });
如果您有 Idee,如何解決此問題,請告訴我。
我找到了自己解決問題的方法,
var fs = require("fs");
const codes = ["M:/HTML-Projekte/ChabanicSouls/original_Codes/chaVar.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaSocket.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaChat.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaVarMain.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaField.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaFriends.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaArchive.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaMain.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaTimer.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaActions.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaFight.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaKeys.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaLvUp.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaMarket.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaMove.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaNextPl.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaParaUp.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaSoul.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaGetFunc.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaStart.js"];
function readIt() {
let combined = [];
let doneCheck = [];
let errVal = false;
for (let x = 0; x < codes.length; x++) {
doneCheck.push(false);
}
for (let x = 0; x < codes.length; x++) {
fs.readFile(codes[x], "UTF-8", function (err, data) {
if (err || !data) {
console.log(codes[x]);
} else {
combined.push(data);
doneCheck[x] = true;
}
if (x == (codes.length - 1)) {
saveIt(combined, doneCheck);
}
});
}
}
function saveIt(combined, doneCheck, round = 0) {
let counter = 0;
for (let x = 0; x < doneCheck.length; x++) {
if (doneCheck[x] == false) {
fs.readFile(codes[x], "UTF-8", function (err, data) {
if (err || !data) {
console.log(codes[x]);
} else {
combined.push(data);
doneCheck[x] = true;
}
if (x == (codes.length - 1)) {
return saveIt(combined, doneCheck, round);
}
});
} else {
counter++;
}
}
if (counter < doneCheck.length) {
return false;
}
let combined_string = "";
for (let y = 0; y < combined.length; y++) {
combined_string = combined_string + combined[y] + " ";
}
fs.writeFile('M:/HTML-Projekte/ChabanicSouls/ugly.js', combined_string, function (err) {
if (err) {
return readIt();
}
console.log("The file was saved!");
});
}
readIt();
這種方式非常有效:-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.