22
33'use strict'
44
5- var Promise = require ( 'bluebird' )
6-
75var chalk = require ( 'chalk' )
86var cmd = require ( 'commander' )
9- var fs = Promise . promisifyAll ( require ( 'fs' ) )
7+ var fs = require ( 'fs' )
8+ var readFile = require ( 'fs-readfile-promise' )
9+ var writeFile = require ( 'fs-writefile-promise' )
1010var HTTPSnippet = require ( '..' )
1111var path = require ( 'path' )
1212var pkg = require ( '../package.json' )
13- var ValidationError = require ( 'har-validator/lib/error' )
1413
1514cmd
1615 . version ( pkg . version )
@@ -35,14 +34,27 @@ if (cmd.output) {
3534cmd . args . forEach ( function ( fileName ) {
3635 var file = path . basename ( fileName )
3736
38- fs . readFileAsync ( fileName )
37+ readFile ( fileName )
3938 . then ( JSON . parse )
39+
40+ . catch ( function ( e ) {
41+ console . error ( '%s %s failed to read JSON: %s' , chalk . red ( '✖' ) , chalk . cyan . bold ( file ) , chalk . red ( e . message ) )
42+ } )
43+
4044 . then ( function ( data ) {
4145 return new HTTPSnippet ( data )
4246 } )
47+
48+ . catch ( function ( e ) {
49+ e . errors . forEach ( function ( err ) {
50+ 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 ) )
51+ } )
52+ } )
53+
4354 . then ( function ( snippet ) {
4455 return snippet . convert ( cmd . target , cmd . client )
4556 } )
57+
4658 . then ( function ( output ) {
4759 // print
4860 if ( ! cmd . output ) {
@@ -57,18 +69,11 @@ cmd.args.forEach(function (fileName) {
5769 base : name + HTTPSnippet . extname ( cmd . target )
5870 } )
5971
60- fs . writeFile ( filename , output + '\n' , function ( ) {
72+ return writeFile ( filename , output + '\n' , function ( ) {
6173 console . log ( '%s %s > %s' , chalk . green ( '✓' ) , chalk . cyan . bold ( file ) , filename )
6274 } )
6375 } )
64- . catch ( SyntaxError , function ( e ) {
65- console . error ( '%s %s failed to read JSON: %s' , chalk . red ( '✖' ) , chalk . cyan . bold ( file ) , chalk . red ( e . message ) )
66- } )
67- . catch ( ValidationError , function ( e ) {
68- e . errors . forEach ( function ( err ) {
69- 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 ) )
70- } )
71- } )
76+
7277 . catch ( function ( e ) {
7378 console . error ( '%s %s fail: %s' , chalk . red ( '✖' ) , chalk . cyan . bold ( file ) , chalk . red ( e . message ) )
7479 } )
0 commit comments