@@ -45,27 +45,13 @@ type ${prefix}Reply = ${generatedReplyNames.join(" | ") || "{}"}
45
45
` . trim ( ) ;
46
46
}
47
47
48
- function importOrWriteSchema (
49
- parsedPath : path . ParsedPath ,
50
- schema : any ,
51
- options : Options ,
52
- isYaml : boolean
53
- ) {
54
- if ( isYaml ) {
55
- return `\
48
+ function writeSchema ( schema : any ) {
49
+ return `\
56
50
const schema = ${ JSON . stringify ( schema , null , 2 ) } \
57
51
` ;
58
- } else {
59
- return `import schema from './${ parsedPath . base } '` ;
60
- }
61
52
}
62
53
63
- async function generateInterfaces (
64
- parsedPath : path . ParsedPath ,
65
- schema : any ,
66
- options : Options ,
67
- isYaml = false
68
- ) {
54
+ async function generateInterfaces ( schema : any , options : Options ) {
69
55
return `\
70
56
/* tslint:disable */
71
57
/* eslint-disable */
@@ -76,7 +62,7 @@ async function generateInterfaces(
76
62
77
63
import { RouteHandler } from "${ options . module } "
78
64
79
- ${ importOrWriteSchema ( parsedPath , schema , options , isYaml ) }
65
+ ${ writeSchema ( schema ) }
80
66
81
67
${ await compile (
82
68
addDefaultValueToSchema ( schema . params || defaultSchema ) ,
@@ -128,19 +114,18 @@ export async function convert(options: Options) {
128
114
const filePaths = glob . sync ( options . glob ) ;
129
115
for ( const filePath of filePaths ) {
130
116
const parsedPath = path . parse ( filePath ) ;
131
- if ( parsedPath . ext === ".yaml" || parsedPath . ext === ".yml" ) {
132
- const schema = yaml . safeLoad ( fs . readFileSync ( filePath , "utf-8" ) ) ;
133
- const template = await generateInterfaces (
134
- parsedPath ,
135
- schema ,
136
- options ,
137
- true
138
- ) ;
139
- await writeFile ( parsedPath , template , options ) ;
140
- } else {
141
- const schema = JSON . parse ( fs . readFileSync ( filePath , "utf-8" ) ) ;
142
- const template = await generateInterfaces ( parsedPath , schema , options ) ;
143
- await writeFile ( parsedPath , template , options ) ;
117
+ try {
118
+ if ( parsedPath . ext === ".yaml" || parsedPath . ext === ".yml" ) {
119
+ const schema = yaml . safeLoad ( fs . readFileSync ( filePath , "utf-8" ) ) ;
120
+ const template = await generateInterfaces ( schema , options ) ;
121
+ await writeFile ( parsedPath , template , options ) ;
122
+ } else {
123
+ const schema = JSON . parse ( fs . readFileSync ( filePath , "utf-8" ) ) ;
124
+ const template = await generateInterfaces ( schema , options ) ;
125
+ await writeFile ( parsedPath , template , options ) ;
126
+ }
127
+ } catch ( err ) {
128
+ console . error ( `Failed to process file ${ parsedPath } ` ) ;
144
129
}
145
130
}
146
131
}
0 commit comments