Skip to content

Commit bff78ca

Browse files
fix: write schemas both for yaml and json files
1 parent 97593b6 commit bff78ca

File tree

1 file changed

+16
-31
lines changed

1 file changed

+16
-31
lines changed

src/index.ts

+16-31
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,13 @@ type ${prefix}Reply = ${generatedReplyNames.join(" | ") || "{}"}
4545
`.trim();
4646
}
4747

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 `\
5650
const schema = ${JSON.stringify(schema, null, 2)}\
5751
`;
58-
} else {
59-
return `import schema from './${parsedPath.base}'`;
60-
}
6152
}
6253

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) {
6955
return `\
7056
/* tslint:disable */
7157
/* eslint-disable */
@@ -76,7 +62,7 @@ async function generateInterfaces(
7662
7763
import { RouteHandler } from "${options.module}"
7864
79-
${importOrWriteSchema(parsedPath, schema, options, isYaml)}
65+
${writeSchema(schema)}
8066
8167
${await compile(
8268
addDefaultValueToSchema(schema.params || defaultSchema),
@@ -128,19 +114,18 @@ export async function convert(options: Options) {
128114
const filePaths = glob.sync(options.glob);
129115
for (const filePath of filePaths) {
130116
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}`);
144129
}
145130
}
146131
}

0 commit comments

Comments
 (0)