prompt: Can I have a TS Node function that recursively copies a file or directory from one path to another? Place a comment at the end of each line explaining what it's doing. It should be asynchronous.
chosen.role: system
chosen.content: A chat.
chosen.role: user
chosen.content: Can I have a TS Node function that recursively copies a file or directory from one path to another? Place a comment at the end of each line explaining what it's doing. It should be asynchronous.
chosen.role: assistant
chosen.content: Sure thing. Here is an asynchronous TypeScript function to recursively copy a file or directory!
  
  import { promises } from "fs";
  import { join } from "path";
  async function copy(src: string, destination: string): Promise<void> {
      const stat = await promises.stat(src); // Get the status of the source to copy from.
  
      if (stat.isDirectory()) { // If the source is a directory:
          const contained = await promises.readdir(src); // Read the contents of the directory.
          await Promise.all( // Wait for all the produced promises to resolve.
              contained.map((file) => copy(join(src, file), join(destination, file))) // Copy each file contained in the directory to its destination path.
          );
      } else if (stat.isFile()) { // If the source is a file:
          await promises.mkdir(dirname(destination), { recursive: true }); // Recursively create the file's path if it does not exist.
          await promises.copyFile(src, destination); // Copy the file to the destination.
      }
  }
rejected.role: system
rejected.content: A chat.
rejected.role: user
rejected.content: Can I have a TS Node function that recursively copies a file or directory from one path to another? Place a comment at the end of each line explaining what it's doing. It should be asynchronous.
rejected.role: assistant