Skip to content

fix: compare tsconfig.federation.json using isDeepStrictEqual #820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion libs/native-federation/src/utils/angular-esbuild-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[],
Expand Down Expand Up @@ -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;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I don't know if logging the error, throwing or what else. For now I just ignore it and return false, that in this very use-specific case will just write the content again. Let me know if you want to handle this in other ways

}
}

function doesFileExist(path: string, content: string): boolean {
if (!fs.existsSync(path)) {
return false;
Expand Down