chore(populateCache): skip if blob already cached

This commit is contained in:
Anton Strogonoff 2021-11-25 14:05:51 +01:00
parent 023e7af0ba
commit 31fb997b69
2 changed files with 22 additions and 6 deletions

View file

@ -4,7 +4,7 @@ import fs from 'fs/promises';
import git from 'isomorphic-git';
import http from 'isomorphic-git/http/node';
import { isWriteable, pointsToLFS } from './util';
import { isVacantAndWriteable, isWriteable, pointsToLFS } from './util';
import downloadBlobFromPointer from './download';
import { readPointer } from "./pointers";
@ -55,8 +55,9 @@ export default async function populateCache(workDir: string, ref: string = 'HEAD
if (pointsToLFS(buff)) {
const pointer = readPointer({ dir: workDir, content: buff });
// Dont even start the download if LFS cache path is not accessible.
if (await isWriteable(pointer.objectPath) === false)
// Dont even start the download if LFS cache path is not accessible,
// or if it already exists
if (await isVacantAndWriteable(pointer.objectPath) === false)
return;
const content = await downloadBlobFromPointer(

View file

@ -20,12 +20,27 @@ export async function isWriteable(filepath: string): Promise<boolean> {
await fs.access(filepath, fsConstants.W_OK);
return true;
} catch (e) {
if ((e as { code: string }).code !== 'ENOENT') {
return false;
} else {
if ((e as { code: string }).code === 'ENOENT') {
return true;
}
return false;
}
}
/**
* Returns true if given path is available for writing
* and not occupied.
*/
export async function isVacantAndWriteable(filepath: string): Promise<boolean> {
try {
await fs.access(filepath, fsConstants.W_OK);
} catch (e) {
if ((e as { code: string }).code === 'ENOENT') {
return true;
}
}
return false;
}