prisma-case-format icon indicating copy to clipboard operation
prisma-case-format copied to clipboard

Allow programmatic usage of library (example: vscode formatter extension)

Open V-ed opened this issue 1 year ago • 2 comments

Currently, the only way to use this tool is through the CLI.

While I love your package and I find it great, I would love for my prisma schema file to be auto formatted using this tool instead of prisma's internal formatter. Currently, the way to automatically format the file is basically using the extension Gruntfuggly.triggertaskonsave and run a silent task on save that runs the command. It works, but it is clunky.

I think it might be possible to create an extension that calls a cli function, but having a proper function that transforms the input into an output (without directly writing to the filesystem) would allow for the vscode.languages.registerDocumentFormattingEditProvider to work its magic using the buffer rather than going through a terminal command.

Basically, move pretty much all of the run() function of the cli.ts file into an exported package, run THAT function from the cli.ts file, which would allow this package to be used programmatically.

I'm actually quite interested in making a vscode extension that uses this package for formatting prisma files, so if you don't have the time to do it yourself, I might pick this one up :)

V-ed avatar Feb 20 '24 06:02 V-ed

I think it might be possible to create an extension that calls a cli function, but having a proper function that transforms the input into an output (without directly writing to the filesystem) would allow for the vscode.languages.registerDocumentFormattingEditProvider to work its magic using the buffer rather than going through a terminal command.

Have you tried just doing like spawnSync and invoking prisma-case-format with the --dry-run flag? --dry-run should spit out the contents to stdout

Seems like a nifty idea to make prisma-case-format a drop-in replacement for prisma format. Unfortunately, I don't have the time or expertise to build anything like that, however you're totally welcome to make a PR for this and your other suggestion, and I'll get it merged in.

iiian avatar Feb 20 '24 19:02 iiian

Running the cli through a spawn process programmatically is indeed a solution (that I use myself in my projects for other stuff) but is a bit overkill, espeically when you already have all the necessary code to not have to do it through a cli.

I'm not saying get rid of the cli (although I don't think that's what you think I'm saying), but allowing to call the formatter in a programmatic way would be beneficial for performances inside (for exemple) a vscode extension.

I'd be happy to try and make a PR describing what I'm saying, I'll see how much time in have in the coming weeks!

V-ed avatar Feb 20 '24 19:02 V-ed