UNPKG

@ng-doc/builder

Version:

<!-- PROJECT LOGO --> <br /> <div align="center"> <a href="https://github.com/ng-doc/ng-doc"> <img src="https://ng-doc.com/assets/images/ng-doc.svg?raw=true" alt="Logo" height="150px"> </a>

46 lines 1.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatCode = formatCode; const sync_1 = require("@prettier/sync"); /** * Format code with Prettier * @param code - Code to format * @param codeType - Type of code */ function formatCode(code, codeType = 'TypeScript') { try { if (codeType) { const parser = getPrettierParserFromCodeType(codeType); const config = (0, sync_1.resolveConfig)(process.cwd(), { editorconfig: true }); return (0, sync_1.format)(code, { ...config, parser, embeddedLanguageFormatting: 'auto' }).trim(); } return code.trim(); } catch (e) { return code; } } /** * Returns the parser for the given code type. * @param {NgDocCodeType} codeType Code type * @returns {string} Parser */ function getPrettierParserFromCodeType(codeType) { switch (codeType) { case 'CSS': case 'LESS': case 'SCSS': case 'SASS': return 'css'; case 'HTML': return 'html'; case 'TypeScript': case 'JavaScript': return 'typescript'; case 'Markdown': return 'markdown'; default: return undefined; } } //# sourceMappingURL=format-code.js.map