diff --git a/src/tools/index.ts b/src/tools/index.ts index 52bdf8e3..99c60f12 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -1,6 +1,7 @@ import { tool as base64FileConverter } from './base64-file-converter'; import { tool as base64StringConverter } from './base64-string-converter'; import { tool as basicAuthGenerator } from './basic-auth-generator'; +import { tool as propertiesToYaml } from './properties-to-yaml'; import { tool as pdfSignatureChecker } from './pdf-signature-checker'; import { tool as numeronymGenerator } from './numeronym-generator'; import { tool as macAddressGenerator } from './mac-address-generator'; @@ -100,6 +101,7 @@ export const toolsByCategory: ToolCategory[] = [ listConverter, tomlToJson, tomlToYaml, + propertiesToYaml, ], }, { diff --git a/src/tools/properties-to-yaml/index.ts b/src/tools/properties-to-yaml/index.ts new file mode 100644 index 00000000..32615d87 --- /dev/null +++ b/src/tools/properties-to-yaml/index.ts @@ -0,0 +1,12 @@ +import { AlignJustified } from '@vicons/tabler'; +import { defineTool } from '../tool'; + +export const tool = defineTool({ + name: 'Properties to YAML', + path: '/properties-to-yaml', + description: '', + keywords: ['properties', 'yaml', 'convert', 'spring', 'spring-boot', 'parse'], + component: () => import('./properties-to-yaml.vue'), + icon: AlignJustified, + createdAt: new Date('2023-12-24'), +}); diff --git a/src/tools/properties-to-yaml/properties-to-yaml.e2e.spec.ts b/src/tools/properties-to-yaml/properties-to-yaml.e2e.spec.ts new file mode 100644 index 00000000..750bc5db --- /dev/null +++ b/src/tools/properties-to-yaml/properties-to-yaml.e2e.spec.ts @@ -0,0 +1,65 @@ +import { expect, test } from '@playwright/test'; + +test.describe('Tool - Properties to YAML', () => { + test.beforeEach(async ({ page }) => { + await page.goto('/properties-to-yaml'); + }); + + test('Has correct title', async ({ page }) => { + await expect(page).toHaveTitle('Properties to YAML - IT Tools'); + }); + + test('', async ({ page }) => { + await page.getByTestId('input').fill(` +spring.mvc.async.request-timeout=-1 +spring.output.ansi.enabled=NEVER +spring.config.import=optional:file:.env[.properties] +spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver +spring.datasource.url=\${DATABASE_URI} +spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl +spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect +spring.jpa.open-in-view=false +management.endpoints.web.base-path=/internal +management.endpoints.web.exposure.include[0]=health +management.endpoints.web.exposure.include[1]=info +management.endpoint.info.enabled=true +management.endpoint.health= + `.trim()); + + const generatedJson = await page.getByTestId('area-content').innerText(); + + expect(generatedJson.trim()).toEqual(` +spring: + mvc: + async: + request-timeout: "-1" + output: + ansi: + enabled: NEVER + config: + import: optional:file:.env[.properties] + datasource: + driverClassName: com.mysql.cj.jdbc.Driver + url: \${DATABASE_URI} + jpa: + hibernate: + naming: + physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl + database-platform: org.hibernate.dialect.MySQLDialect + open-in-view: "false" +management: + endpoints: + web: + base-path: /internal + exposure: + include: + - health + - info + endpoint: + info: + enabled: "true" + health: "" + `.trim(), + ); + }); +}); diff --git a/src/tools/properties-to-yaml/properties-to-yaml.service.test.ts b/src/tools/properties-to-yaml/properties-to-yaml.service.test.ts new file mode 100644 index 00000000..a25e4c92 --- /dev/null +++ b/src/tools/properties-to-yaml/properties-to-yaml.service.test.ts @@ -0,0 +1,19 @@ +import { describe, expect, it } from 'vitest'; +import { isValidProperties } from './properties-to-yaml.service'; + +describe('isValidProperties', () => { + it('should return true for valid properties', () => { + const properties = 'key1=value1\nkey2=value2'; + expect(isValidProperties(properties)).toBe(true); + }); + + it('should return false for properties with duplicate keys', () => { + const properties = 'key1=value1\nkey1=value2'; + expect(isValidProperties(properties)).toBe(false); + }); + + it('should return false for properties with incorrect format', () => { + const properties = 'key1\nkey2=value2'; + expect(isValidProperties(properties)).toBe(false); + }); +}); diff --git a/src/tools/properties-to-yaml/properties-to-yaml.service.ts b/src/tools/properties-to-yaml/properties-to-yaml.service.ts new file mode 100644 index 00000000..50e2ea4e --- /dev/null +++ b/src/tools/properties-to-yaml/properties-to-yaml.service.ts @@ -0,0 +1,45 @@ +import _ from 'lodash'; + +export { isValidProperties, parseProperties }; + +function isValidProperties(properties: string): boolean { + const lines = properties.split('\n'); + const keys: Set = new Set(); + + for (const line of lines) { + if (line.trim().startsWith('#') || line.trim() === '') { + continue; + } + + if (!line.includes('=')) { + return false; + } + + const [key, _value] = line.split('='); + + if (!key) { + return false; + } + + if (keys.has(key.trim())) { + return false; + } + + keys.add(key.trim()); + } + + return true; +} + +// Thanks to https://github.com/sdoxsee/env-gen +function parseProperties(properties: string): Record { + const result = properties + .split('\n') + .filter(Boolean) // removes empty lines + .reduce((acc, line) => { + const pair = line.split('='); + _.set(acc, pair[0], pair[1]); + return acc; + }, {}); + return result; +} diff --git a/src/tools/properties-to-yaml/properties-to-yaml.vue b/src/tools/properties-to-yaml/properties-to-yaml.vue new file mode 100644 index 00000000..d375f53d --- /dev/null +++ b/src/tools/properties-to-yaml/properties-to-yaml.vue @@ -0,0 +1,25 @@ + + +