eslint-remote-tester
Version:
Tool for running ESLint on multiple repositories
72 lines (71 loc) • 2.31 kB
JavaScript
import fs from 'node:fs';
import simpleGit from 'simple-git';
import { removeDirectorySync } from './file-utils.js';
import { CACHE_LOCATION, URL } from './file-constants.js';
import config from '../config/index.js';
// Clone only latest history of the main branch
const CLONE_OPTS = { '--depth': 1 };
// Create cache if missing
if (!fs.existsSync(CACHE_LOCATION)) {
fs.mkdirSync(CACHE_LOCATION);
}
/**
* Clone or pull latest of given repository
*/
export async function cloneRepository({ repository, onClone, onCloneFailure, onPull, onPullFailure, }) {
const repoLocation = `${CACHE_LOCATION}/${repository}`;
if (!fs.existsSync(repoLocation)) {
onClone();
try {
const git = simpleGit();
await git.clone(`${URL}/${repository}.git`, repoLocation, CLONE_OPTS);
}
catch {
onCloneFailure();
}
}
else {
// Cached repositories should not be updated when in comparison mode
if (config.compare)
return;
onPull();
try {
const git = simpleGit({ baseDir: repoLocation });
await git.pull();
}
catch {
onPullFailure();
}
}
}
/**
* Remove repository from the cache
*/
export async function removeCachedRepository(repository) {
const repoLocation = `${CACHE_LOCATION}/${repository}`;
if (fs.existsSync(repoLocation)) {
removeDirectorySync(repoLocation);
}
}
/**
* Get status of cache. Includes count of cached repositories and location of the cache.
*/
export function getCacheStatus() {
let countOfRepositories = 0;
if (fs.existsSync(CACHE_LOCATION)) {
// Root level directories are users/organizations, e.g. `@testing-library`
const repositoryOwners = fs.readdirSync(CACHE_LOCATION);
repositoryOwners.forEach(repositoryOwner => {
const stats = fs.statSync(`${CACHE_LOCATION}/${repositoryOwner}`);
// Check type just to be sure
if (!stats.isDirectory())
return;
const repositories = fs.readdirSync(`${CACHE_LOCATION}/${repositoryOwner}`);
countOfRepositories += repositories.length;
});
}
return {
countOfRepositories,
location: CACHE_LOCATION,
};
}