function updatePathWithStats()

in src/FileUpdater.js [57:111]


function updatePathWithStats (sourcePath, sourceStats, targetPath, targetStats, options, log = () => {}) {
    const rootDir = (options && options.rootDir) || '';
    const copyAll = (options && options.all) || false;
    const targetFullPath = path.join(rootDir, targetPath);

    // Source or target could be a device, socket or pipe. We just skip these.
    const isSpecial = stats => stats && !stats.isFile() && !stats.isDirectory();
    if (isSpecial(targetStats) || isSpecial(sourceStats)) return false;

    if (!sourceStats) {
        if (!targetStats) return false;

        // The target exists but the source not, so we delete the target.
        log(`delete ${targetPath} (no source)`);
        fs.rmSync(targetFullPath, { recursive: true, force: true });
        return true;
    }

    if (targetStats && (targetStats.isDirectory() !== sourceStats.isDirectory())) {
        // The target exists but the directory status doesn't match the source.
        // So we delete it and let it be created again by the code below.
        log(`delete ${targetPath} (wrong type)`);
        fs.rmSync(targetFullPath, { recursive: true, force: true });
        targetStats = null;
    }

    if (sourceStats.isDirectory() && !targetStats) {
        // The target directory does not exist, so we create it.
        log(`mkdir ${targetPath}`);
        fs.mkdirSync(targetFullPath, { recursive: true });
        return true;
    }

    if (sourceStats.isFile()) {
        // The source is a file and the target either is one too or missing.

        // If the caller did not specify that all files should be copied, check
        // if the source has been modified since it was copied to the target, or
        // if the file sizes are different. (The latter catches most cases in
        // which something was done to the file after copying.) Comparison is >=
        // rather than > to allow for timestamps lacking sub-second precision in
        // some filesystems.
        const needsUpdate = !targetStats || copyAll ||
            sourceStats.size !== targetStats.size ||
            sourceStats.mtime.getTime() >= targetStats.mtime.getTime();
        if (!needsUpdate) return false;

        const type = targetStats ? 'updated' : 'new';
        log(`copy  ${sourcePath} ${targetPath} (${type} file)`);
        fs.cpSync(path.join(rootDir, sourcePath), targetFullPath, { recursive: true });
        return true;
    }

    return false;
}