Skip to content

Commit

Permalink
edit properties file using dto-properties lib
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Jun 29, 2023
1 parent 2b398fe commit 57aecd5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 30 deletions.
21 changes: 8 additions & 13 deletions generators/server/support/spring-factories.mts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import _ from 'lodash';

const { escapeRegExp } = _;
import properties from 'dot-properties';

// eslint-disable-next-line import/prefer-default-export
export const addSpringFactory =
({ key, value }) =>
content => {
const match = content?.match(`${escapeRegExp(key)}(\\w*)=`);
if (match) {
const matchEnd = match.index + match[0].length;
content = `${content.slice(0, matchEnd)}\\
${value},${content.slice(matchEnd)}`;
} else {
content = `${content ? `${content.trimEnd()}\n\n` : ''}${key}=\\
${value}
`;
const obj = properties.parse(content ?? '');
const oldContent = obj[key] as string;
const factories = (oldContent?.split(',') ?? []).map(val => val.trim());
if (factories.includes(value)) {
return content;
}
return content;
obj[key] = [...factories, value].join(',');
return properties.stringify(obj);
};
28 changes: 11 additions & 17 deletions generators/server/support/spring-factories.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,25 @@ import { addSpringFactory } from './spring-factories.mjs';
describe('generator - server - support - spring-factories', () => {
describe('addSpringFactory', () => {
it('should add the first property', () => {
expect(addSpringFactory({ key: 'key.prop', value: 'value' })(null)).toBe(`key.prop=\\
value
`);
expect(addSpringFactory({ key: 'key.prop', value: 'value' })(null)).toBe('key.prop = value');
});
it('should add wrap long value', () => {
expect(addSpringFactory({ key: 'key.prop', value: '12345678901234567890123456789012345678901234567890' })(null)).toBe(
'key.prop = 12345678901234567890123456789012345678901234567890'
);
});
it('should add a new value to a property', () => {
expect(
addSpringFactory({ key: 'key.prop', value: 'new.value' })(`key.prop=\\
value
`)
).toBe(`key.prop=\\
new.value,\\
value
`);
value`)
).toBe('key.prop = value,new.value');
});
it('should add a new property', () => {
expect(
addSpringFactory({ key: 'key.prop2', value: 'new.value' })(`key.prop=\\
value
addSpringFactory({ key: 'key.prop2', value: 'new.value' })(`key.prop = value
`)
).toBe(`key.prop=\\
value
key.prop2=\\
new.value
`);
).toBe(`key.prop = value
key.prop2 = new.value`);
});
});
});
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
"debug": "4.3.4",
"didyoumean": "1.2.2",
"dockerfile-ast": "0.5.0",
"dot-properties": "^1.0.1",
"ejs": "3.1.9",
"escape-string-regexp": "5.0.0",
"execa": "7.1.1",
Expand Down

0 comments on commit 57aecd5

Please sign in to comment.