This is a small CLI tool that dumps all of Prettier’s default configuration options to a file in the format of your choice.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

index.js 3.9KB

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