22
33'use strict' ;
44
5- const async = require ( 'async' ) ;
65const chalk = require ( 'chalk' ) ;
76const exit = require ( 'exit' ) ;
87const program = require ( 'commander' ) ;
@@ -14,30 +13,17 @@ program
1413 . option ( '-l, --lint' , 'Search of safe and unsafe replacements' )
1514 . option ( '-s, --sort' , 'Sort results' )
1615 . option ( '--no-colors' , 'Clean output without colors' )
16+ . option ( '--stdin' , 'Process text provided on <STDIN>' )
17+ . option ( '--stdin-filename <file>' , 'Specify filename to process STDIN as' )
1718 . parse ( process . argv ) ;
1819
1920chalk . enabled = program . colors ;
2021
21- if ( process . stdin . isTTY && ! program . args . length ) {
22+ if ( ! program . stdin && ! program . args . length ) {
2223 program . help ( ) ;
2324}
2425
25- if ( process . stdin . isTTY ) {
26- const tasks = [ ] ;
27- program . args . forEach ( function ( resource ) {
28- tasks . push ( function ( callback ) {
29- if ( resource . search ( / ^ h t t p s ? : / ) !== - 1 ) {
30- utils . _processUrl ( resource , callback ) ;
31- } else {
32- utils . _processFile ( resource , callback ) ;
33- }
34- } ) ;
35- } ) ;
36-
37- async . series ( tasks , function ( ) {
38- exit ( process . exitCode ) ;
39- } ) ;
40- } else {
26+ if ( program . stdin ) {
4127 let text = '' ;
4228
4329 process . stdin
@@ -49,7 +35,19 @@ if (process.stdin.isTTY) {
4935 }
5036 } )
5137 . on ( 'end' , function ( ) {
52- utils . _processText ( text , 'stdin' ) ;
38+ utils . _processText ( text , program . stdinFilename || 'stdin' ) ;
5339 exit ( process . exitCode ) ;
5440 } ) ;
41+ } else {
42+ Promise . all ( program . args . map ( resource => {
43+ return new Promise ( resolve => {
44+ if ( resource . search ( / ^ h t t p s ? : / ) !== - 1 ) {
45+ utils . _processUrl ( resource , resolve ) ;
46+ } else {
47+ utils . _processFile ( resource , resolve ) ;
48+ }
49+ } ) ;
50+ } ) ) . then ( ( ) => {
51+ exit ( process . exitCode ) ;
52+ } ) ;
5553}
0 commit comments