UNPKG

prettierx

Version:

prettierX - a less opinionated fork of the Prettier code formatter

47 lines (35 loc) 1.3 kB
"use strict"; const path = require("path"); const editorconfig = require("editorconfig"); const mem = require("mem"); const editorConfigToPrettier = require("editorconfig-to-prettier"); const findProjectRoot = require("./find-project-root"); const jsonStringifyMem = (fn) => mem(fn, { cacheKey: JSON.stringify }); const maybeParse = (filePath, parse) => filePath && parse(filePath, { root: findProjectRoot(path.dirname(path.resolve(filePath))), }); const editorconfigAsyncNoCache = async (filePath) => editorConfigToPrettier(await maybeParse(filePath, editorconfig.parse)); const editorconfigAsyncWithCache = jsonStringifyMem(editorconfigAsyncNoCache); const editorconfigSyncNoCache = (filePath) => editorConfigToPrettier(maybeParse(filePath, editorconfig.parseSync)); const editorconfigSyncWithCache = jsonStringifyMem(editorconfigSyncNoCache); function getLoadFunction(opts) { if (!opts.editorconfig) { return () => null; } if (opts.sync) { return opts.cache ? editorconfigSyncWithCache : editorconfigSyncNoCache; } return opts.cache ? editorconfigAsyncWithCache : editorconfigAsyncNoCache; } function clearCache() { mem.clear(editorconfigSyncWithCache); mem.clear(editorconfigAsyncWithCache); } module.exports = { getLoadFunction, clearCache, };