-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprettier.mts
70 lines (64 loc) · 1.8 KB
/
prettier.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* @file dprint - prettier
* @module dprint/prettier
*/
import { ok } from 'devlop'
import editorconfig from 'editorconfig'
import { Transform } from 'node:stream'
import * as prettier from 'prettier'
declare module 'editorconfig' {
interface KnownProps {
max_line_length?: number | undefined
single_attribute_per_line?: boolean | undefined
}
}
process.stdin.pipe(new Transform({
/**
* Formats a file using `prettier`.
*
* @see https://prettier.io
*
* @async
*
* @param {Buffer} buffer
* Data buffer
* @return {Promise<string>}
* Formatted file content
*/
async transform(buffer: Buffer): Promise<string> {
const [filepath] = process.argv.slice(2)
ok(typeof filepath === 'string', 'expected `filepath`')
const {
end_of_line = 'lf',
indent_size = 2,
indent_style = 'space',
max_line_length = 80,
quote_type = 'single',
single_attribute_per_line = false,
spaces_around_brackets = 'inside',
tab_width = 2
} = await editorconfig.parse(filepath)
/**
* Formatted text.
*
* @const {string} text
*/
const text: string = await prettier.format(buffer.toString(), {
arrowParens: 'avoid',
bracketSpacing: spaces_around_brackets === 'inside',
endOfLine: end_of_line === 'unset' ? 'auto' : end_of_line,
filepath,
jsxSingleQuote: quote_type === 'single',
printWidth: max_line_length,
proseWrap: 'always',
quoteProps: 'as-needed',
semi: false,
singleAttributePerLine: single_attribute_per_line,
singleQuote: quote_type === 'single',
tabWidth: tab_width === 'unset' ? 2 : tab_width,
trailingComma: 'none',
useTabs: [indent_size, indent_style].includes('tab')
})
return process.stdout.write(text), text
}
}))