1
- import fs from 'fs' ;
1
+ import fsp from 'fs/promises ' ;
2
2
import path from 'path' ;
3
3
import { parseArguments , SignalValue } from './args' ;
4
4
import postcss from 'postcss' ;
@@ -8,170 +8,138 @@ export * from './help';
8
8
9
9
type PluginCreatorOptions = Record < string , unknown > | null ;
10
10
11
- export function cli ( plugin : PluginCreator < PluginCreatorOptions > , allowedPluginOpts : Array < string > , helpLogger : ( ) => void ) {
11
+ export async function cli ( plugin : PluginCreator < PluginCreatorOptions > , allowedPluginOpts : Array < string > , helpLogger : ( ) => void ) {
12
12
// get process and plugin options from the command line
13
13
const argo = parseArguments ( process . argv . slice ( 2 ) , allowedPluginOpts , helpLogger ) ;
14
14
if ( argo === SignalValue . InvalidArguments ) {
15
15
process . exit ( 1 ) ;
16
16
}
17
17
18
+ // Read from stdin and write to stdout
18
19
if ( argo . stdin && argo . stdout ) {
19
- return getStdin ( ) . then ( ( css ) => {
20
+ try {
21
+ const css = await getStdin ( ) ;
20
22
if ( ! css ) {
21
23
helpLogger ( ) ;
22
24
process . exit ( 1 ) ;
23
25
}
24
26
25
- const result = postcss ( [ plugin ( argo . pluginOptions ) ] ) . process ( css , {
27
+ const result = await postcss ( [ plugin ( argo . pluginOptions ) ] ) . process ( css , {
26
28
from : 'stdin' ,
27
29
to : 'stdout' ,
28
30
map : argo . inlineMap ? { inline : true } : false ,
29
31
} ) ;
30
32
31
- return result . css ;
32
- } ) . then ( ( result ) => {
33
- process . stdout . write ( result + ( argo . inlineMap ? '\n' : '' ) ) ;
34
-
35
- process . exit ( 0 ) ;
36
- } ) . catch ( ( error ) => {
33
+ process . stdout . write ( result . css + ( argo . inlineMap ? '\n' : '' ) ) ;
34
+ } catch ( error ) {
37
35
console . error ( argo . debug ? error : error . message ) ;
38
36
39
37
process . exit ( 1 ) ;
40
- } ) ;
38
+ }
39
+
40
+ return ;
41
41
}
42
42
43
+ // Read from stdin and write to a file
43
44
if ( argo . stdin ) {
44
45
let output = argo . output ;
45
46
if ( ! output && argo . outputDir ) {
46
47
output = path . join ( argo . outputDir , 'output.css' ) ;
47
48
}
48
49
49
- return getStdin ( ) . then ( ( css ) => {
50
+ try {
51
+ const css = await getStdin ( ) ;
50
52
if ( ! css ) {
51
53
helpLogger ( ) ;
52
54
process . exit ( 1 ) ;
53
55
}
54
56
55
- const result = postcss ( [ plugin ( argo . pluginOptions ) ] ) . process ( css , {
57
+ const result = await postcss ( [ plugin ( argo . pluginOptions ) ] ) . process ( css , {
56
58
from : 'stdin' ,
57
59
to : output ,
58
60
map : ( argo . inlineMap || argo . externalMap ) ? { inline : argo . inlineMap } : false ,
59
61
} ) ;
60
62
61
63
if ( argo . externalMap && result . map ) {
62
- return Promise . all ( [
63
- writeFile ( output , result . css ) ,
64
- writeFile ( `${ output } .map` , result . map . toString ( ) ) ,
65
- ] ) . then ( ( ) => {
66
- console . log ( `CSS was written to "${ path . normalize ( output ) } "` ) ;
67
- return ;
68
- } ) ;
64
+ await Promise . all ( [
65
+ await fsp . writeFile ( output , result . css + ( argo . inlineMap ? '\n' : '' ) ) ,
66
+ await fsp . writeFile ( `${ output } .map` , result . map . toString ( ) ) ,
67
+ ] ) ;
68
+ } else {
69
+ await fsp . writeFile ( output , result . css + ( argo . inlineMap ? '\n' : '' ) ) ;
69
70
}
70
71
71
- return writeFile ( output , result . css + ( argo . inlineMap ? '\n' : '' ) ) ;
72
- } ) . then ( ( ) => {
73
72
console . log ( `CSS was written to "${ path . normalize ( output ) } "` ) ;
74
-
75
- process . exit ( 0 ) ;
76
- } ) . catch ( ( error ) => {
73
+ } catch ( error ) {
77
74
console . error ( argo . debug ? error : error . message ) ;
78
75
79
76
process . exit ( 1 ) ;
80
- } ) ;
77
+ }
78
+
79
+ return ;
81
80
}
82
81
82
+ // Read from one or more files and write to stdout
83
83
if ( argo . stdout ) {
84
- const outputs = argo . inputs . map ( ( input ) => {
85
- return {
86
- input : input ,
87
- result : null ,
88
- } ;
89
- } ) ;
90
-
91
- return Promise . all ( argo . inputs . map ( ( input ) => {
92
- return readFile ( input ) . then ( ( css ) => {
93
- const result = postcss ( [ plugin ( argo . pluginOptions ) ] ) . process ( css , {
84
+ let allCss : Array < string > = [ ] ;
85
+ try {
86
+ allCss = await Promise . all ( argo . inputs . map ( async ( input ) => {
87
+ const css = await fsp . readFile ( input ) ;
88
+ const result = await postcss ( [ plugin ( argo . pluginOptions ) ] ) . process ( css , {
94
89
from : input ,
95
90
to : 'stdout' ,
96
91
map : false ,
97
92
} ) ;
98
93
99
94
return result . css ;
100
- } ) . then ( ( result ) => {
101
- outputs . find ( ( output ) => output . input === input ) . result = result ;
102
- } ) ;
103
- } ) ) . then ( ( ) => {
104
- outputs . forEach ( ( output ) => {
105
- process . stdout . write ( output . result ) ;
106
- } ) ;
107
-
108
- process . exit ( 0 ) ;
109
- } ) . catch ( ( error ) => {
95
+ } ) ) ;
96
+ } catch ( error ) {
110
97
console . error ( argo . debug ? error : error . message ) ;
111
98
112
99
process . exit ( 1 ) ;
113
- } ) ;
114
- }
115
-
116
- return Promise . all ( argo . inputs . map ( ( input ) => {
117
- let output = argo . output ;
118
- if ( argo . outputDir ) {
119
- output = path . join ( argo . outputDir , path . basename ( input ) ) ;
120
100
}
121
- if ( argo . replace ) {
122
- output = input ;
101
+
102
+ for ( const css of allCss ) {
103
+ process . stdout . write ( css ) ;
123
104
}
124
105
125
- return readFile ( input ) . then ( ( css ) => {
126
- return postcss ( [ plugin ( argo . pluginOptions ) ] ) . process ( css , {
106
+ return ;
107
+ }
108
+
109
+ // Read from one or more files and write to as many files
110
+ try {
111
+ await Promise . all ( argo . inputs . map ( async ( input ) => {
112
+ let output = argo . output ;
113
+ if ( argo . outputDir ) {
114
+ output = path . join ( argo . outputDir , path . basename ( input ) ) ;
115
+ }
116
+ if ( argo . replace ) {
117
+ output = input ;
118
+ }
119
+
120
+ const css = await fsp . readFile ( input ) ;
121
+ const result = await postcss ( [ plugin ( argo . pluginOptions ) ] ) . process ( css , {
127
122
from : input ,
128
123
to : output ,
129
124
map : ( argo . inlineMap || argo . externalMap ) ? { inline : argo . inlineMap } : false ,
130
125
} ) ;
131
- } ) . then ( ( result ) => {
126
+
132
127
if ( argo . externalMap && result . map ) {
133
- return Promise . all ( [
134
- writeFile ( output , result . css ) ,
135
- writeFile ( `${ output } .map` , result . map . toString ( ) ) ,
136
- ] ) . then ( ( ) => {
137
- console . log ( `CSS was written to "${ path . normalize ( output ) } "` ) ;
138
- return ;
139
- } ) ;
128
+ await Promise . all ( [
129
+ await fsp . writeFile ( output , result . css + ( argo . inlineMap ? '\n' : '' ) ) ,
130
+ await fsp . writeFile ( `${ output } .map` , result . map . toString ( ) ) ,
131
+ ] ) ;
132
+ } else {
133
+ await fsp . writeFile ( output , result . css + ( argo . inlineMap ? '\n' : '' ) ) ;
140
134
}
141
135
142
- return writeFile ( output , result . css + ( argo . inlineMap ? '\n' : '' ) ) . then ( ( ) => {
143
- console . log ( `CSS was written to "${ path . normalize ( output ) } "` ) ;
144
- } ) ;
145
- } ) ;
146
- } ) ) . catch ( ( error ) => {
136
+ console . log ( `CSS was written to "${ path . normalize ( output ) } "` ) ;
137
+ } ) ) ;
138
+ } catch ( error ) {
147
139
console . error ( argo . debug ? error : error . message ) ;
148
140
149
141
process . exit ( 1 ) ;
150
- } ) ;
151
- }
152
-
153
- function readFile ( pathname : string ) : Promise < string > {
154
- return new Promise ( ( resolve , reject ) => {
155
- fs . readFile ( pathname , 'utf8' , ( error , data ) => {
156
- if ( error ) {
157
- reject ( error ) ;
158
- } else {
159
- resolve ( data ) ;
160
- }
161
- } ) ;
162
- } ) ;
163
- }
164
-
165
- function writeFile ( pathname : string , data : string ) : Promise < void > {
166
- return new Promise ( ( resolve , reject ) => {
167
- fs . writeFile ( pathname , data , ( error ) => {
168
- if ( error ) {
169
- reject ( error ) ;
170
- } else {
171
- resolve ( ) ;
172
- }
173
- } ) ;
174
- } ) ;
142
+ }
175
143
}
176
144
177
145
function getStdin ( ) : Promise < string > {
0 commit comments