feat(Docker Compose to .env): add updated Docker compose

Fix #1828
This commit is contained in:
ShareVB committed 2026-07-09 23:31:30 +02:00
1 parent 14bfc21b0a
commit 00584bd549
4 files changed
+116 -29

No files matched your search

+1
View File
@@ -7579,6 +7579,7 @@ tools:
label-docker-compose-file: 'Docker Compose file:' label-docker-compose-file: 'Docker Compose file:'
placeholder-paste-your-docker-compose-yaml-here: Paste your docker-compose YAML here... placeholder-paste-your-docker-compose-yaml-here: Paste your docker-compose YAML here...
title-extract-env: Extract .env title-extract-env: Extract .env
title-updated-docker-compose: 'Updated Docker Compose:'
emv-tlv-decoder: emv-tlv-decoder:
title: EMV TLV decoder title: EMV TLV decoder
description: >- description: >-
@@ -15,12 +15,26 @@ services:
- POSTGRES_PASSWORD=secret - POSTGRES_PASSWORD=secret
`; `;
const result = extractEnvFromCompose(yaml); const result = extractEnvFromCompose(yaml);
expect(result).toContain('# web'); expect(result).to.deep.eq({
expect(result).toContain('PORT=3000'); dotenv: `# web
expect(result).toContain('DEBUG=true'); PORT=3000
expect(result).toContain('# db'); DEBUG=true
expect(result).toContain('POSTGRES_USER=admin');
expect(result).toContain('POSTGRES_PASSWORD=secret'); # db
POSTGRES_USER=admin
POSTGRES_PASSWORD=secret
`,
updatedCompose: `services:
web:
environment:
- PORT=\${PORT}
- DEBUG=\${DEBUG}
db:
environment:
- POSTGRES_USER=\${POSTGRES_USER}
- POSTGRES_PASSWORD=\${POSTGRES_PASSWORD}
`,
});
}); });
it('handles object format', () => { it('handles object format', () => {
@@ -32,9 +46,40 @@ services:
API_KEY: abc123 API_KEY: abc123
`; `;
const result = extractEnvFromCompose(yaml); const result = extractEnvFromCompose(yaml);
expect(result).toContain('# api'); expect(result).to.deep.eq({
expect(result).toContain('NODE_ENV=production'); dotenv: `# api
expect(result).toContain('API_KEY=abc123'); NODE_ENV=production
API_KEY=abc123
`,
updatedCompose: `services:
api:
environment:
- NODE_ENV=\${NODE_ENV}
- API_KEY=\${API_KEY}
`,
});
});
it('handles with already existing environment variables', () => {
const yaml = `
services:
api:
environment:
NODE_ENV: \${NODE_ENV}
API_KEY: abc123
`;
const result = extractEnvFromCompose(yaml);
expect(result).to.deep.eq({
dotenv: `# api
API_KEY=abc123
`,
updatedCompose: `services:
api:
environment:
- NODE_ENV=\${NODE_ENV}
- API_KEY=\${API_KEY}
`,
});
}); });
it('handles mixed formats and null values', () => { it('handles mixed formats and null values', () => {
@@ -51,13 +96,28 @@ services:
FOO2: null FOO2: null
`; `;
const result = extractEnvFromCompose(yaml); const result = extractEnvFromCompose(yaml);
expect(result).toContain('# mixed'); expect(result).to.deep.eq({
expect(result).toContain('FOO=bar'); dotenv: `# mixed
expect(result).toContain('EMPTY='); FOO=bar
expect(result).toContain('# obj'); EMPTY=
expect(result).toContain('BAR=baz'); FOO3=null
expect(result).toContain('FOO2=');
expect(result).toContain('FOO3='); # obj
BAR=baz
FOO2=
`,
updatedCompose: `services:
mixed:
environment:
- FOO=\${FOO}
- EMPTY=\${EMPTY}
- FOO3=\${FOO3}
obj:
environment:
- BAR=\${BAR}
- FOO2=\${FOO2}
`,
});
}); });
it('returns empty string if no env vars', () => { it('returns empty string if no env vars', () => {
@@ -67,11 +127,20 @@ services:
image: nginx image: nginx
`; `;
const result = extractEnvFromCompose(yaml); const result = extractEnvFromCompose(yaml);
expect(result).toBe(''); expect(result).to.deep.eq({
dotenv: '',
updatedCompose: `services:
noenv:
image: nginx
`,
});
}); });
it('handles empty input gracefully', () => { it('handles empty input gracefully', () => {
const result = extractEnvFromCompose(''); const result = extractEnvFromCompose('');
expect(result).toBe(''); expect(result).to.deep.eq({
dotenv: '',
updatedCompose: '',
});
}); });
}); });
@@ -1,6 +1,9 @@
import yaml from 'yaml'; import yaml from 'yaml';
export function extractEnvFromCompose(yamlInput: string) { export function extractEnvFromCompose(yamlInput: string) {
if (!yamlInput?.trim()) {
return { dotenv: '', updatedCompose: '' };
}
const doc = yaml.parse(yamlInput); const doc = yaml.parse(yamlInput);
const envLines: string[] = []; const envLines: string[] = [];
@@ -11,25 +14,42 @@ export function extractEnvFromCompose(yamlInput: string) {
} }
const serviceVars: string[] = []; const serviceVars: string[] = [];
const serviceReplacementVars: string[] = [];
if (Array.isArray(env)) { if (Array.isArray(env)) {
for (const entry of env) { for (const entry of env) {
const [key, value = ''] = entry.split('='); const [key, value = ''] = entry.split('=');
if (value?.trim().match(/^\$\{.*?\}$/)) {
serviceReplacementVars.push(entry);
continue;
}
serviceVars.push(`${key.trim()}=${value.trim()}`); serviceVars.push(`${key.trim()}=${value.trim()}`);
serviceReplacementVars.push(`${key.trim()}=\$\{${key.trim()}\}`);
} }
} } else if (typeof env === 'object') {
else if (typeof env === 'object') {
for (const [key, value] of Object.entries(env)) { for (const [key, value] of Object.entries(env)) {
if (
value
?.toString()
.trim()
.match(/^\$\{.*?\}$/)
) {
serviceReplacementVars.push(`${key.trim()}=${(value ?? '').toString().trim()}`);
continue;
}
serviceVars.push(`${key.trim()}=${(value ?? '').toString().trim()}`); serviceVars.push(`${key.trim()}=${(value ?? '').toString().trim()}`);
serviceReplacementVars.push(`${key.trim()}=\$\{${key.trim()}\}`);
} }
} }
if (serviceVars.length) { if (serviceVars.length) {
(service as { environment: any }).environment = serviceReplacementVars;
envLines.push(`# ${serviceName}`); envLines.push(`# ${serviceName}`);
envLines.push(...serviceVars); envLines.push(...serviceVars);
envLines.push(''); // Add spacing between services envLines.push(''); // Add spacing between services
} }
} }
return envLines.join('\n'); return { dotenv: envLines.join('\n'), updatedCompose: yaml.stringify(doc) };
} }
@@ -17,8 +17,7 @@ const yamlInput = ref(`services:
const envOutput = computed(() => { const envOutput = computed(() => {
try { try {
return extractEnvFromCompose(yamlInput.value); return extractEnvFromCompose(yamlInput.value);
} } catch (e: any) {
catch (e: any) {
return e.toString(); return e.toString();
} }
}); });
@@ -36,12 +35,10 @@ const envOutput = computed(() => {
/> />
<n-card :title="t('tools.docker-compose-to-env-file.texts.title-extract-env')"> <n-card :title="t('tools.docker-compose-to-env-file.texts.title-extract-env')">
<textarea-copyable <textarea-copyable v-model:value="envOutput.dotenv" rows="3" download-file-name=".env" multiline />
v-model:value="envOutput" </n-card>
rows="3" <n-card :title="t('tools.docker-compose-to-env-file.texts.title-updated-docker-compose')">
download-file-name=".env" <textarea-copyable v-model:value="envOutput.updatedCompose" rows="3" download-file-name="compose.yml" multiline />
multiline
/>
</n-card> </n-card>
</div> </div>
</template> </template>