77 lines
2.7 KiB
JavaScript
77 lines
2.7 KiB
JavaScript
|
import { existsSync, } from 'fs'
|
||
|
import { lstat, readFile } from 'fs/promises'
|
||
|
import git from 'git-client'
|
||
|
import { resolve } from 'path'
|
||
|
import int from 'set.prototype.intersection';
|
||
|
import { FILTER_LIST } from './FILTER_LIST.mjs';
|
||
|
/**
|
||
|
*
|
||
|
* @param {[string,string,number]} param0
|
||
|
* @returns {Promise<[string,string|null]>}
|
||
|
*/
|
||
|
export async function cloneRepoAndCheck([repoName, repoGitUrl, downloadCount]) {
|
||
|
const repoPath = resolve('cache/repos', repoName)
|
||
|
|
||
|
if (FILTER_LIST.includes(repoGitUrl)) {
|
||
|
console.log("[git] ignoring ", repoName)
|
||
|
return [repoName, null]
|
||
|
};
|
||
|
// console.log('[git] fetching',repoName, repoGitUrl);
|
||
|
await cacheCloneIdempotently(repoPath, repoName, repoGitUrl)
|
||
|
|
||
|
const tsConfigFileLocation = resolve(repoPath,'tsconfig.json');
|
||
|
const tsConfigFileExists = existsSync(tsConfigFileLocation);
|
||
|
if(tsConfigFileExists) return [repoName, null];
|
||
|
|
||
|
|
||
|
const packageFile = resolve(repoPath, 'package.json')
|
||
|
if (!existsSync(packageFile)) return [repoName, null];
|
||
|
const packageJSONContentsString = (await readFile(packageFile)).toString()
|
||
|
|
||
|
// console.log(packageJSONContentsString);
|
||
|
const packageJSONContents = JSON.parse(packageJSONContentsString)
|
||
|
// console.log(repoName, packageJSONContents.license)
|
||
|
const hasDependencies = checkTestingDependencies(packageJSONContents, repoName);
|
||
|
if (hasDependencies)
|
||
|
return [repoName, ((packageJSONContents?.scripts?.test))]
|
||
|
else return [repoName, null]
|
||
|
}
|
||
|
|
||
|
function checkTestingDependencies(packageJSONContents, repoName) {
|
||
|
const testingLibraries = new Set(['jest', 'mocha', 'chai', 'istanbul', 'vitest']);
|
||
|
const dependencies = new Set();
|
||
|
if (packageJSONContents.dependencies !== undefined) {
|
||
|
for (const dep of Object.keys(packageJSONContents.dependencies)) {
|
||
|
dependencies.add(dep)
|
||
|
}
|
||
|
}
|
||
|
if (packageJSONContents.devDependencies !== undefined) {
|
||
|
for (const dep of Object.keys(packageJSONContents.devDependencies)) {
|
||
|
dependencies.add(dep)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// console.log(dependencies)
|
||
|
/**
|
||
|
* @type {Set}
|
||
|
*/
|
||
|
const x = int(testingLibraries, dependencies);
|
||
|
// console.log(`join`, x)
|
||
|
return x.size > 0;
|
||
|
|
||
|
}
|
||
|
|
||
|
async function cacheCloneIdempotently(repoPath, repoName, repoGitUrl) {
|
||
|
if (existsSync(repoPath)) {
|
||
|
const isDir = (await lstat(repoPath)).isDirectory()
|
||
|
if (!isDir) throw new Error(repoName, " is mangled. delete directory and re-clone.")
|
||
|
else {
|
||
|
// const path = await git.status({ $cwd: repoPath })
|
||
|
|
||
|
}
|
||
|
} else {
|
||
|
console.log("[git] cloning", repoGitUrl);
|
||
|
await git.clone(repoGitUrl, repoPath,{'single-branch':true,depth:1})
|
||
|
}
|
||
|
}
|