gpt4 book ai didi

node.js - 在构建脚本中使用 util.promisfy 无法按预期工作

转载 作者:太空宇宙 更新时间:2023-11-04 00:05:16 24 4
gpt4 key购买 nike

我在 Node 中有一个构建脚本,它在某一时刻重写了指向 ./dist 引用的链接中的所有 ./src 引用。我一直在阅读 util.promisfy 并尝试将 async/await 合并到我的脚本中,但它没有按计划工作。我可以将我的文件从/src 移动到/dist 目录,然后 uglify index.js 并压缩我的图像,但使用 Node fs 函数读取和写入我的 index.html 文件来更新链接不起作用。以下是 build.js 的相关部分:

const fs = require("fs");
const fc = require("file-copy");
const { promisify } = require("util");
const copyFileAsync = promisify(fs.access); // convert fs.access to a promise
const readFileAsync = promisify(fs.readFile); // convert fs.readFile to a promise
const confirmWriteAsync = promisify(fs.stat); // convert fs.stat to a promise;
const writeFileAsync = promisify(fs.writeFile); // convert fs.writeFile to a promise

const mkdirp = require("mkdirp");
// Compressed and uglified files here ok //

// ============ Copy index.html to dist/index.html(copyIndexHtml) ============ //
/* jshint ignore:start */
const copyIndexFile = async function(result) {
try {
console.log(result);
await copyFileAsync(
"./index.html",
fs.constants.R_OK | fs.constants.W_OK
)
await fc("./index.html", "./dist/index.html");
} catch (err) {
console.log("ERROR:", err);
}
return "Copied Index.html to /dist!";
}; // end copyIndexFile
/* jshint ignore:end */

// ================== End copyIndexFile ================ //

// ====== Read data from dist/index.html(getData) =============== //


/* jshint ignore:start */
const getData = async function(result) {
console.log(result);

// Lets update dist/index.html file src and href links to reflect new location.
console.log(
"index.html: Redoing file links to reflect move to /dist folder."
);
try {
const fileContents = await readFileAsync("./dist/index.html", {
encoding: "utf8"
});
console.log("CONTENT:", fileContents);

// check and replace both src= and href= links to reflect chenge to dist/ folder
// Notice we chained .replace to do it
const regEx1 = /src\s*=\s*"\.\/src\//gi;
const regEx2 = /src\s*=\s*'\.\/src\//gi;
const regEx3 = /href\s*=\s*"\.\/src\//gi;
const regEx4 = /href\s*=\s*'\.\/src\//gi;

let distIndexHtml = fileContents
.replace(regEx1, 'src="./')
.replace(regEx2, "src='./")
.replace(regEx3, 'href="./')
.replace(regEx4, "href='./");

console.log(distIndexHtml);

// Confirm Write to index.html
await confirmWriteAsync("./dist/index.html", function(err, stats) {
if (err) {
console.log(`Error: ${err}`);
} else if (stats.size === 0) {
console.log(`Error copying index.html!!!!!!`);
} else {
console.log(
`Succesfully copied to dist\index.html. File size is ${
stats.size
}`
);
}
});

await writeFileAsync(
"dist/index.html",
distIndexHtml,
"utf8",
err => {
if (err) {
reject(err);
} else {
resolve("Write to dist//index.html OK.");
}
}
);
} catch (err) {
console.log("ERROR:", err);
}
return "Read /dist/index.html file!!";
};
/* jshint ignore:end */

// ==================================================== //
// ========== Call promise chain ====================== //
// ==================================================== //
browserifyJS()
.then(
result => {
return compressImages(result);
},
err => {
console.log(err);
return compressImages(err);
}
)
.then(result => {
return copyIndexFile(result);
})
.then(result => {
return getData(result);
})
.then(result => {
console.log(result);
});
} // mkdirp else end
}); // mkdirp callback end
}); // rimraf callback end

修正后更新了控制台读数:控制台读数为:

    npm run build

> sports-page-app@1.0.0 build C:\Users\akillian\Local Documents\GitHub\sports-page-app
> node ./scripts/build

main.css: build and uglify
Checking for index.js
Bundling Successful!
Images Compressed!!!
/dist/index.js: build and uglify
Copied Index.html to /dist!
index.html: Redoing file links to reflect move to /dist folder.
CONTENT:

Succesfully copied to distindex.html. File size is 3059
~\Local Documents\GitHub\sports-page-app [refactor-app ≡ +0 ~1 -0 !]

内容现在为空。

最佳答案

你有这个:

      await copyFileAsync(
"./index.html",
fs.constants.R_OK | fs.constants.W_OK,
err => {
if (err) {
console.log("No index.html file present!");
} else {
// Note: fc() returns a promise and no .catch()
// This also means the code continues on without
// waiting for the file to copy.
fc("./index.html", "./dist/index.html");
}
}
);

但是如果你 promise 了,就没有错误回调函数。简单地说:

      await copyFileAsync(
"./index.html",
fs.constants.R_OK | fs.constants.W_OK
)
await fc("./index.html", "./dist/index.html");

你的 try/catch 将会得到错误。

此外,对于特定的 try/catch return "Copied Index.html to/dist!"; 总是返回,即使存在错误,所以我建议创建两个 return 语句,一个在trycatch 中的一个,或者如果不重要的话使用 finally

关于node.js - 在构建脚本中使用 util.promisfy 无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52613160/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com