UNPKG

babel-plugin-tester

Version:
41 lines (40 loc) 1.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.prettierFormatter = exports.default = void 0; var _fs = require("@-xun/fs"); var _prettier = require("prettier"); var _constant = require("../constant.js"); const prettierFormatter = async (code, { cwd = process.cwd(), filename, filepath = filename || (0, _fs.toPath)(cwd, 'dummy.js'), config, prettierOptions = config || getCachedConfig(filepath) } = {}) => { const finalPrettierOptions = { filepath, ...(await prettierOptions) }; debug('cwd: %O', cwd); debug('filepath: %O', filepath); debug('prettier options: %O', finalPrettierOptions); debug('original code: %O', code); const formattedCode = await (0, _prettier.format)(code, finalPrettierOptions); debug('formatted code: %O', code); return formattedCode; }; exports.prettierFormatter = prettierFormatter; var _default = exports.default = prettierFormatter; const debug = _constant.globalDebugger.extend('formatter'); const configDirectoryCache = Object.create(null); const getCachedConfig = async filepath => { if (!(filepath in configDirectoryCache)) { configDirectoryCache[filepath] = await (0, _prettier.resolveConfig)(filepath); debug(`caching prettier configuration resolved from ${filepath}: %O`, configDirectoryCache[filepath]); } else { debug(`using cached prettier configuration resolved from ${filepath}`); } return configDirectoryCache[filepath]; };