UNPKG

@dbml/cli

Version:

See our website [@dbml/cli](https://dbml.dbdiagram.io/cli/) for more information

47 lines (41 loc) 1.64 kB
import figures from 'figures'; import chalk from 'chalk'; import path from 'path'; import { exporter } from '@dbml/core'; import { validateInputFilePaths, resolvePaths, getFormatOpt, generate, } from './utils'; import { validateFilePlugin } from './validatePlugins/validatePlugins'; import OutputConsolePlugin from './outputPlugins/outputConsolePlugin'; import OutputFilePlugin from './outputPlugins/outputFilePlugin'; import config from './config'; import logger from '../helpers/logger'; import { SyntaxError } from '../errors'; export default async function exportHandler (program) { try { const inputPaths = resolvePaths(program.args); validateInputFilePaths(inputPaths, validateFilePlugin); const opts = program.opts(); const format = getFormatOpt(opts); if (!opts.outFile && !opts.outDir) { generate(inputPaths, (dbml) => exporter.export(dbml, format), OutputConsolePlugin); } else if (opts.outFile) { const header = [ '-- SQL dump generated using DBML (dbml.dbdiagram.io)\n', `-- Database: ${config[format].name}\n`, `-- Generated at: ${new Date().toISOString()}\n\n`, ].join(''); generate( inputPaths, (dbml) => exporter.export(dbml, format), new OutputFilePlugin(resolvePaths(opts.outFile), header), ); console.log(` ${chalk.green(figures.main.tick)} Generated SQL dump file (${config[format].name}): ${path.basename(opts.outFile)}`); } } catch (error) { logger.error(`\n ${error.diags.map((diag) => new SyntaxError(diag.filepath, diag)).map(({ message }) => message).join('\n ')}`); } }