This is a small CLI tool that dumps all of Prettier’s default configuration options to a file in the format of your choice.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env node
  2. const fs = require('fs');
  3. const minimist = require('minimist');
  4. const path = require('path');
  5. const defaultConfig = {
  6. arrowParens: 'avoid',
  7. bracketSpacing: true,
  8. endOfLine: 'auto',
  9. filepath: '',
  10. htmlWhitespaceSensitivity: 'css',
  11. insertPragma: false,
  12. jsxBracketSameLine: false,
  13. jsxSingleQuote: false,
  14. parser: '',
  15. printWidth: 80,
  16. proseWrap: 'preserve',
  17. quoteProps: 'as-needed',
  18. requirePragma: false,
  19. semi: true,
  20. singleQuote: false,
  21. tabWidth: 2,
  22. trailingComma: 'none',
  23. useTabs: false,
  24. vueIndentScriptAndStyle: false,
  25. };
  26. const formats = {
  27. json: {
  28. filename: '.prettierrc',
  29. generate: function() {
  30. return JSON.stringify(defaultConfig, null, 2);
  31. },
  32. },
  33. yaml: {
  34. filename: '.prettierrc.yaml',
  35. generate: function() {
  36. return Object.entries(defaultConfig)
  37. .map(([key, value]) => {
  38. if (typeof value === 'string' && value === '') return `${key}: ''`;
  39. return `${key}: ${value}`;
  40. })
  41. .join('\n');
  42. },
  43. },
  44. toml: {
  45. filename: '.prettierrc.toml',
  46. generate: function() {
  47. return Object.entries(defaultConfig)
  48. .map(([key, value]) => {
  49. if (typeof value === 'string') return `${key} = "${value}"`;
  50. return `${key} = ${value}`;
  51. })
  52. .join('\n');
  53. },
  54. },
  55. js: {
  56. filename: '.prettierrc.js',
  57. generate: function() {
  58. const contents = Object.entries(defaultConfig)
  59. .map(([key, value]) => {
  60. if (typeof value === 'string') return ` ${key}: '${value}',`;
  61. return ` ${key}: ${value},`;
  62. })
  63. .join('\n');
  64. return `module.exports = {\n${contents}\n};`;
  65. },
  66. },
  67. 'package.json': {
  68. filename: 'package.json',
  69. generate: function() {
  70. const file = path.resolve('package.json');
  71. if (!fs.existsSync(file)) {
  72. console.error(
  73. 'Error: no package.json file found in the current directory'
  74. );
  75. process.exit(0);
  76. }
  77. const data = JSON.parse(fs.readFileSync(file));
  78. data.prettier = defaultConfig;
  79. return JSON.stringify(data, null, 2);
  80. },
  81. },
  82. };
  83. function writeToFile(filename, data) {
  84. fs.writeFile(path.resolve(filename), `${data}\n`, {}, () => {
  85. console.log(`Wrote default config to ${filename}`);
  86. process.exit(0);
  87. });
  88. }
  89. function writeToStdout(data) {
  90. process.stdout.write(`${data}\n`);
  91. process.exit(0);
  92. }
  93. function help(logger) {
  94. if (!logger) logger = console.log;
  95. const formatDisplay = Object.keys(formats).join('|');
  96. logger('USAGE:');
  97. logger(' prettier-default-config [OPTIONS]');
  98. logger();
  99. logger('OPTIONS:');
  100. logger(' --format <FORMAT> The config file format to generate.');
  101. logger(` <${formatDisplay}>`);
  102. logger(' default: json');
  103. logger();
  104. logger(' --stdout Write config to STDOUT rather than to a file');
  105. logger();
  106. logger(' --help Prints help information');
  107. process.exit(0);
  108. }
  109. function run() {
  110. const args = minimist(process.argv);
  111. if (args.help) help();
  112. const format = formats[args.format ? args.format : 'json'];
  113. if (format) {
  114. const config = format.generate();
  115. if (args.stdout) {
  116. writeToStdout(config);
  117. } else {
  118. writeToFile(format.filename, config);
  119. }
  120. } else {
  121. console.error('Error: invalid format');
  122. console.error();
  123. help(console.error);
  124. }
  125. }
  126. module.exports = {
  127. defaultConfig,
  128. formats,
  129. };
  130. if (require.main === module) {
  131. run();
  132. }