1
1
const prettier = require ( "prettier" ) ;
2
2
3
3
app . post ( "/prettier/config-options" , ( req , res ) => {
4
- var config_data = req . body ;
5
- var prettier_config_path = config_data . prettier_config_path ;
6
- var prettier_config_options = config_data . prettier_config_options || { } ;
4
+ const config_data = req . body ;
5
+ const prettier_config_path = config_data . prettier_config_path ;
6
+ const prettier_config_options = config_data . prettier_config_options || { } ;
7
7
8
8
if ( prettier_config_path ) {
9
9
prettier
10
10
. resolveConfig ( undefined , { config : prettier_config_path } )
11
11
. then ( options => {
12
- var mergedConfigOptions = mergeConfigOptions ( options , prettier_config_options ) ;
12
+ const mergedConfigOptions = mergeConfigOptions ( options , prettier_config_options ) ;
13
13
res . set ( "Content-Type" , "application/json" )
14
14
res . json ( mergedConfigOptions ) ;
15
15
} )
@@ -20,12 +20,12 @@ app.post("/prettier/config-options", (req, res) => {
20
20
res . json ( prettier_config_options ) ;
21
21
} ) ;
22
22
23
- app . post ( "/prettier/format" , ( req , res ) => {
24
- var format_data = req . body ;
23
+ app . post ( "/prettier/format" , async ( req , res ) => {
24
+ const format_data = req . body ;
25
25
26
- var formatted_file_content = "" ;
26
+ let formatted_file_content = "" ;
27
27
try {
28
- formatted_file_content = prettier . format ( format_data . file_content , format_data . config_options ) ;
28
+ formatted_file_content = await prettierFormat ( format_data . file_content , format_data . config_options ) ;
29
29
} catch ( err ) {
30
30
res . status ( 500 ) . send ( "Error while formatting: " + err ) ;
31
31
return ;
@@ -34,7 +34,20 @@ app.post("/prettier/format", (req, res) => {
34
34
res . send ( formatted_file_content ) ;
35
35
} ) ;
36
36
37
- var mergeConfigOptions = function ( resolved_config_options , config_options ) {
37
+ const prettierFormat = async function ( file_content , config_options ) {
38
+ const result = prettier . format ( file_content , config_options ) ;
39
+
40
+ // Check if result is a Promise (version 3.0.0 and above)
41
+ if ( typeof result . then === 'function' ) {
42
+ return result ;
43
+ }
44
+
45
+ // If it's not a Promise (meaning it's a string), wrap it in a Promise (< 3.0.0)
46
+ return Promise . resolve ( result ) ;
47
+ }
48
+
49
+
50
+ const mergeConfigOptions = function ( resolved_config_options , config_options ) {
38
51
if ( resolved_config_options !== undefined && config_options !== undefined ) {
39
52
return extend ( resolved_config_options , config_options ) ;
40
53
}
@@ -46,15 +59,15 @@ var mergeConfigOptions = function(resolved_config_options, config_options) {
46
59
}
47
60
} ;
48
61
49
- var extend = function ( ) {
62
+ const extend = function ( ) {
50
63
// Variables
51
- var extended = { } ;
52
- var i = 0 ;
53
- var length = arguments . length ;
64
+ const extended = { } ;
65
+ let i = 0 ;
66
+ const length = arguments . length ;
54
67
55
68
// Merge the object into the extended object
56
- var merge = function ( obj ) {
57
- for ( var prop in obj ) {
69
+ const merge = function ( obj ) {
70
+ for ( const prop in obj ) {
58
71
if ( Object . prototype . hasOwnProperty . call ( obj , prop ) ) {
59
72
extended [ prop ] = obj [ prop ] ;
60
73
}
@@ -63,9 +76,8 @@ var extend = function() {
63
76
64
77
// Loop through each object and conduct a merge
65
78
for ( ; i < length ; i ++ ) {
66
- var obj = arguments [ i ] ;
79
+ const obj = arguments [ i ] ;
67
80
merge ( obj ) ;
68
81
}
69
-
70
82
return extended ;
71
83
} ;
0 commit comments