1
1
import fs from 'fs' ;
2
2
import plugin from './postcss' ;
3
3
4
- if ( process . argv . length < 3 ) {
5
- console . log ( [
6
- 'CSS Has Pseudo\n' ,
7
- ' Transforms CSS with :has {}\n' ,
8
- 'Usage:\n' ,
9
- ' css-has-pseudo source.css transformed.css' ,
10
- ' css-has-pseudo --in=source.css --out=transformed.css --opts={}' ,
11
- ' echo "body:has(:focus) {}" | css-has-pseudo\n'
12
- ] . join ( '\n' ) ) ;
13
- process . exit ( 0 ) ;
14
- }
15
-
16
4
// get process and plugin options from the command line
17
5
const fileRegExp = / ^ [ \w \/ . ] + $ / ;
18
6
const argRegExp = / ^ - - ( \w + ) = ( " | ' ) ? ( .+ ) \2$ / ;
@@ -41,6 +29,19 @@ const argo = process.argv.slice(2).reduce(
41
29
// get css from command line arguments or stdin
42
30
( argo . from === '<stdin>' ? getStdin ( ) : readFile ( argo . from ) )
43
31
. then ( css => {
32
+ if ( argo . from === '<stdin>' && ! css ) {
33
+ console . log ( [
34
+ 'CSS Has Pseudo\n' ,
35
+ ' Transforms CSS with :has {}\n' ,
36
+ 'Usage:\n' ,
37
+ ' css-has-pseudo source.css transformed.css' ,
38
+ ' css-has-pseudo --from=source.css --to=transformed.css --opts={}' ,
39
+ ' echo "body:has(:focus) {}" | css-has-pseudo\n'
40
+ ] . join ( '\n' ) ) ;
41
+
42
+ process . exit ( 0 ) ;
43
+ }
44
+
44
45
const pluginOpts = JSON . parse (
45
46
argo . opts
46
47
. replace ( relaxedJsonPropRegExp , '"$2": ' )
@@ -57,14 +58,26 @@ const argo = process.argv.slice(2).reduce(
57
58
( ) => `CSS was written to "${ argo . to } "`
58
59
)
59
60
}
60
- } ) . then (
61
+ } ) . catch (
62
+ error => {
63
+ if ( Object ( error ) . name === 'CssSyntaxError' ) {
64
+ throw new Error ( `PostCSS had trouble reading the file (${ error . reason } on line ${ error . line } , column ${ error . column } ).` ) ;
65
+ }
66
+
67
+ if ( Object ( error ) . errno === - 2 ) {
68
+ throw new Error ( `Sorry, "${ error . path } " could not be read.` ) ;
69
+ }
70
+
71
+ throw error ;
72
+ }
73
+ ) . then (
61
74
result => {
62
75
console . log ( result ) ;
63
76
64
77
process . exit ( 0 ) ;
65
78
} ,
66
79
error => {
67
- console . error ( error ) ;
80
+ console . error ( Object ( error ) . message || 'Something bad happened and we don’t even know what it was.' ) ;
68
81
69
82
process . exit ( 1 ) ;
70
83
}
0 commit comments