22
33'use strict'
44
5- var async = require ( 'async' )
5+ var Promise = require ( 'bluebird' )
6+
67var chalk = require ( 'chalk' )
78var cmd = require ( 'commander' )
8- var debug = require ( 'debug' ) ( 'httpsnippet' )
9- var fs = require ( 'fs' )
10- var HTTPSnippet = require ( '../src' )
9+ var fs = Promise . promisifyAll ( require ( 'fs' ) )
10+ var HTTPSnippet = require ( '..' )
1111var path = require ( 'path' )
1212var pkg = require ( '../package.json' )
13+ var ValidationError = require ( 'har-validator/src/error' )
1314
1415cmd
1516 . version ( pkg . version )
@@ -32,94 +33,44 @@ if (cmd.output) {
3233 }
3334}
3435
35- async . waterfall ( [
36- function isFile ( next ) {
37- var iterator = function ( item , cb ) {
38- cb ( fs . statSync ( item ) . isFile ( ) )
39- }
36+ cmd . args . forEach ( function ( fileName ) {
37+ var file = path . basename ( fileName )
4038
41- async . filter ( cmd . args , iterator , function ( results ) {
42- next ( null , results )
39+ fs . readFileAsync ( fileName )
40+ . then ( JSON . parse )
41+ . then ( function ( data ) {
42+ return new HTTPSnippet ( data )
4343 } )
44- } ,
45-
46- function read ( files , next ) {
47- var iterator = function ( file , cb ) {
48- fs . readFile ( file , cb )
49- }
50-
51- async . map ( files , iterator , function ( err , results ) {
52- next ( err , files , results )
44+ . then ( function ( snippet ) {
45+ return snippet . convert ( cmd . target , cmd . client )
5346 } )
54- } ,
55-
56- function parse ( files , buffers , next ) {
57- var iterator = function ( buffer , cb ) {
58- try {
59- cb ( null , JSON . parse ( buffer ) )
60- } catch ( e ) {
61- debug ( 'failed to parse source json' )
62- cb ( 'failed to parse source json' , null )
47+ . then ( function ( output ) {
48+ // print
49+ if ( ! cmd . output ) {
50+ return console . log ( '%s %s > %s [%s] :\n%s' , chalk . green ( '✓' ) , chalk . cyan . bold ( file ) , chalk . yellow ( cmd . target ) , chalk . yellow ( cmd . client ? cmd . client : 'default' ) , output )
6351 }
64- }
65-
66- async . map ( buffers , iterator , function ( err , results ) {
67- next ( err , files , results )
68- } )
69- } ,
7052
71- function snippet ( files , sources , next ) {
72- var iterator = function ( source , cb ) {
73- var snippet
53+ // write to file
54+ var name = path . basename ( file , path . extname ( file ) )
7455
75- try {
76- snippet = new HTTPSnippet ( source )
77- } catch ( e ) {
78- debug ( e )
56+ var filename = path . format ( {
57+ dir : dir ,
58+ base : name + HTTPSnippet . extname ( cmd . target )
59+ } )
7960
80- return cb ( ! e [ 0 ] ? 'invalid input' : ( e [ 0 ] . field + ' ' + e [ 0 ] . message ) , null )
81- }
82-
83- cb ( null , snippet . convert ( cmd . target , cmd . client ) )
84- }
85-
86- async . map ( sources , iterator , function ( err , results ) {
87- next ( err , files , results )
61+ fs . writeFile ( filename , output + '\n' , function ( ) {
62+ console . log ( '%s %s > %s' , chalk . green ( '✓' ) , chalk . cyan . bold ( file ) , filename )
63+ } )
8864 } )
89- } ,
90-
91- function writeOutput ( files , snippets , next ) {
92- if ( cmd . output ) {
93- var iterator = function ( file ) {
94- var index = files . indexOf ( file )
95- var name = path . basename ( file , path . extname ( file ) )
96-
97- var filename = path . format ( {
98- dir : dir ,
99- base : name + HTTPSnippet . extname ( cmd . target )
100- } )
101-
102- fs . writeFile ( filename , snippets [ index ] + '\n' )
103- }
104-
105- async . each ( files , iterator )
106- }
107-
108- next ( null , files , snippets )
109- } ,
110-
111- function log ( files , snippets , next ) {
112- if ( ! cmd . output ) {
113- var iterator = function ( file ) {
114- var index = files . indexOf ( file )
115- console . log ( '%s:\n%s\n' , chalk . cyan . bold . underline ( file ) , snippets [ index ] )
116- }
117-
118- async . each ( files , iterator )
119- }
120- }
121- ] , function ( err , result ) {
122- if ( err ) {
123- console . log ( '%s: %s' , chalk . red . bold ( 'ERROR' ) , err )
124- }
125- } )
65+ . catch ( SyntaxError , function ( e ) {
66+ console . error ( '%s %s failed to read JSON: %s' , chalk . red ( '✖' ) , chalk . cyan . bold ( file ) , chalk . red ( e . message ) )
67+ } )
68+ . catch ( ValidationError , function ( e ) {
69+ e . errors . forEach ( function ( err ) {
70+ console . error ( '%s %s failed validation: (%s: %s) %s' , chalk . red ( '✖' ) , chalk . cyan . bold ( file ) , chalk . cyan . italic ( err . field ) , chalk . magenta . italic ( err . value ) , chalk . red ( err . message ) )
71+ } )
72+ } )
73+ . catch ( function ( e ) {
74+ console . error ( '%s %s fail: %s' , chalk . red ( '✖' ) , chalk . cyan . bold ( file ) , chalk . red ( e . message ) )
75+ } )
76+ } )
0 commit comments