36

I have following Prettier commands:

prettier --parser typescript --write ./src/**/*.ts
prettier --parser typescript --write ./src/**/*.tsx

I would like to merge them to single one - use some king of regex to listen (write) on both .ts and also .tsx extensions.

Something like:

prettier --write ./src/**/*.ts(x?)
Jurosh
  • 6,984
  • 7
  • 40
  • 51

2 Answers2

65

Just found solution. Following command will target both ts and tsx:

prettier --write "./src/**/*.{ts,tsx}"

Prettier is using Glob syntax which is syntax similar to Regex, used in shell.

See GLOB syntax details: https://github.com/isaacs/node-glob/blob/master/README.md#glob-primer

Jurosh
  • 6,984
  • 7
  • 40
  • 51
  • 2
    Are you sure it's not your shell expanding that glob pattern? – robertklep Jun 26 '17 at 16:24
  • Hmm.. Maybe without `" "` will be used shell directly ? https://github.com/prettier/prettier#cli But I am on windows, so not sure.. Please if you can provide better explanation would be appreciated and I may accept your answer. Or just do edit.. – Jurosh Jun 26 '17 at 16:35
  • Ah, in Windows it might work differently. Anyway, it's not really relevant as it's working :D – robertklep Jun 26 '17 at 16:36
  • Yea it is working now, but always is good to know explanation why.. and if it's expanded by shell - not glob lib, it's pretty much difference - even if it's done only on Unix-like system.. – Jurosh Jun 26 '17 at 16:39
  • 1
    You can try and put quotes around it. If it still works, it's `prettier` that does the expansion. On Unix, you can also try `echo ./src/**/*.{ts,tsx}` to see how the shell expands it, perhaps that works on Windows too. – robertklep Jun 26 '17 at 17:39
  • I think the answer should be amended to surround with quotes for 'nix platforms. – Chris Jul 15 '19 at 19:29
  • @khoomeister Yep, that's true, let's use Prettier glob instead of relying on system. Updated answer. – Jurosh Aug 05 '19 at 11:38
  • How can I do this using a config file? @Jurosh – lomse May 16 '20 at 15:11
  • @lomse as far as I know - no, this needs to be in command line. – Jurosh May 17 '20 at 19:13
  • True. I figured it out later. Thanks. – lomse May 17 '20 at 19:14
  • Can I use `{ts,tsx}` in prettierrc config file? – Mikki Ukraine Apr 26 '23 at 19:48
16

If you want to run prettier command on multiple paths with multiple extensions use the following command:

prettier --write "src/**/*.{ts,tsx,js,jsx}" "pages/**/*.{ts,tsx,js,jsx}" "server/**/*.js"
ziishaned
  • 4,944
  • 3
  • 25
  • 32