From a9351e0c080dd22484488aa0c4f608bc50dc51e5 Mon Sep 17 00:00:00 2001 From: Gilad Shoham Date: Sun, 3 Sep 2023 11:25:00 +0300 Subject: [PATCH 1/2] improvement(ws-config write) - allow post process specific extending file (when same file should be written to few paths) --- .../config-writer-entry.ts | 16 ++++++++++++- .../writers/extending-config-files.ts | 24 +++++++++++++++---- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/scopes/workspace/workspace-config-files/config-writer-entry.ts b/scopes/workspace/workspace-config-files/config-writer-entry.ts index e0bea074d525..972f246c50dd 100644 --- a/scopes/workspace/workspace-config-files/config-writer-entry.ts +++ b/scopes/workspace/workspace-config-files/config-writer-entry.ts @@ -42,6 +42,13 @@ export type PostProcessExtendingConfigFilesArgs = { */ paths: string[]; envMapValue: EnvMapValue; + /** + * This is a flag for backward compatibility + * We used to return string from the post process, so old versions of bit only knows to handle string results + * while in new version we support getting array of objects + * we need to know if bit the user is using support the new format or not + */ + supportSpecificPathChange?: boolean; }; export type GenerateExtendingConfigFilesArgs = { @@ -51,6 +58,11 @@ export type GenerateExtendingConfigFilesArgs = { envMapValue: EnvMapValue; }; +export type PostProcessExtendingConfigFilesOneFile = { + path: string; + content: string; +}; + export type MergeConfigFilesFunc = (configFile: ConfigFile, configFile2: ConfigFile) => string; export interface ConfigWriterEntry { /** @@ -112,7 +124,9 @@ export interface ConfigWriterEntry { * or based on other config files that were written. * @param args */ - postProcessExtendingConfigFiles?(args: PostProcessExtendingConfigFilesArgs): Promise; + postProcessExtendingConfigFiles?( + args: PostProcessExtendingConfigFilesArgs + ): Promise | undefined>; /** * Find all the files that are relevant for the config type. diff --git a/scopes/workspace/workspace-config-files/writers/extending-config-files.ts b/scopes/workspace/workspace-config-files/writers/extending-config-files.ts index d93e1ce96b9a..25ef98b49fd3 100644 --- a/scopes/workspace/workspace-config-files/writers/extending-config-files.ts +++ b/scopes/workspace/workspace-config-files/writers/extending-config-files.ts @@ -183,18 +183,34 @@ async function postProcessExtendingConfigFiles( return undefined; } - const newContent = await postProcessFunc({ + const postProcessRes = await postProcessFunc({ configsRootDir, extendingConfigFile: extendingConfigFileEntry.extendingConfigFile, envMapValue: envMapVal, workspaceDir, paths: dedupEntry.paths, + supportSpecificPathChange: true, }); - if (!newContent) { + if (!postProcessRes) { return undefined; } - extendingConfigFileEntry.extendingConfigFile.content = newContent; - return undefined; + if (typeof postProcessRes === 'string') { + extendingConfigFileEntry.extendingConfigFile.content = postProcessRes; + return undefined; + } + postProcessRes.forEach(({ path, content }) => { + // Remove it from the current entry + dedupEntry.paths = dedupEntry.paths.filter((currPath) => currPath !== path); + const newHash = sha1(content); + extendingConfigFilesMap[newHash] = JSON.parse(JSON.stringify(extendingConfigFileEntry)); + extendingConfigFilesMap[newHash].extendingConfigFile.content = content; + const foundNewHash = fileHashPerDedupedPaths.find((entry) => entry.fileHash === newHash); + if (foundNewHash) { + foundNewHash.paths.push(path); + } else { + fileHashPerDedupedPaths.push({ fileHash: newHash, paths: [path] }); + } + }); }); } From c03e6801a7da1acdb1b9b329faa333abe73f4fd5 Mon Sep 17 00:00:00 2001 From: Gilad Shoham Date: Sun, 3 Sep 2023 11:30:46 +0300 Subject: [PATCH 2/2] linting --- .../workspace-config-files/writers/extending-config-files.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/scopes/workspace/workspace-config-files/writers/extending-config-files.ts b/scopes/workspace/workspace-config-files/writers/extending-config-files.ts index 25ef98b49fd3..b30bcf3358bb 100644 --- a/scopes/workspace/workspace-config-files/writers/extending-config-files.ts +++ b/scopes/workspace/workspace-config-files/writers/extending-config-files.ts @@ -211,6 +211,7 @@ async function postProcessExtendingConfigFiles( fileHashPerDedupedPaths.push({ fileHash: newHash, paths: [path] }); } }); + return undefined; }); }