2
2
3
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 , lstatSync } 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
9
import { promisify } from 'util'
10
- import { join , resolve , basename } from 'path'
10
+ import { join , resolve , basename , extname } from 'path'
11
11
import stdin = require ( 'stdin' )
12
12
import { compile , Options } from './index'
13
13
@@ -40,54 +40,40 @@ async function main(argv: minimist.ParsedArgs) {
40
40
}
41
41
42
42
const argIn : string = argv . _ [ 0 ] || argv . input
43
- const argOut : string = argv . _ [ 1 ] || argv . output
43
+ const argOut : string | undefined = argv . _ [ 1 ] || argv . output
44
44
45
45
try {
46
- const files = await getFilesToProcess ( argIn , argOut , argv as Partial < Options > )
47
- await Promise . all ( files )
46
+ await processFiles ( argIn , argOut , argv as Partial < Options > )
48
47
} catch ( e ) {
49
48
console . error ( whiteBright . bgRedBright ( 'error' ) , e )
50
49
process . exit ( 1 )
51
50
}
52
51
}
53
52
54
- function getFilesToProcess ( argIn : string , argOut : string , argv : Partial < Options > ) : Promise < Promise < void > [ ] > {
55
- return new Promise ( async ( res , rej ) => {
56
- try {
57
- if ( isGlob ( argIn ) ) {
58
- const files = await glob ( join ( process . cwd ( ) , argIn ) )
59
-
60
- if ( files . length === 0 ) {
61
- rej ( 'No files match glob pattern' )
62
- }
63
-
64
- if ( argOut && ! existsSync ( argOut ) ) {
65
- await mkdirp ( argOut )
66
- }
67
-
68
- res ( files . map ( file => processFile ( file , { dir : argOut } , argv ) ) )
69
- return
70
- } else {
71
- res ( [ processFile ( argIn , { file : argOut } , argv ) ] )
72
- }
73
- } catch ( e ) {
74
- console . error ( whiteBright . bgRedBright ( 'error' ) , e )
75
- process . exit ( 1 )
76
- }
77
- } )
53
+ async function processFiles ( argIn : string , argOut : string | undefined , argv : Partial < Options > ) : Promise < void [ ] > {
54
+ const files = isGlob ( argIn ) ? await glob ( join ( process . cwd ( ) , argIn ) ) : [ argIn ]
55
+
56
+ if ( files . length === 0 ) {
57
+ throw ReferenceError (
58
+ `You passed a glob pattern "${ argIn } ", but there are no files that match that pattern in ${ process . cwd ( ) } `
59
+ )
60
+ }
61
+
62
+ if ( argOut && extname ( argOut ) === '' && ! existsSync ( argOut ) ) {
63
+ await mkdirp ( argOut )
64
+ }
65
+
66
+ return Promise . all ( files . map ( file => processFile ( file , argOut , argv ) ) )
78
67
}
79
68
80
- function processFile ( file : string , out : { dir ?: string ; file ?: string } , argv : Partial < Options > ) : Promise < void > {
81
- return new Promise ( async ( res , rej ) => {
82
- try {
83
- const schema = JSON . parse ( await readInput ( file ) )
84
- const ts = await compile ( schema , file , argv )
85
- await writeOutput ( ts , out . dir ? join ( process . cwd ( ) , out . dir , `${ basename ( file , '.json' ) } .d.ts` ) : out . file || '' )
86
- res ( )
87
- } catch ( err ) {
88
- rej ( err )
89
- }
90
- } )
69
+ async function processFile ( file : string , out : string | undefined , argv : Partial < Options > ) : Promise < void > {
70
+ out = out ? join ( process . cwd ( ) , out ) : ''
71
+ const schema = JSON . parse ( await readInput ( file ) )
72
+ const ts = await compile ( schema , file , argv )
73
+ return await writeOutput (
74
+ ts ,
75
+ existsSync ( out ) && lstatSync ( out ) . isDirectory ( ) ? join ( out , `${ basename ( file , '.json' ) } .d.ts` ) : out
76
+ )
91
77
}
92
78
93
79
function readInput ( argIn ?: string ) {
@@ -97,7 +83,7 @@ function readInput(argIn?: string) {
97
83
return readFile ( resolve ( process . cwd ( ) , argIn ) , 'utf-8' )
98
84
}
99
85
100
- function writeOutput ( ts : string , argOut : string ) : Promise < void > {
86
+ function writeOutput ( ts : string , argOut : string | undefined ) : Promise < void > {
101
87
if ( ! argOut ) {
102
88
try {
103
89
process . stdout . write ( ts )
0 commit comments