/* lib/terminal/utilities/writeStream - A utility to pipe from a read stream to a write stream. */ import node from "./node.js";
const writeStream = function terminal_utilities_writeStream(config) {
    const readStream = node.fs.createReadStream(config.source), writeStream = node.fs.createWriteStream(config.destination, {
        mode: config.stat.mode
    });
    let errorFlag = false;
    readStream.on("error", function terminal_utilities_writeStream_readError(error) {
        config.callback(error);
        errorFlag = true;
    });
    if (errorFlag === false) {
        writeStream.on("error", function terminal_utilities_writeStream_writeError(error) {
            config.callback(error);
            errorFlag = true;
        });
        if (errorFlag === false) {
            writeStream.on("open", function terminal_utilities_writeStream_writeOpen() {
                readStream.pipe(writeStream);
            });
            writeStream.once("finish", function terminal_utilities_writeStream_writeStream() {
                node.fs.utimes(config.destination, new Date(config.stat.atimeMs), new Date(config.stat.mtimeMs), function terminal_utilities_writeStream_writeStream_callback() {
                    config.callback(null);
                });
            });
        }
    }
};
export default writeStream;
