22
33'use strict'
44
5- var chalk = require ( 'chalk' )
6- var cmd = require ( 'commander' )
7- var fs = require ( 'fs' )
8- var readFile = require ( 'fs-readfile-promise' )
9- var writeFile = require ( 'fs-writefile-promise' )
10- var HTTPSnippet = require ( '..' )
11- var path = require ( 'path' )
12- var pkg = require ( '../package.json' )
5+ const chalk = require ( 'chalk' )
6+ const cmd = require ( 'commander' )
7+ const fs = require ( 'fs' )
8+ const readFile = require ( 'fs-readfile-promise' )
9+ const writeFile = require ( 'fs-writefile-promise' )
10+ const HTTPSnippet = require ( '..' )
11+ const path = require ( 'path' )
12+ const pkg = require ( '../package.json' )
1313
1414cmd
1515 . version ( pkg . version )
1616 . usage ( '[options] <files ...>' )
1717 . option ( '-t, --target <target>' , 'target output' )
1818 . option ( '-c, --client [client]' , 'target client library' )
1919 . option ( '-o, --output <directory>' , 'write output to directory' )
20+ . option ( '-x, --extra [{"optionKey": "optionValue"}]' , 'provide extra options for the target/client' )
2021 . parse ( process . argv )
2122
2223if ( ! cmd . args . length || ! cmd . target ) {
2324 cmd . help ( )
2425}
2526
27+ let extraOptions
28+ if ( cmd . extra ) {
29+ try {
30+ extraOptions = JSON . parse ( cmd . extra )
31+ } catch ( e ) {
32+ console . error ( '%s failed to parse options %s (should be JSON)' , chalk . red ( '✖' ) , chalk . cyan . bold ( cmd . extra ) )
33+ process . exit ( )
34+ }
35+ }
36+
37+ let dir
2638if ( cmd . output ) {
27- var dir = path . resolve ( cmd . output )
39+ dir = path . resolve ( cmd . output )
2840
2941 if ( ! fs . existsSync ( dir ) ) {
3042 fs . mkdirSync ( dir )
3143 }
3244}
3345
3446cmd . args . forEach ( function ( fileName ) {
35- var file = path . basename ( fileName )
47+ const file = path . basename ( fileName )
3648
3749 readFile ( fileName )
3850 . then ( JSON . parse )
@@ -52,19 +64,24 @@ cmd.args.forEach(function (fileName) {
5264 } )
5365
5466 . then ( function ( snippet ) {
55- return snippet . convert ( cmd . target , cmd . client )
67+ return snippet . convert ( cmd . target , cmd . client , extraOptions )
5668 } )
5769
5870 . then ( function ( output ) {
71+ if ( ! output ) {
72+ const targetNames = HTTPSnippet . availableTargets ( ) . map ( function ( t ) { return t . key } ) . join ( ', ' )
73+ return console . error ( '%s %s is not a valid target. Valid targets: %s' , chalk . red ( '✖' ) , chalk . red ( cmd . target ) , chalk . cyan ( targetNames ) )
74+ }
75+
5976 // print
6077 if ( ! cmd . output ) {
6178 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 )
6279 }
6380
6481 // write to file
65- var name = path . basename ( file , path . extname ( file ) )
82+ const name = path . basename ( file , path . extname ( file ) )
6683
67- var filename = path . format ( {
84+ const filename = path . format ( {
6885 dir : dir ,
6986 base : name + HTTPSnippet . extname ( cmd . target )
7087 } )
0 commit comments