import { parse, stringify } from 'yaml'; import { getProjectPath } from './cluserHelper'; import fs from 'fs'; import path from 'path'; import chalk from 'chalk'; export function saveConnection( connection: any, fileName: string, overwrite: boolean = false, ) { var result = connection; if (typeof connection === 'string') { result = parse(connection); } if (!connection.connectors) { result = { connectors: [connection] }; } var projectPath = getProjectPath(); fileName = fileName.indexOf('.yaml') != -1 ? fileName : `${fileName}.yaml`; var filePath = path.join(projectPath, fileName); if (fs.existsSync(filePath) && !overwrite) { console.error( chalk.red( `Connection file ${fileName} already exists. Use --overwrite flag to overwrite the file.`, ), ); return; } fs.writeFileSync( filePath, stringify(result, { keepSourceTokens: true, lineWidth: 0 }), ); }