使用 fs.existsSync ...我无法将输出设为 true ...(已编辑)

Posted

技术标签:

【中文标题】使用 fs.existsSync ...我无法将输出设为 true ...(已编辑)【英文标题】:using fs.existsSync...I am unable to get true as the output...(edited) 【发布时间】:2021-03-11 22:06:46 【问题描述】:

在下面的代码中, 我正在解压缩一个文件夹,并在解压缩其中的验证文件之后。但是,我可以手动查看我的 mac 中的文件,尽管我无法断言它的代码。 fs.existsSync 断言它是假的。

const zipFilePath = path.join(zipDirPath, `webfontkit$params.tc.zip`);
const unzipFolderPath = path.join(outDir, `webfontkit$params.tc`);

// Execute request for kit API and save response zip files
await utils.executeRequestAndSaveReponseInFileSystem(params,
    zipFilePath);

// Unzip the zip files downloaded
let unzipper = new DecompressZip( zipFilePath );
unzipper.extract(
    path: unzipFolderPath
); 

// await utils.unzipFolder(zipFilePath, unzipFolderPath);
//
// await expect(fs.existsSync(unzipFolderPath)).toEqual(true);

const kitname = await utils
    .replaceDelimitter(params.body.kitName, ' ', '_');

const kitPath = path.join(unzipFolderPath, kitname);
console.log(kitPath);

// Verify kitpath and kitname exists
await expect(fs.existsSync(kitPath)).toEqual(true);
await expect(fs.existsSync(path.join(kitPath, `$kitname.css`))).toEqual(true);

这是目前基于反馈的另一个版本:

static async unzipFolder(zipFilePath, unzipFolderPath) 
    return new Promise((resolve, reject) => 
         fs.createReadStream(zipFilePath)
            .pipe(unzip.Extract( path: unzipFolderPath ))
            .on('close', () =>      
                resolve();
                
            )
            .on('error', (error) => 
                reject(error);
                
            );
    );



await utils.unzipFolder(zipFilePath, unzipFolderPath);



await expect(fs.existsSync(unzipFolderPath)).toEqual(true);
        
const kitname = await utils
    .replaceDelimitter(params.body.kitName, ' ', '_');

const kitPath = path.join(unzipFolderPath, kitname);
console.log(kitPath,'KITPATH');
console.log(kitname,'KITNAME');

await utils.getFile(unzipFolderPath,50000);

// Verify kitpath and kitname exists
await expect(fs.existsSync(kitPath)).toEqual(true);
await expect(fs.existsSync(path.join(kitPath, `$kitname.css`))).toEqual(true);
    

【问题讨论】:

kitpath 的输出是什么 什么是DecompressZip?它可能是异步的。 unzipper.extract() 不是异步的吗?如果是这样,您必须等待它完成才能检查结果。 @PDHide 输出是 output/webfontkit1/SampleKit @jfriend00 我现在已经用 async/await 替换了......仍然没有为我解决 【参考方案1】:

您在解压缩操作完成之前很久就调用了fs.existsSync(),因此文件还没有。

如果您查看 the doc 以了解您正在使用的库,那么 unzipper.extract()异步。当您调用它时,它只是开始操作,然后立即返回,并在稍后的某个时间结束。

您需要安装事件处理程序才能查看它何时真正完成。文档中的代码显示:

unzipper.on('extract', function (log) 
    console.log('Finished extracting');
    expect(fs.existsSync(kitPath)).toEqual(true);
);

查看它何时真正完成解压缩。在回调内部是你应该调用fs.existsSync()的地方。


我尝试制作可在我自己的系统上运行的代码的可重现版本。这就是我所拥有的,它工作得很好。我使用了一些随机的 ZIP 文件,我在我的系统上放置了一些图像,其中包含一堆图像。我选择了其中一张图像的文件名并将其编码到程序中以测试它是否存在。

const fs = require('fs');
const unzipper = require('unzipper');
const path = require('path');

async function unzipFolder(zipFilePath, unzipFolderPath) 
    return new Promise((resolve, reject) => 
        fs.createReadStream(zipFilePath)
            .pipe(unzipper.Extract( path: unzipFolderPath ))
            .on('close', resolve)
            .on('error', reject);
    );


async function run() 

    const zipFilePath = "./test.zip";        // sample input zip file
    const unzipFolderPath = "./unzip";       // directory to put unzipped contents into

    await unzipFolder(zipFilePath, unzipFolderPath);

    console.log("fs.existsSync(unzipFolderPath)", fs.existsSync(unzipFolderPath));

    const testFileName = path.join(unzipFolderPath, "20200610_122443.jpg");

    // Verify expected output file exists
    console.log("fs.existsSync(testFileName)", fs.existsSync(testFileName));


run().then(result => 
    console.log("success");
).catch(err => 
    console.log(err);
);

此代码有效。它生成以下输出:

fs.existsSync(unzipFolderPath) true
fs.existsSync(testFileName) true
success

【讨论】:

我现在已经使用了以下功能和 async/await..... 但没有喘息的机会...... 静态异步 unzipFolder(zipFilePath, unzipFolderPath) return new Promise((resolve, reject) => fs.createReadStream(zipFilePath) .pipe(unzip.Extract( path: unzipFolderPath, ). on('close',()=>resolve();)) // .on('finish', () => // resolve(); // ) .on('error', (错误) => 拒绝(错误); ); ); 等待 utils.unzipFolder(zipFilePath, unzipFolderPath); console.log(unzipFolderPath,'UNZIP') const kitname = await utils .replaceDelimitter(params.body.kitName, ' ', '_'); const kitPath = path.join(unzipFolderPath, kitname);等待 utils.getFile(kitPath,5000); // 验证 kitpath 和 kitname 是否存在 await expect(fs.existsSync(kitPath)).toEqual(true);等待期望(fs.existsSync(path.join(kitPath, $kitname.css))).toEqual(true); @nps - cmets 中的多行代码不可读。如果您想向我展示后续的修复工作,请将其添加到问题的末尾(不要删除或覆盖或替换原始问题中的任何内容),然后在此处留下您已添加到的评论您的问题结束您当前的实施并描述您仍然遇到的问题。 现在添加了一个 async/await...仍然面临同样的问题....@jfriend00

以上是关于使用 fs.existsSync ...我无法将输出设为 true ...(已编辑)的主要内容,如果未能解决你的问题,请参考以下文章

fs.existsSync 在电子中使用时不是函数

fs.exists 和 fs.existsSync 之间的区别

fs.existsSync 不是 Angular (5) 中的函数

TypeError:fs.​​existsSync 不是函数(Electron/ReactJS/Typescript)

nodejs_fs模块相关练习1

具有完整路径的节点异步 mkdir