|
@@ -3,42 +3,42 @@
|
3
|
3
|
const fs = require('fs');
|
4
|
4
|
const minimist = require('minimist');
|
5
|
5
|
const path = require('path');
|
|
6
|
+const prettier = require('prettier');
|
6
|
7
|
|
7
|
|
-const defaultConfig = {
|
8
|
|
- arrowParens: 'avoid',
|
9
|
|
- bracketSpacing: true,
|
10
|
|
- endOfLine: 'auto',
|
11
|
|
- filepath: '',
|
12
|
|
- htmlWhitespaceSensitivity: 'css',
|
13
|
|
- insertPragma: false,
|
14
|
|
- jsxBracketSameLine: false,
|
15
|
|
- jsxSingleQuote: false,
|
16
|
|
- parser: '',
|
17
|
|
- printWidth: 80,
|
18
|
|
- proseWrap: 'preserve',
|
19
|
|
- quoteProps: 'as-needed',
|
20
|
|
- requirePragma: false,
|
21
|
|
- semi: true,
|
22
|
|
- singleQuote: false,
|
23
|
|
- tabWidth: 2,
|
24
|
|
- trailingComma: 'none',
|
25
|
|
- useTabs: false,
|
26
|
|
- vueIndentScriptAndStyle: false,
|
|
8
|
+const defaultConfigFor = function(format) {
|
|
9
|
+ let config = prettier.getSupportInfo().options;
|
|
10
|
+
|
|
11
|
+ if (!['js', 'yaml'].includes(format)) {
|
|
12
|
+ config = config.filter(
|
|
13
|
+ option => ![null, Infinity].includes(option.default)
|
|
14
|
+ );
|
|
15
|
+ }
|
|
16
|
+
|
|
17
|
+ if (format != 'js') {
|
|
18
|
+ config = config.filter(option => option.default !== undefined);
|
|
19
|
+ }
|
|
20
|
+
|
|
21
|
+ return Object.fromEntries(
|
|
22
|
+ config.map(option => [option.name, option.default])
|
|
23
|
+ );
|
27
|
24
|
};
|
28
|
25
|
|
29
|
26
|
const formats = {
|
30
|
27
|
json: {
|
31
|
28
|
filename: '.prettierrc',
|
32
|
29
|
generate: function() {
|
33
|
|
- return JSON.stringify(defaultConfig, null, 2);
|
|
30
|
+ return JSON.stringify(defaultConfigFor('json'), null, 2);
|
34
|
31
|
},
|
35
|
32
|
},
|
36
|
33
|
yaml: {
|
37
|
34
|
filename: '.prettierrc.yaml',
|
38
|
35
|
generate: function() {
|
39
|
|
- return Object.entries(defaultConfig)
|
|
36
|
+ return Object.entries(defaultConfigFor('yaml'))
|
40
|
37
|
.map(([key, value]) => {
|
41
|
38
|
if (typeof value === 'string' && value === '') return `${key}: ''`;
|
|
39
|
+ if (value === null) return `${key}: ~`;
|
|
40
|
+ if (value === Infinity) return `${key}: .inf`;
|
|
41
|
+ if (!!value.forEach && value.length === 0) return `${key}: []`;
|
42
|
42
|
return `${key}: ${value}`;
|
43
|
43
|
})
|
44
|
44
|
.join('\n');
|
|
@@ -47,9 +47,10 @@ const formats = {
|
47
|
47
|
toml: {
|
48
|
48
|
filename: '.prettierrc.toml',
|
49
|
49
|
generate: function() {
|
50
|
|
- return Object.entries(defaultConfig)
|
|
50
|
+ return Object.entries(defaultConfigFor('toml'))
|
51
|
51
|
.map(([key, value]) => {
|
52
|
52
|
if (typeof value === 'string') return `${key} = "${value}"`;
|
|
53
|
+ if (!!value.forEach && value.length === 0) return `${key} = []`;
|
53
|
54
|
return `${key} = ${value}`;
|
54
|
55
|
})
|
55
|
56
|
.join('\n');
|
|
@@ -58,9 +59,12 @@ const formats = {
|
58
|
59
|
js: {
|
59
|
60
|
filename: '.prettierrc.js',
|
60
|
61
|
generate: function() {
|
61
|
|
- const contents = Object.entries(defaultConfig)
|
|
62
|
+ const contents = Object.entries(defaultConfigFor('js'))
|
62
|
63
|
.map(([key, value]) => {
|
63
|
64
|
if (typeof value === 'string') return ` ${key}: '${value}',`;
|
|
65
|
+ if (typeof value === 'undefined') return ` ${key}: undefined,`;
|
|
66
|
+ if (value === null) return ` ${key}: null,`;
|
|
67
|
+ if (!!value.forEach && value.length === 0) return `${key}: [],`;
|
64
|
68
|
return ` ${key}: ${value},`;
|
65
|
69
|
})
|
66
|
70
|
.join('\n');
|
|
@@ -78,7 +82,7 @@ const formats = {
|
78
|
82
|
process.exit(0);
|
79
|
83
|
}
|
80
|
84
|
const data = JSON.parse(fs.readFileSync(file));
|
81
|
|
- data.prettier = defaultConfig;
|
|
85
|
+ data.prettier = defaultConfigFor('json');
|
82
|
86
|
return JSON.stringify(data, null, 2);
|
83
|
87
|
},
|
84
|
88
|
},
|
|
@@ -137,7 +141,7 @@ function run() {
|
137
|
141
|
}
|
138
|
142
|
|
139
|
143
|
module.exports = {
|
140
|
|
- defaultConfig,
|
|
144
|
+ defaultConfigFor,
|
141
|
145
|
formats,
|
142
|
146
|
};
|
143
|
147
|
|