首页 > 解决方案 > 为什么我的 async 在第三个调用中不能很好地工作,但在前两个调用中却很完美

问题描述

所以我有一个令人难以置信的想法,我的应用程序抓取了一个 git repo(simple-git)然后在里面的文件(shelljs)上创建一个 npm i,然后使用归档器压缩。当然它需要是异步的,但是第一部分和第二部分可以工作,但是在归档时它失败了(下一步是让 axios 等待 zip 完成),在此之前,当我用抓取运行邮政编码时repo 代码它会在正确的根目录中创建 zip 文件,但现在在文件夹目录中创建 (repo/folder) ,虽然现在 zip 是空的,但不是压缩其他内容。如果可以,请提供帮助代码:

// // //Make call
function makeCall() {
  return new Promise((resolve) => {
    resolve(
      git()
        .silent(true)
        .clone(remote, ["-b", branch])
        .then(() => console.log("finished"))
        .catch((err) => console.error("failed: ", err))
    );
  });
}

//Make NPM Modules
async function makeModules() {
  await makeCall();

  const pathOfFolder = path.join(__dirname, "folder/sub");
  shell.cd(pathOfFolder)

  return new Promise((resolve) => {
    resolve(
      shell.exec("npm i"));
  });
}

async function makeZip() {
  await makeModules();

  const output = fs.createWriteStream(`${branch}.zip`);
  const archive = archiver("zip");

  
  output.on("close", function () {
    console.log(archive.pointer() + " total bytes");
    console.log(
      "archiver has been finalized and the output file descriptor has closed."
    );
  });

  archive.pipe(output);

  // append files from a sub-directory, putting its contents at the root of archive
  archive.directory("folder", false);

  // append files from a sub-directory and naming it `new-subdir` within the archive
  archive.directory("subdir/", "new-subdir");

  archive.finalize();
}

makeZip();

标签: javascriptnode.jsasync-awaitpath

解决方案


解决它,移动到其他文件并正确设置路径


推荐阅读