|
| 1 | +import { execa } from 'execa'; |
| 2 | +import fs from 'fs-extra'; |
| 3 | +import { responseT } from '../utils/types.js'; |
| 4 | +import { errorText, successText } from '../utils/utils.js'; |
| 5 | + |
| 6 | +export const configurePrettier = async (response: responseT, pmi: string[], pm: string) => { |
| 7 | + try { |
| 8 | + await execa( |
| 9 | + pmi[0], |
| 10 | + [ |
| 11 | + pmi[1], |
| 12 | + '--save-dev', |
| 13 | + 'prettier', |
| 14 | + '@ianvs/prettier-plugin-sort-imports', |
| 15 | + 'prettier-plugin-css-order', |
| 16 | + 'prettier-plugin-organize-attributes', |
| 17 | + 'prettier-plugin-tailwindcss', |
| 18 | + ].filter((str) => str !== ''), |
| 19 | + { stdio: 'ignore' }, |
| 20 | + ); |
| 21 | + |
| 22 | + const prettierConfigContent = `{ |
| 23 | + "printWidth": 120, |
| 24 | + "tabWidth": 4, |
| 25 | + "useTabs": false, |
| 26 | + "semi": true, |
| 27 | + "singleQuote": true, |
| 28 | + "jsxSingleQuote": true, |
| 29 | + "endOfLine": "lf", |
| 30 | + "plugins": [ |
| 31 | + "@ianvs/prettier-plugin-sort-imports", |
| 32 | + "prettier-plugin-css-order", |
| 33 | + "prettier-plugin-organize-attributes", |
| 34 | + "prettier-plugin-tailwindcss" |
| 35 | + ] |
| 36 | + } |
| 37 | + `; |
| 38 | + |
| 39 | + try { |
| 40 | + await fs.writeFile('./.prettierrc.json', prettierConfigContent); |
| 41 | + } catch (error) { |
| 42 | + console.error(errorText(`Error creating ${'.prettierrc.json'} file:`), error); |
| 43 | + process.exit(1); |
| 44 | + } |
| 45 | + |
| 46 | + await execa(pm, ['run', 'prettier', '.', '-w'], { stdio: 'ignore' }); |
| 47 | + |
| 48 | + if (!response['prettier']) { |
| 49 | + await execa( |
| 50 | + pm, |
| 51 | + [ |
| 52 | + 'remove', |
| 53 | + 'prettier', |
| 54 | + '@ianvs/prettier-plugin-sort-imports', |
| 55 | + 'prettier-plugin-css-order', |
| 56 | + 'prettier-plugin-organize-attributes', |
| 57 | + 'prettier-plugin-tailwindcss', |
| 58 | + ], |
| 59 | + { stdio: 'ignore' }, |
| 60 | + ); |
| 61 | + try { |
| 62 | + await fs.remove('./.prettierrc.json'); |
| 63 | + } catch (error) { |
| 64 | + console.error(errorText(`Error removing ${'.prettierrc.json'} file:`), error); |
| 65 | + process.exit(1); |
| 66 | + } |
| 67 | + } else { |
| 68 | + console.log(successText('Prettier installed successfully!')); |
| 69 | + } |
| 70 | + } catch (error) { |
| 71 | + console.error(error); |
| 72 | + process.exit(1); |
| 73 | + } |
| 74 | +}; |
0 commit comments