orval icon indicating copy to clipboard operation
orval copied to clipboard

Run prettier on generated code.

Open Boorj opened this issue 3 years ago • 9 comments

Prettier is already imported. I see no disadvantages to use it for generated code. That will correct all ugly linebreaks produced

  1. add src/utils/formatter.ts
import Prettier from 'prettier';
export function prettifyTypescript(data){
    return Prettier.format(data, {
        parser: 'typescript',
        "useTabs": false,
        "tabWidth": 4,
        "singleQuote": true,
        "semi": true,
        "printWidth": 100,
        "trailingComma": "all",
        "proseWrap": "never"
    });
}
  1. apply prettifyTypescript in all writers before writing TS-code.

Boorj avatar May 23 '21 12:05 Boorj

Prettier is use as dev dependency at the moment. That's a new feature that can be added but should be config in the orval config file. Also could be good to try to find a prettier config in the project and not only apply a custom config.

anymaniax avatar May 23 '21 12:05 anymaniax

Have some concerns about using hardcoded config instead of flexible user config. But as a default option - definitly.

p.s. @anymaniax, btw have you checked https://openapi.tools ? Orval deserves to be in the list.

Boorj avatar May 23 '21 12:05 Boorj

@Boorj I didn't know about https://openapi.tools could be interesting to add Orval there

anymaniax avatar May 23 '21 12:05 anymaniax

@Boorj We should only use the hardcoded config when we cannot find the user config in its own project. And what I meant is to add a property in the orval config to activate prettier or override the config found or the default one.

anymaniax avatar May 23 '21 13:05 anymaniax

I am having trouble with this feature. I have Orval and Prettier in my devDependencies

        "orval": "^6.5.3",
        "prettier": "^2.5.1",

When I run npm run codegen i get this output..

⚠️ authorization - Prettier not found 🎉 authorization - Your OpenAPI spec has been converted into ready to use orval!

melloware avatar Feb 04 '22 14:02 melloware

Probably because with this code it's only using the global installed prettier

anymaniax avatar Feb 04 '22 14:02 anymaniax

hmmm i dont have prettier installed globally.

melloware avatar Feb 04 '22 14:02 melloware

yep, it's a bug need to find a better solution for this

anymaniax avatar Feb 04 '22 14:02 anymaniax

You can run prettier on pre-commit hook. Probably another solution could be to add prettier as a peer dependency.

Also, I made a pull request #459 where you can run a script (prettier, lint, etc...) when client is generated by orval.

c0m1t avatar Jun 29 '22 23:06 c0m1t

This is easy too..


"scripts": {
		"format": "prettier --write \"{src,__tests__}/**/*.{ts,tsx}\"",
		"codegen": "orval && npm run format",
		"start": "react-app-rewired start"

melloware avatar Nov 28 '23 23:11 melloware