1
- var eslintStandardConfig = require ( 'eslint-config-standard' )
1
+ const prettier = require ( "prettier" ) ;
2
2
var format = require ( 'prettier-eslint' )
3
- var fsReadFile = require ( 'fs-readfile-promise' )
4
- var fsWriteFile = require ( 'fs-writefile-promise/lib/node7' )
3
+ const fs = require ( "fs" ) ;
5
4
var path = require ( 'path' )
6
5
7
- function processFilePath ( { file, encoding, filePath, eslintConfig, prettierOptions, logLevel, eslintPath, prettierPath } ) {
6
+ const PRETTIER_ESLINT_PLUGIN = 'PrettierEslintPlugin' ;
7
+ const DEFAULT_ENCODING = "utf-8" ;
8
+ const DEFAULT_EXTENSIONS = prettier . getSupportInfo
9
+ ? prettier
10
+ . getSupportInfo ( )
11
+ . languages . map ( l => l . extensions )
12
+ . reduce ( ( accumulator , currentValue ) => accumulator . concat ( currentValue ) )
13
+ : [
14
+ ".css" ,
15
+ ".graphql" ,
16
+ ".js" ,
17
+ ".json" ,
18
+ ".jsx" ,
19
+ ".less" ,
20
+ ".sass" ,
21
+ ".scss" ,
22
+ ".ts" ,
23
+ ".tsx" ,
24
+ ".vue" ,
25
+ ".yaml" ,
26
+ ] ;
27
+
28
+ function processFilePath ( { fileCurrent, encoding, filePath, eslintConfig, prettierOptions, logLevel, eslintPath, prettierPath } ) {
8
29
return new Promise ( ( resolve , reject ) => {
9
- fsReadFile ( file , { encoding : encoding } )
10
- . then ( buffer => buffer . toString ( ) )
11
- . catch ( err => { reject ( err . message ) } )
12
- . then ( source => {
30
+ fs . readFile ( fileCurrent , encoding , ( err , source ) => {
31
+ if ( err ) {
32
+ return reject ( err ) ;
33
+ }
34
+
35
+ try {
13
36
const fmtOptions = {
14
37
text : source ,
15
38
filePath, eslintConfig, prettierOptions, logLevel, eslintPath, prettierPath
16
39
}
40
+
17
41
const formatted = format ( fmtOptions )
42
+
18
43
if ( formatted !== source ) {
19
- fsWriteFile ( file , formatted , { encoding : encoding } )
20
- . catch ( err => reject ( err . message ) )
21
- . then ( ( ) => { resolve ( 'success!' ) } )
44
+ fs . writeFile ( fileCurrent , prettierSource , this . encoding , err => {
45
+ if ( err ) {
46
+ return reject ( err ) ;
47
+ }
48
+ resolve ( 'success!' ) ;
49
+ } ) ;
50
+ } else {
51
+ resolve ( 'success!' ) ;
22
52
}
23
- } )
24
- . catch ( err => { reject ( err . message ) } )
53
+ }
54
+ catch ( err ) {
55
+ return reject ( err ) ;
56
+ }
57
+ } ) ;
25
58
} )
26
59
}
27
60
28
61
class PrettierEslintPlugin {
29
62
constructor (
30
63
{
31
- encoding = 'utf-8' ,
32
- extensions = [ '.js' , '.jsx' ] ,
64
+ encoding = DEFAULT_ENCODING ,
65
+ extensions = DEFAULT_EXTENSIONS ,
33
66
// prettier-eslint API
34
67
filePath, eslintConfig, prettierOptions, logLevel, eslintPath,
35
68
prettierPath, sillyLogs, config
@@ -49,38 +82,36 @@ class PrettierEslintPlugin {
49
82
}
50
83
51
84
apply ( compiler ) {
52
- compiler . plugin ( 'emit' , ( compilation , callback ) => {
53
- // Explore each chunk (build output):
54
- compilation . chunks . forEach ( chunk => {
55
- // Explore each module within the chunk (built inputs):
56
- chunk . modules . forEach ( module => {
57
- if ( ! module . fileDependencies ) return
58
- // Explore each source file path that was included into the module
59
- module . fileDependencies . forEach ( file => {
60
- // match extensions and exclude node modules
61
- if (
62
- this . extensions . indexOf ( path . extname ( file ) ) !== - 1 &&
63
- file . indexOf ( 'node_modules' ) === - 1
64
- ) {
65
- processFilePath ( {
66
- file : file ,
67
- encoding : this . encoding ,
85
+ compiler . hooks . emit . tapAsync ( PRETTIER_ESLINT_PLUGIN , ( compilation , callback ) => {
86
+ const promises = [ ] ;
87
+ if ( ! compilation . fileDependencies ) return ;
88
+ compilation . fileDependencies . forEach ( fileCurrent => {
89
+ if ( this . extensions . indexOf ( path . extname ( fileCurrent ) ) === - 1 &&
90
+ fileCurrent . indexOf ( 'node_modules' ) === - 1 ) {
91
+ return ;
92
+ }
68
93
69
- filePath : this . filePath ,
70
- eslintConfig : this . eslintConfig ,
71
- prettierOptions : this . prettierOptions ,
72
- logLevel : this . logLevel ,
73
- eslintPath : this . eslintPath ,
74
- prettierPath : this . prettierPath ,
75
- } )
76
- . then ( ( ) => { console . log ( 'succeed' ) } )
77
- . catch ( err => { console . error ( err ) } )
78
- }
94
+ promises . push (
95
+ processFilePath ( {
96
+ fileCurrent : fileCurrent ,
97
+ encoding : this . encoding ,
98
+
99
+ filePath : this . filePath ,
100
+ eslintConfig : this . eslintConfig ,
101
+ prettierOptions : this . prettierOptions ,
102
+ logLevel : this . logLevel ,
103
+ eslintPath : this . eslintPath ,
104
+ prettierPath : this . prettierPath ,
79
105
} )
80
- } )
81
- } )
82
- callback ( )
83
- } )
106
+ ) ;
107
+ } ) ;
108
+
109
+ Promise . all ( promises ) . then ( ( ) => {
110
+ callback ( ) ;
111
+ } ) . catch ( err => {
112
+ callback ( err ) ;
113
+ } ) ;
114
+ } ) ;
84
115
}
85
116
}
86
117
0 commit comments