UNPKG

@neodx/vfs

Version:

Simple virtual file system - working dir context, lazy changes, different modes, integrations and moreover

1 lines 3 kB
{"version":3,"file":"prettier.cjs","sources":["../../../src/plugins/prettier.ts"],"sourcesContent":["import { tryFormatPrettier } from '@neodx/pkg-misc';\nimport { concurrently, isTypeOfString } from '@neodx/std';\nimport { displayPath, getVfsActions } from '../core/operations';\nimport type { VfsContentLike } from '../core/types';\nimport { createVfsPlugin } from '../create-vfs-plugin';\n\nexport interface PrettierPluginParams {\n /** Pass `false` to disable auto formatting on apply */\n auto?: boolean;\n}\n\nexport interface PrettierPluginApi {\n format(path: string): Promise<void>;\n formatAll(): Promise<void>;\n}\n\nexport function prettier({ auto = true }: PrettierPluginParams = {}) {\n return createVfsPlugin<PrettierPluginApi>('prettier', (vfs, { context, beforeApply }) => {\n const log = context.log.child('prettier');\n\n async function format(path: string, content: VfsContentLike) {\n log.debug('Formatting %s', displayPath(context, path));\n const formattedContent = await tryFormatPrettier(\n path,\n isTypeOfString(content) ? content : content.toString('utf-8')\n );\n\n if (formattedContent !== null) {\n await vfs.write(path, formattedContent);\n log.debug('Formatted %s', displayPath(context, path));\n } else {\n log.debug('Skipped %s', displayPath(context, path));\n }\n }\n\n vfs.format = async (path: string) => await format(path, await vfs.read(path, 'utf-8'));\n vfs.formatAll = async () => {\n log.debug('Formatting all changed files...');\n const changes = await getVfsActions(context, ['create', 'update']);\n\n await concurrently(changes, async ({ path, content }) => format(path, content), 5);\n };\n\n if (auto) {\n beforeApply(() => vfs.formatAll!());\n }\n\n return vfs;\n });\n}\n"],"names":["auto","createVfsPlugin","vfs","context","beforeApply","log","child","format","path","content","debug","displayPath","formattedContent","tryFormatPrettier","isTypeOfString","toString","write","read","formatAll","changes","getVfsActions","concurrently"],"mappings":"qLAgBO,SAAkB,CAAEA,KAAAA,EAAO,CAAA,CAAI,CAAwB,CAAG,CAAA,CAAE,EACjE,OAAOC,EAAAA,eAAAA,CAAmC,WAAY,CAACC,EAAK,CAAEC,QAAAA,CAAO,CAAEC,YAAAA,CAAW,CAAE,IAClF,IAAMC,EAAMF,EAAQE,GAAG,CAACC,KAAK,CAAC,YAE9B,eAAeC,EAAOC,CAAY,CAAEC,CAAuB,EACzDJ,EAAIK,KAAK,CAAC,gBAAiBC,EAAAA,WAAAA,CAAYR,EAASK,IAChD,IAAMI,EAAmB,MAAMC,mBAC7BL,CAAAA,EACAM,EAAAA,eAAeL,GAAWA,EAAUA,EAAQM,QAAQ,CAAC,SAGnDH,AAAqB,QAArBA,GACF,MAAMV,EAAIc,KAAK,CAACR,EAAMI,GACtBP,EAAIK,KAAK,CAAC,eAAgBC,EAAAA,WAAAA,CAAYR,EAASK,KAE/CH,EAAIK,KAAK,CAAC,aAAcC,EAAAA,WAAAA,CAAYR,EAASK,GAEjD,CAcA,OAZAN,EAAIK,MAAM,CAAG,MAAOC,GAAiB,MAAMD,EAAOC,EAAM,MAAMN,EAAIe,IAAI,CAACT,EAAM,UAC7EN,EAAIgB,SAAS,CAAG,UACdb,EAAIK,KAAK,CAAC,mCACV,IAAMS,EAAU,MAAMC,EAAAA,aAAAA,CAAcjB,EAAS,CAAC,SAAU,SAAS,CAEjE,OAAMkB,cAAAA,CAAaF,EAAS,MAAO,CAAEX,KAAAA,CAAI,CAAEC,QAAAA,CAAO,CAAE,GAAKF,EAAOC,EAAMC,GAAU,EAClF,EAEIT,GACFI,EAAY,IAAMF,EAAIgB,SAAS,IAG1BhB,CACT,EACF"}