无法使用 Promise、catch 和 gulp 将文件写入 dist 文件夹

Can't writeFile to the dist folder using Promise, catch and gulp

我正在将一个 index.html 文件映射到 抓取 标签内容并保存为名为 fonts.css

的文件

这是我的风格:

<style>@font-face{font-family:Averti;src:url(https://services.serving-sys.com/HostingServices/custdev/site-140253/Averti/Averti-Bold.woff) format('truetype');font-weight:700;font-style:normal}@font-face{font-family:Averti-Light;src:url(https://services.serving-sys.com/HostingServices/custdev/site-140253/Averti_Webfonts/Averti-Light.woff) format('truetype');font-weight:400;font-style:normal}</style>

函数没有重大错误,但 console.log 告诉我 { [错误:ENOENT:没有这样的文件或目录,打开'./dist/css/fonts.css'] 错误号:-2, 代码:'ENOENT', 系统调用:'open', 路径:'./dist/css/fonts.css'}

我不确定这是否正确,因为文件尚未创建。

请参阅下面的函数,让我知道我缺少什么。

提前致谢。

async function createStyle(){

var jsonObject = [] 

setTimeout(function(){  

fs.readFile('./dist/index.html', 'utf8',  function(err, html){
    if (!err){
        const $ = cheerio.load(html)
        var cssScript = $('head > style').map(( i, x ) => x.children[0])
                                         .filter(( i, x ) => x && x.data.match(/@font-face/)).get(0);
        jsonObject.push(cssScript)
        exportStyle(jsonObject)
    }
})}, 2000); 

async function exportStyle(_json) {

const stylePromise = new Promise(function(resolve, reject) {

    fs.writeFile('./dist/css/fonts.css', _json, err => {
        if (err) {
            reject();
        } else {                                
            resolve();
            console.log('Created: fonts.css');                                                     
        }            
        console.log(err);                             
        });
});
       (async function() {
           try {
                await stylePromise;
         } catch(err) {
                console.log(err);
         }
})();}}

我在 ./dist 文件夹中创建了 css 和 js 文件夹,代码运行得非常棒。除此之外,错误消息出现在控制台中。