Skip to content

Commit

Permalink
refactor: unify clone repo
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackDark committed Nov 2, 2024
1 parent 2aa101c commit 18c1b69
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 54 deletions.
35 changes: 9 additions & 26 deletions src/recyclarr-importer.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,24 @@
import { default as fs } from "node:fs";
import path from "node:path";
import { CheckRepoActions, simpleGit } from "simple-git";
import yaml from "yaml";
import { getConfig } from "./config";
import { logger } from "./logger";
import { ArrType, MappedTemplates } from "./types/common.types";
import { RecyclarrTemplates } from "./types/recyclarr.types";
import { recyclarrRepoPaths } from "./util";
import { cloneGitRepo, recyclarrRepoPaths } from "./util";

const DEFAULT_RECYCLARR_GIT_URL = "https://github.com/recyclarr/config-templates";

export const cloneRecyclarrTemplateRepo = async () => {
const rootPath = recyclarrRepoPaths.root;
logger.info(`Checking Recyclarr repo (${rootPath})`);

if (!fs.existsSync(rootPath)) {
fs.mkdirSync(rootPath, { recursive: true });
}

const gitClient = simpleGit({ baseDir: rootPath });
const r = await gitClient.checkIsRepo(CheckRepoActions.IS_REPO_ROOT);
logger.info(`Checking Recyclarr repo ...`);

const rootPath = recyclarrRepoPaths.root;
const applicationConfig = getConfig();
const gitUrl = getConfig().recyclarrConfigUrl ?? DEFAULT_RECYCLARR_GIT_URL;
const revision = applicationConfig.recyclarrRevision ?? "master";

if (!r) {
await simpleGit().clone(applicationConfig.recyclarrConfigUrl ?? DEFAULT_RECYCLARR_GIT_URL, rootPath);
}

await gitClient.checkout(applicationConfig.recyclarrRevision ?? "master", ["-f"]);
const result = await gitClient.status();

if (!result.detached) {
const res = await gitClient.pull();
if (res.files.length > 0) {
logger.info(`Updated Recyclarr repo.`);
}
}

logger.info(`Recyclarr repo on '${result.current}'`);
const cloneResult = await cloneGitRepo(rootPath, gitUrl, revision);
logger.info(`Recyclarr repo: ref[${cloneResult.ref}], hash[${cloneResult.hash}], path[${cloneResult.localPath}]`);
};

export const getLocalTemplatePath = () => {
Expand Down Expand Up @@ -83,6 +64,8 @@ export const loadRecyclarrTemplates = (arrType: ArrType): Map<string, MappedTemp
fillMap(localPath);
}

logger.debug(`Found ${map.size} Recyclarr templates.`);

return new Map(
Array.from(map, ([k, v]) => {
const customFormats = v.custom_formats?.map((cf) => {
Expand Down
37 changes: 10 additions & 27 deletions src/trash-guide.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,25 @@
import fs from "node:fs";
import path from "node:path";
import { CheckRepoActions, simpleGit } from "simple-git";
import { ConfigCustomFormat, ConfigQualityProfile, ConfigQualityProfileItem } from "src/types/config.types";
import { MergedCustomFormatResource } from "./__generated__/mergedTypes";
import { getConfig } from "./config";
import { logger } from "./logger";
import { ArrType, CFProcessing, ConfigarrCF, QualityDefintionsRadarr, QualityDefintionsSonarr } from "./types/common.types";
import { ConfigCustomFormat, ConfigQualityProfile, ConfigQualityProfileItem } from "./types/config.types";
import { TrashCF, TrashQP, TrashQualityDefintion } from "./types/trashguide.types";
import { loadJsonFile, mapImportCfToRequestCf, notEmpty, toCarrCF, trashRepoPaths } from "./util";
import { cloneGitRepo, loadJsonFile, mapImportCfToRequestCf, notEmpty, toCarrCF, trashRepoPaths } from "./util";

const DEFAULT_TRASH_GIT_URL = "https://github.com/TRaSH-Guides/Guides";

export const cloneTrashRepo = async () => {
const rootPath = trashRepoPaths.root;
logger.info(`Checking TrashGuide repo (${rootPath})`);

if (!fs.existsSync(rootPath)) {
fs.mkdirSync(rootPath, { recursive: true });
}

const gitClient = simpleGit({ baseDir: rootPath });
const r = await gitClient.checkIsRepo(CheckRepoActions.IS_REPO_ROOT);
logger.info(`Checking TrashGuide repo ...`);

const rootPath = trashRepoPaths.root;
const applicationConfig = getConfig();
const gitUrl = getConfig().trashGuideUrl ?? DEFAULT_TRASH_GIT_URL;
const revision = applicationConfig.trashRevision ?? "master";

if (!r) {
await simpleGit().clone(applicationConfig.trashGuideUrl ?? DEFAULT_TRASH_GIT_URL, rootPath);
}

await gitClient.checkout(applicationConfig.trashRevision ?? "master", ["-f"]);
const result = await gitClient.status();

if (!result.detached) {
const res = await gitClient.pull();
if (res.files.length > 0) {
logger.info(`Updated TrashGuide repo.`);
}
}

logger.info(`TrashGuide repo on '${result.current}'`);
const cloneResult = await cloneGitRepo(rootPath, gitUrl, revision);
logger.info(`TrashGuide repo: ref[${cloneResult.ref}], hash[${cloneResult.hash}], path[${cloneResult.localPath}]`);
};

export const loadSonarrTrashCFs = async (arrType: ArrType): Promise<CFProcessing> => {
Expand Down Expand Up @@ -134,9 +115,11 @@ export const loadQPFromTrash = async (arrType: ArrType) => {
// fillMap(localPath);
// }

logger.debug(`Found ${map.size} TrashGuide QualityProfiles.`);
return map;
};

// TODO merge two methods?
export const transformTrashQPToTemplate = (data: TrashQP): ConfigQualityProfile => {
return {
min_format_score: data.minFormatScore,
Expand Down
46 changes: 45 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { readFileSync } from "node:fs";
import { existsSync, mkdirSync, readFileSync } from "node:fs";
import path from "node:path";
import simpleGit, { CheckRepoActions } from "simple-git";
import { MergedCustomFormatResource } from "./__generated__/mergedTypes";
import { logger } from "./logger";
import { ConfigarrCF, ImportCF, UserFriendlyField } from "./types/common.types";
Expand Down Expand Up @@ -241,3 +242,46 @@ export function zipNLength<T extends unknown[][]>(...arrays: T): Array<{ [K in k

return result as Array<{ [K in keyof T]: T[K] extends (infer U)[] ? U : never }>;
}

export const cloneGitRepo = async (localPath: string, gitUrl: string, revision: string) => {
const rootPath = localPath;

if (!existsSync(rootPath)) {
mkdirSync(rootPath, { recursive: true });
}

const gitClient = simpleGit({ baseDir: rootPath });
const r = await gitClient.checkIsRepo(CheckRepoActions.IS_REPO_ROOT);

if (!r) {
await simpleGit().clone(gitUrl, rootPath);
}

await gitClient.checkout(revision, ["-f"]);
const result = await gitClient.status();

let updated = false;

if (!result.detached) {
const res = await gitClient.pull();
if (res.files.length > 0) {
updated = true;
}
}

let hash: string = "unknown";

try {
hash = await gitClient.revparse(["--verify", "HEAD"]);
} catch (err: unknown) {
// Ignore
logger.debug(`Unable to extract hash from commit`);
}

return {
ref: result.current,
hash: hash,
localPath: localPath,
updated,
};
};

0 comments on commit 18c1b69

Please sign in to comment.