From dd6f013846df4a5b03d80f41ceaa91cc51976392 Mon Sep 17 00:00:00 2001 From: Alessio Marchi <65831826+kettei-sproutty@users.noreply.github.com> Date: Thu, 3 Apr 2025 20:16:12 +0200 Subject: [PATCH] fix: compare federation tsconfig using isDeepStrictEqual --- .../src/utils/angular-esbuild-adapter.ts | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/libs/native-federation/src/utils/angular-esbuild-adapter.ts b/libs/native-federation/src/utils/angular-esbuild-adapter.ts index dd630089..e296e768 100644 --- a/libs/native-federation/src/utils/angular-esbuild-adapter.ts +++ b/libs/native-federation/src/utils/angular-esbuild-adapter.ts @@ -38,6 +38,7 @@ import { import { RebuildEvents, RebuildHubs } from './rebuild-events'; import JSON5 from 'json5'; +import { isDeepStrictEqual } from 'node:util'; export type MemResultHandler = ( outfiles: esbuild.OutputFile[], @@ -352,13 +353,37 @@ function createTsConfigForFederation( const tsconfigFedPath = path.join(tsconfigDir, 'tsconfig.federation.json'); - if (!doesFileExist(tsconfigFedPath, content)) { + if (!doesFileExistAndJsonEqual(tsconfigFedPath, content)) { fs.writeFileSync(tsconfigFedPath, JSON.stringify(tsconfig, null, 2)); } tsConfigPath = tsconfigFedPath; return tsConfigPath; } +/** + * Checks if a file exists and if its content is equal to the provided content. + * If the file does not exist, it returns false. + * If the file or its content is invalid JSON, it returns false. + * @param {string} path - The path to the file + * @param {string} content - The content to compare with + * @returns {boolean} - Returns true if the file exists and its content is equal to the provided content + */ +function doesFileExistAndJsonEqual(path: string, content: string) { + if (!fs.existsSync(path)) { + return false; + } + + try { + const currentContent = fs.readFileSync(path, 'utf-8'); + const currentJson = JSON5.parse(currentContent); + const newJson = JSON5.parse(content); + + return isDeepStrictEqual(currentJson, newJson); + } catch (_error) { + return false; + } +} + function doesFileExist(path: string, content: string): boolean { if (!fs.existsSync(path)) { return false;