From 2153835b94a1ee065e24375bcb4117c90e16b09d Mon Sep 17 00:00:00 2001
From: Sergei Zharinov <zharinov@users.noreply.github.com>
Date: Sat, 16 Apr 2022 11:20:09 +0300
Subject: [PATCH] refactor: Separate repository caches init and reset phases
 (#15142)

---
 lib/workers/repository/init/apis.ts  |  3 ---
 lib/workers/repository/init/cache.ts | 11 +++++++++--
 lib/workers/repository/init/index.ts |  5 +++--
 3 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/lib/workers/repository/init/apis.ts b/lib/workers/repository/init/apis.ts
index 47a0fb7150..35743546c0 100644
--- a/lib/workers/repository/init/apis.ts
+++ b/lib/workers/repository/init/apis.ts
@@ -4,7 +4,6 @@ import {
   REPOSITORY_DISABLED_BY_CONFIG,
   REPOSITORY_FORKED,
 } from '../../../constants/error-messages';
-import * as npmApi from '../../../modules/datasource/npm';
 import { RepoParams, RepoResult, platform } from '../../../modules/platform';
 
 // TODO: fix types
@@ -64,7 +63,5 @@ export async function initApis(
   config = await getPlatformConfig(config as never);
   await validateOptimizeForDisabled(config);
   await validateIncludeForks(config);
-  npmApi.resetMemCache();
-  npmApi.setNpmrc(config.npmrc);
   return config;
 }
diff --git a/lib/workers/repository/init/cache.ts b/lib/workers/repository/init/cache.ts
index e9668143f8..4c9fa85f52 100644
--- a/lib/workers/repository/init/cache.ts
+++ b/lib/workers/repository/init/cache.ts
@@ -1,13 +1,20 @@
 import fs from 'fs-extra';
 import type { RenovateConfig } from '../../../config/types';
+import * as npmApi from '../../../modules/datasource/npm';
 import * as memCache from '../../../util/cache/memory';
 import * as repositoryCache from '../../../util/cache/repository';
 import { privateCacheDir } from '../../../util/fs';
 
+export async function resetCaches(): Promise<void> {
+  memCache.reset();
+  repositoryCache.reset();
+  await fs.remove(privateCacheDir());
+  npmApi.resetMemCache();
+}
+
 export async function initializeCaches(config: RenovateConfig): Promise<void> {
   memCache.init();
   await repositoryCache.initialize(config);
-
-  await fs.remove(privateCacheDir());
   await fs.ensureDir(privateCacheDir());
+  npmApi.setNpmrc(config.npmrc);
 }
diff --git a/lib/workers/repository/init/index.ts b/lib/workers/repository/init/index.ts
index 7638e4885d..76a15e89fd 100644
--- a/lib/workers/repository/init/index.ts
+++ b/lib/workers/repository/init/index.ts
@@ -6,7 +6,7 @@ import { clone } from '../../../util/clone';
 import { setUserRepoConfig } from '../../../util/git';
 import { checkIfConfigured } from '../configured';
 import { initApis } from './apis';
-import { initializeCaches } from './cache';
+import { initializeCaches, resetCaches } from './cache';
 import { getRepoConfig } from './config';
 import { detectVulnerabilityAlerts } from './vulnerability';
 
@@ -26,8 +26,9 @@ export async function initRepo(
   config_: RenovateConfig
 ): Promise<RenovateConfig> {
   let config: RenovateConfig = initializeConfig(config_);
-  await initializeCaches(config);
+  await resetCaches();
   config = await initApis(config);
+  await initializeCaches(config);
   config = await getRepoConfig(config);
   checkIfConfigured(config);
   warnOnUnsupportedOptions(config);
-- 
GitLab