Skip to content

Commit

Permalink
fix mergeDestinationYaml with arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Jun 24, 2024
1 parent 6bf50bf commit b773bf8
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions generators/base-core/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { requireNamespace } from '@yeoman/namespace';
import { GeneratorMeta } from '@yeoman/types';
import chalk from 'chalk';
import { parse as parseYaml, stringify as stringifyYaml } from 'yaml';
import { kebabCase, snakeCase, merge, get, set, defaults } from 'lodash-es';
import { kebabCase, snakeCase, merge, get, set, defaults, mergeWith } from 'lodash-es';
import { simpleGit } from 'simple-git';
import type { CopyOptions } from 'mem-fs-editor';
import type { Data as TemplateData, Options as TemplateOptions } from 'ejs';
Expand Down Expand Up @@ -96,6 +96,8 @@ const relativeDir = (from: string, to: string) => {
return rel ? `${rel}/` : '';
};

const deepMerge = (source1: any, source2: any) => mergeWith({}, source1, source2, (a, b) => (Array.isArray(a) ? a.concat(b) : undefined));

/**
* This is the base class for a generator for every generator.
*/
Expand Down Expand Up @@ -1123,7 +1125,10 @@ templates: ${JSON.stringify(existingTemplates, null, 2)}`;
}
return true;
});
return headerComments.join('\n').concat('\n', stringifyYaml(merge(parseYaml(content), value)));

const mergedContent = stringifyYaml(deepMerge(parseYaml(content), value));
const header = headerComments.length > 0 ? headerComments.join('\n').concat('\n') : '';
return `${header}${mergedContent}`;
});
}

Expand Down

0 comments on commit b773bf8

Please sign in to comment.