@@ -2,41 +2,56 @@ const fs = require("fs-extra");
2
2
const isFunction = require ( "lodash/isFunction" ) ;
3
3
const merge = require ( "lodash/merge" ) ;
4
4
const path = require ( "path" ) ;
5
+ const prettier = require ( "prettier" ) ;
5
6
const stripIndent = require ( "strip-indent" ) ;
6
7
const uniq = require ( "lodash/uniq" ) ;
7
- const { formatCode, formatJson, formatMd } = require ( "./format" ) ;
8
8
9
- let currentHandler ;
9
+ async function getPrettierConfig ( file ) {
10
+ return await prettier . resolveConfig ( file ) ;
11
+ }
10
12
11
- async function readIfExists ( file ) {
12
- return ( await fs . exists ( file ) ) ? ( await fs . readFile ( file ) ) . toString ( ) : null ;
13
+ async function formatCode ( file , data ) {
14
+ return prettier . format ( data , {
15
+ parser : "babel" ,
16
+ ...( await getPrettierConfig ( file ) )
17
+ } ) ;
13
18
}
14
19
15
- async function handleArray ( file ) {
16
- let data ;
17
- const curr = ( ( await readIfExists ( file . name ) ) || "" ) . split ( "\n" ) ;
18
- data = file . data . concat ( curr ) ;
19
- data = uniq ( data ) ;
20
- return data . filter ( Boolean ) . join ( "\n" ) ;
20
+ async function formatMd ( file , data ) {
21
+ return prettier . format ( data , {
22
+ parser : "markdown" ,
23
+ ...( await getPrettierConfig ( file ) )
24
+ } ) ;
25
+ }
26
+
27
+ async function readIfExists ( file ) {
28
+ return ( await fs . exists ( file ) ) ? ( await fs . readFile ( file ) ) . toString ( ) : null ;
21
29
}
22
30
23
31
async function handleJs ( file ) {
24
- const curr = ( await readIfExists ( file . name ) ) || file . data ;
25
- const data = file . overwrite ? file . data : curr ;
26
- return formatCode ( data ) ;
32
+ const currData = await readIfExists ( file . name ) ;
33
+ if ( file . overwrite || ! currData ) {
34
+ return await formatCode ( file . name , file . data ) ;
35
+ }
36
+ return currData ;
27
37
}
28
38
29
39
async function handleJson ( file ) {
30
- const curr = JSON . parse ( await readIfExists ( file . name ) ) ;
40
+ const currData = await readIfExists ( file . name ) ;
41
+ const currJson = JSON . parse ( currData ) ;
31
42
let data ;
32
43
33
- if ( curr ) {
44
+ if ( currData ) {
34
45
if ( file . overwrite ) {
35
- data = file . merge ? merge ( { } , curr , file . data ) : file . data ;
46
+ if ( file . merge ) {
47
+ data = merge ( { } , currJson , file . data ) ;
48
+ } else {
49
+ data = file . data ;
50
+ }
36
51
} else if ( file . merge ) {
37
- data = merge ( { } , file . data , curr ) ;
52
+ data = merge ( { } , file . data , currJson ) ;
38
53
} else {
39
- data = curr ;
54
+ return currData ;
40
55
}
41
56
} else {
42
57
data = file . data ;
@@ -46,13 +61,19 @@ async function handleJson(file) {
46
61
}
47
62
48
63
async function handleMd ( file ) {
49
- return formatMd ( await handleString ( file ) ) ;
64
+ const currData = await readIfExists ( file . name ) ;
65
+ if ( file . overwrite || ! currData ) {
66
+ return await formatMd ( file . name , file . data ) ;
67
+ }
68
+ return currData ;
50
69
}
51
70
52
71
async function handleString ( file ) {
53
- const curr = ( await readIfExists ( file . name ) ) || file . data ;
54
- const data = file . overwrite ? file . data : curr ;
55
- return stripIndent ( data ) . trim ( ) ;
72
+ const currData = await readIfExists ( file . name ) ;
73
+ if ( file . overwrite || ! currData ) {
74
+ return stripIndent ( file . data ) . trim ( ) ;
75
+ }
76
+ return currData ;
56
77
}
57
78
58
79
const mapExtname = {
0 commit comments