@@ -6,6 +6,7 @@ const fs = require("fs");
6
6
const os = require ( "os" ) ;
7
7
const path = require ( "path" ) ;
8
8
const { URL } = require ( "url" ) ;
9
+ const { promisify } = require ( "util" ) ;
9
10
const md = require ( "markdown-it" ) ( ) ;
10
11
const pluginInline = require ( "markdown-it-for-inline" ) ;
11
12
const pluginKatex = require ( "markdown-it-katex" ) ;
@@ -26,29 +27,19 @@ const version = packageJson.version;
26
27
27
28
const deprecatedRuleNames = [ "MD002" ] ;
28
29
29
- function promisify ( func , ...args ) {
30
- return new Promise ( ( resolve , reject ) => {
31
- func ( ...args , ( error , result ) => {
32
- if ( error ) {
33
- return reject ( error ) ;
34
- }
35
- return resolve ( result ) ;
36
- } ) ;
37
- } ) ;
38
- }
39
-
40
30
function createTestForFile ( file ) {
31
+ const markdownlintPromise = promisify ( markdownlint ) ;
41
32
return function testForFile ( test ) {
42
33
const detailedResults = / [ / \\ ] d e t a i l e d - r e s u l t s - / . test ( file ) ;
43
34
test . plan ( detailedResults ? 3 : 2 ) ;
44
35
const resultsFile = file . replace ( / \. m d $ / , ".results.json" ) ;
45
36
const fixedFile = file . replace ( / \. m d $ / , ".md.fixed" ) ;
46
37
const configFile = file . replace ( / \. m d $ / , ".json" ) ;
47
38
let mergedConfig = null ;
48
- const actualPromise = promisify ( fs . stat , configFile )
39
+ const actualPromise = fs . promises . stat ( configFile )
49
40
. then (
50
41
function configFileExists ( ) {
51
- return promisify ( fs . readFile , configFile , helpers . utf8Encoding )
42
+ return fs . promises . readFile ( configFile , helpers . utf8Encoding )
52
43
. then ( JSON . parse ) ;
53
44
} ,
54
45
function noConfigFile ( ) {
@@ -60,7 +51,7 @@ function createTestForFile(file) {
60
51
...defaultConfig ,
61
52
...config
62
53
} ;
63
- return promisify ( markdownlint , {
54
+ return markdownlintPromise ( {
64
55
"files" : [ file ] ,
65
56
"config" : mergedConfig ,
66
57
"resultVersion" : detailedResults ? 2 : 3
@@ -70,13 +61,13 @@ function createTestForFile(file) {
70
61
function diffFixedFiles ( resultVersion2or3 ) {
71
62
return detailedResults ?
72
63
Promise . all ( [
73
- promisify ( markdownlint , {
64
+ markdownlintPromise ( {
74
65
"files" : [ file ] ,
75
66
"config" : mergedConfig ,
76
67
"resultVersion" : 3
77
68
} ) ,
78
- promisify ( fs . readFile , file , helpers . utf8Encoding ) ,
79
- promisify ( fs . readFile , fixedFile , helpers . utf8Encoding )
69
+ fs . promises . readFile ( file , helpers . utf8Encoding ) ,
70
+ fs . promises . readFile ( fixedFile , helpers . utf8Encoding )
80
71
] )
81
72
. then ( function validateApplyFixes ( fulfillments ) {
82
73
const [ resultVersion3 , content , expected ] = fulfillments ;
@@ -107,7 +98,7 @@ function createTestForFile(file) {
107
98
}
108
99
) ;
109
100
const expectedPromise = detailedResults ?
110
- promisify ( fs . readFile , resultsFile , helpers . utf8Encoding )
101
+ fs . promises . readFile ( resultsFile , helpers . utf8Encoding )
111
102
. then (
112
103
function fileContents ( contents ) {
113
104
const errorObjects = JSON . parse ( contents ) ;
@@ -119,7 +110,7 @@ function createTestForFile(file) {
119
110
} ) ;
120
111
return errorObjects ;
121
112
} ) :
122
- promisify ( fs . readFile , file , helpers . utf8Encoding )
113
+ fs . promises . readFile ( file , helpers . utf8Encoding )
123
114
. then (
124
115
function fileContents ( contents ) {
125
116
const lines = contents . split ( helpers . newLineRe ) ;
@@ -153,11 +144,11 @@ function createTestForFile(file) {
153
144
if ( detailedResults ) {
154
145
return test . ok ( true ) ;
155
146
}
156
- return promisify ( fs . readFile , file , helpers . utf8Encoding )
147
+ return fs . promises . readFile ( file , helpers . utf8Encoding )
157
148
. then (
158
149
function applyFixes ( content ) {
159
150
const corrections = helpers . applyFixes ( content , errors ) ;
160
- return promisify ( markdownlint , {
151
+ return markdownlintPromise ( {
161
152
"strings" : {
162
153
"input" : corrections
163
154
} ,
0 commit comments