From e307f18a68c858f0ff7966d6a962f1a5717475af Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Wed, 17 Nov 2021 16:21:36 +0100
Subject: [PATCH] fix(npm): retain package-lock indentation when massaging
 (#12715)

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
---
 lib/manager/npm/post-update/index.ts              | 13 +++++++++++--
 lib/manager/npm/update/locked-dependency/index.ts |  8 +++++++-
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/lib/manager/npm/post-update/index.ts b/lib/manager/npm/post-update/index.ts
index 4b0a360607..b957e78e03 100644
--- a/lib/manager/npm/post-update/index.ts
+++ b/lib/manager/npm/post-update/index.ts
@@ -1,5 +1,6 @@
 import is from '@sindresorhus/is';
 import deepmerge from 'deepmerge';
+import detectIndent from 'detect-indent';
 import { dump, load } from 'js-yaml';
 import upath from 'upath';
 import { getGlobalConfig } from '../../../config/global';
@@ -167,9 +168,11 @@ export async function writeExistingFiles(
       } else {
         logger.debug(`Writing ${npmLock}`);
         let existingNpmLock: string;
+        let detectedIndent: string;
         let npmLockParsed: any;
         try {
           existingNpmLock = await getFile(npmLock);
+          detectedIndent = detectIndent(existingNpmLock).indent || '  ';
           npmLockParsed = JSON.parse(existingNpmLock);
         } catch (err) {
           logger.warn({ err }, 'Error parsing npm lock file');
@@ -217,7 +220,11 @@ export async function writeExistingFiles(
           }
           if (lockFileChanged) {
             logger.debug('Massaging npm lock file before writing to disk');
-            existingNpmLock = JSON.stringify(npmLockParsed, null, 2);
+            existingNpmLock = JSON.stringify(
+              npmLockParsed,
+              null,
+              detectedIndent
+            );
           }
           await outputFile(npmLockPath, existingNpmLock);
         }
@@ -258,6 +265,8 @@ export async function writeUpdatedPackageFiles(
       continue;
     }
     logger.debug(`Writing ${String(packageFile.name)}`);
+    const detectedIndent =
+      detectIndent(packageFile.contents.toString()).indent || '  ';
     const massagedFile = JSON.parse(packageFile.contents.toString());
     try {
       const { token } = hostRules.find({
@@ -279,7 +288,7 @@ export async function writeUpdatedPackageFiles(
     }
     await outputFile(
       upath.join(localDir, packageFile.name),
-      JSON.stringify(massagedFile)
+      JSON.stringify(massagedFile, null, detectedIndent)
     );
   }
 }
diff --git a/lib/manager/npm/update/locked-dependency/index.ts b/lib/manager/npm/update/locked-dependency/index.ts
index f968303915..e9dd8f6d29 100644
--- a/lib/manager/npm/update/locked-dependency/index.ts
+++ b/lib/manager/npm/update/locked-dependency/index.ts
@@ -1,3 +1,4 @@
+import detectIndent from 'detect-indent';
 import type { PackageJson } from 'type-fest';
 import { logger } from '../../../../logger';
 import { api as semver } from '../../../../versioning/npm';
@@ -46,6 +47,7 @@ export async function updateLockedDependency(
     }
     let packageJson: PackageJson;
     let packageLockJson: PackageLockOrEntry;
+    const detectedIndent = detectIndent(lockFileContent).indent || '  ';
     let newPackageJsonContent: string;
     try {
       packageJson = JSON.parse(packageFileContent);
@@ -161,7 +163,11 @@ export async function updateLockedDependency(
       delete dependency.resolved;
       delete dependency.integrity;
     }
-    let newLockFileContent = JSON.stringify(packageLockJson, null, 2);
+    let newLockFileContent = JSON.stringify(
+      packageLockJson,
+      null,
+      detectedIndent
+    );
     // iterate through the parent updates first
     for (const parentUpdate of parentUpdates) {
       const parentUpdateConfig = {
-- 
GitLab