Skip to content
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

improvement(ws-config write) - allow post process specific extending file (when same file should be written to few paths) #7865

Merged
merged 2 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
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
16 changes: 15 additions & 1 deletion scopes/workspace/workspace-config-files/config-writer-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 {
/**
Expand Down Expand Up @@ -112,7 +124,9 @@ export interface ConfigWriterEntry {
* or based on other config files that were written.
* @param args
*/
postProcessExtendingConfigFiles?(args: PostProcessExtendingConfigFilesArgs): Promise<string | undefined>;
postProcessExtendingConfigFiles?(
args: PostProcessExtendingConfigFilesArgs
): Promise<string | Array<PostProcessExtendingConfigFilesOneFile> | undefined>;

/**
* Find all the files that are relevant for the config type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +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;
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] });
}
});
return undefined;
});
}
Expand Down