From aae31e60df75360771e22803d35b9fd4f4c0d1ee Mon Sep 17 00:00:00 2001 From: Lasim Date: Tue, 1 Oct 2024 10:15:03 +0200 Subject: [PATCH] feat(dockerComposeContent) switched to content --- README.md | 11 +++++++---- src/index.ts | 9 ++++----- test/test.ts | 8 ++++++-- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ae86133..590d2e0 100644 --- a/README.md +++ b/README.md @@ -16,9 +16,12 @@ npm install ```typescript import { translate } from 'docker-to-iac'; -import { writeFileSync } from 'fs'; +import { readFileSync, writeFileSync } from 'fs'; -const translatedConfig = translate('path/to/docker-compose.yml', 'aws-cloudformation'); +// Read Docker Compose file content as plain text +const dockerComposeContent = readFileSync('path/to/docker-compose.yml', 'utf8'); + +const translatedConfig = translate(dockerComposeContent, 'CFN'); console.log(translatedConfig); // Write the translated config to a file @@ -32,13 +35,13 @@ You can retrieve metadata about the available parsers as well: ```typescript import { getParserInfo } from 'docker-to-iac'; -const awsInfo = getParserInfo('aws-cloudformation'); +const awsInfo = getParserInfo('CFN'); console.log(awsInfo); ``` ### Supported Platforms -- `aws-cloudformation` +- `CFN`: AWS CloudFormation ## Development diff --git a/src/index.ts b/src/index.ts index 01a1882..3f5599f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,3 @@ -import { readFileSync } from 'fs'; import * as yaml from 'js-yaml'; import cloudFormationParserInstance from './parsers/aws-cloudformation'; import { BaseParser, ParserInfo } from './parsers/base-parser'; @@ -10,10 +9,10 @@ const parsers: BaseParser[] = [ // e.g., terraformParserInstance ]; -function translate(dockerComposeFilePath: string, targetPlatform: string): any { +// Updated translate function to take plain text as input +function translate(dockerComposeContent: string, targetPlatform: string): any { try { - const fileContents = readFileSync(dockerComposeFilePath, 'utf8'); - const dockerCompose = yaml.load(fileContents) as any; + const dockerCompose = yaml.load(dockerComposeContent) as any; const parser = parsers.find(parser => parser.getInfo().abbreviation.toLowerCase() === targetPlatform.toLowerCase()); if (!parser) { @@ -23,7 +22,7 @@ function translate(dockerComposeFilePath: string, targetPlatform: string): any { const translatedConfig = parser.parse(dockerCompose); return translatedConfig; } catch (e) { - console.error(`Error translating docker-compose file: ${e}`); + console.error(`Error translating docker-compose content: ${e}`); return null; } } diff --git a/test/test.ts b/test/test.ts index c672e0a..1b83dc5 100644 --- a/test/test.ts +++ b/test/test.ts @@ -1,17 +1,21 @@ import { translate, getParserInfo, listAllParsers } from '../src/index'; -import { writeFileSync } from 'fs'; +import { writeFileSync, readFileSync } from 'fs'; const awsInfo = getParserInfo('CFN'); console.log('AWS CloudFormation Info:'); console.log(awsInfo); +// Read the Docker Compose file as plain text +const dockerComposeContent = readFileSync('test/sample-docker-compose.yml', 'utf8'); + // Testing AWS CloudFormation -const awsConfig = translate('test/sample-docker-compose.yml', 'CFN'); +const awsConfig = translate(dockerComposeContent, 'CFN'); console.log('AWS CloudFormation:'); console.log(awsConfig); writeFileSync('test/output/output-aws.json', JSON.stringify(awsConfig, null, 2)); // List all available parsers const parsers = listAllParsers(); + console.log('Available Parsers:'); console.log(parsers);