diff --git a/packages/prettier-plugin-panda/__tests__/prettier-plugin-panda.test.ts b/packages/prettier-plugin-panda/__tests__/prettier-plugin-panda.test.ts index e49eeab59..4fb4a3ec4 100644 --- a/packages/prettier-plugin-panda/__tests__/prettier-plugin-panda.test.ts +++ b/packages/prettier-plugin-panda/__tests__/prettier-plugin-panda.test.ts @@ -157,7 +157,7 @@ describe('JSX style props', () => { expect(await run(code)).toMatchInlineSnapshot(` "import { NotPanda } from "not-panda"; - + Hello ; " diff --git a/packages/prettier-plugin-panda/src/plugin.ts b/packages/prettier-plugin-panda/src/plugin.ts index 80eb4948a..2bf1a3b9e 100644 --- a/packages/prettier-plugin-panda/src/plugin.ts +++ b/packages/prettier-plugin-panda/src/plugin.ts @@ -3,10 +3,16 @@ import typescriptParser from 'prettier/plugins/typescript' import { BuilderResolver } from './builder-resolver' import type { PluginOptions } from './options' import { PrettyPanda } from './pretty-panda' +import { Builder } from '@pandacss/node' export { options } from './options' export type { PluginOptions } +// Programatic usage +const builder = new Builder() +let configPath: string | undefined + +// CLI/VSCode usage const resolver = new BuilderResolver() export const parsers: Plugin['parsers'] = { @@ -16,6 +22,24 @@ export const parsers: Plugin['parsers'] = { const ast = typescriptParser.parsers.typescript.parse(text, options) const filePath = options.filepath + // Programatic usage + if (!options.filepath) { + if (options.pandaConfigPath) { + configPath = options.pandaConfigPath + } + + const setupOptions = { configPath: options.pandaConfigPath, cwd: options.pandaCwd } + const ctx = (await builder.setup(setupOptions)) || builder.context + if (!ctx) return ast + + // cache the config path + if (!configPath) configPath = ctx.conf.path + + const pretty = new PrettyPanda(ast, ctx, options as any) + return pretty.format(text) + } + + // CLI/VSCode usage try { const ctx = await resolver.getOrCreate(filePath) if (!ctx) return ast @@ -23,7 +47,9 @@ export const parsers: Plugin['parsers'] = { const pretty = new PrettyPanda(ast, ctx, options as any) return pretty.format(text) - } catch { + } catch (err) { + console.log('prettier-plugin-panda error') + console.error(err) return ast } },