1
1
#!/usr/bin/env node
2
2
3
- import { whiteBright } from 'cli-color'
4
- import { JSONSchema4 } from 'json-schema'
3
+ import { whiteBright } from 'cli-color'
5
4
import minimist = require ( 'minimist' )
6
- import { readFile , writeFile } from 'mz/fs'
7
- import { resolve } from 'path'
5
+ import { readFile , writeFile , existsSync } from 'mz/fs'
6
+ import * as _mkdirp from 'mkdirp'
7
+ import * as _glob from 'glob'
8
+ import isGlob = require ( 'is-glob' )
9
+ import { promisify } from 'util'
10
+ import { join , resolve , basename } from 'path'
8
11
import stdin = require ( 'stdin' )
9
12
import { compile , Options } from './index'
10
13
11
- main (
12
- minimist ( process . argv . slice ( 2 ) , {
13
- alias : {
14
- help : [ 'h' ] ,
15
- input : [ 'i' ] ,
16
- output : [ 'o' ]
17
- }
14
+ // Promisify mkdirp
15
+ const mkdirp = ( path : string ) => new Promise ( ( res , rej ) => {
16
+ _mkdirp ( path , ( err , made ) => {
17
+ if ( err ) rej ( err )
18
+ else res ( made === null ? undefined : made )
18
19
} )
19
- )
20
+ } )
21
+
22
+ const glob = promisify ( _glob )
23
+
24
+ main ( minimist ( process . argv . slice ( 2 ) , {
25
+ alias : {
26
+ help : [ 'h' ] ,
27
+ input : [ 'i' ] ,
28
+ output : [ 'o' ] ,
29
+ recursive : [ 'r' ]
30
+ }
31
+ } ) )
20
32
21
33
async function main ( argv : minimist . ParsedArgs ) {
22
34
if ( argv . help ) {
@@ -28,15 +40,56 @@ async function main(argv: minimist.ParsedArgs) {
28
40
const argOut : string = argv . _ [ 1 ] || argv . output
29
41
30
42
try {
31
- const schema : JSONSchema4 = JSON . parse ( await readInput ( argIn ) )
32
- const ts = await compile ( schema , argIn , argv as Partial < Options > )
33
- await writeOutput ( ts , argOut )
43
+ let files = await getFilesToProcess ( argIn , argOut , argv as Partial < Options > )
44
+ await Promise . all ( files )
34
45
} catch ( e ) {
35
46
console . error ( whiteBright . bgRedBright ( 'error' ) , e )
36
47
process . exit ( 1 )
37
48
}
38
49
}
39
50
51
+ function getFilesToProcess ( argIn : string , argOut : string , argv : Partial < Options > ) : Promise < Promise < void > [ ] > {
52
+ return new Promise ( async ( res , rej ) => {
53
+ try {
54
+ if ( isGlob ( argIn ) ) {
55
+ let files = await glob ( join ( process . cwd ( ) , argIn ) )
56
+
57
+ if ( files . length === 0 ) {
58
+ rej ( 'No files match glob pattern' )
59
+ }
60
+
61
+ if ( argOut && ! existsSync ( argOut ) ) {
62
+ await mkdirp ( argOut )
63
+ }
64
+
65
+ res ( files . map ( file => processFile ( file , { dir : argOut } , argv ) ) )
66
+ return
67
+ } else {
68
+ res ( [ processFile ( argIn , { file : argOut } , argv ) ] )
69
+ }
70
+ } catch ( e ) {
71
+ console . error ( whiteBright . bgRedBright ( 'error' ) , e )
72
+ process . exit ( 1 )
73
+ }
74
+ } )
75
+ }
76
+
77
+ function processFile ( file : string , out : { dir ? : string , file ?: string } , argv : Partial < Options > ) : Promise < void > {
78
+ return new Promise ( async ( res , rej ) => {
79
+ try {
80
+ const schema = JSON . parse ( await readInput ( file ) )
81
+ const ts = await compile ( schema , file , argv )
82
+ await writeOutput (
83
+ ts ,
84
+ out . dir ? join ( process . cwd ( ) , out . dir , `${ basename ( file , '.json' ) } .d.ts` ) : out . file || ''
85
+ )
86
+ res ( )
87
+ } catch ( err ) {
88
+ rej ( err )
89
+ }
90
+ } )
91
+ }
92
+
40
93
function readInput ( argIn ? : string ) {
41
94
if ( ! argIn ) {
42
95
return new Promise ( stdin )
0 commit comments