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