Skip to content

Commit

Permalink
Merge pull request #5 from thomasthiebaud/support-yaml
Browse files Browse the repository at this point in the history
feat: add support for yaml schema
  • Loading branch information
thomasthiebaud authored Sep 2, 2020
2 parents 4efd8d7 + fb69fa9 commit 8087963
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
2 changes: 1 addition & 1 deletion bin/fastify-schema-to-typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ program
.option(
"-g, --glob <value>",
"glob matching JSON schema to convert",
"src/**/schema.json"
"src/**/schema.(json|yaml|yml)"
)
.option(
"-p, --prefix <value>",
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@semantic-release/release-notes-generator": "^9.0.1",
"@types/glob": "^7.1.3",
"@types/jest": "^26.0.10",
"@types/js-yaml": "^3.12.5",
"@types/node": "^14.6.0",
"fastify": "^3.2.1",
"husky": "^4.2.5",
Expand All @@ -53,6 +54,7 @@
"dependencies": {
"commander": "^6.0.0",
"glob": "^7.1.6",
"js-yaml": "^3.14.0",
"json-schema-to-typescript": "^9.1.1"
},
"peerDependencies": {
Expand Down
42 changes: 35 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import glob from "glob";
import path from "path";
import fs from "fs";
import glob from "glob";
import yaml from "js-yaml";
import { compile } from "json-schema-to-typescript";
import path from "path";
import { promisify } from "util";

const compileOptions = { bannerComment: "" };
Expand Down Expand Up @@ -37,10 +38,26 @@ type ${prefix}Reply = ${generatedReplyNames.join(" | ") || "{}"}
`.trim();
}

function importOrWriteSchema(
parsedPath: path.ParsedPath,
schema: any,
options: Options,
isYaml: boolean
) {
if (isYaml) {
return `\
const schema = ${schema}\
`;
} else {
return `import schema from './${parsedPath.base}'`;
}
}

async function generateInterfaces(
parsedPath: path.ParsedPath,
schema: any,
options: Options
options: Options,
isYaml = false
) {
return `\
/* tslint:disable */
Expand All @@ -52,7 +69,7 @@ async function generateInterfaces(
import { RouteHandler } from "${options.module}"
import schema from './${parsedPath.base}'
${importOrWriteSchema(parsedPath, schema, options, isYaml)}
${await compile(
schema.params || defaultSchema,
Expand Down Expand Up @@ -104,8 +121,19 @@ export async function convert(options: Options) {
const filePaths = glob.sync(options.glob);
for (const filePath of filePaths) {
const parsedPath = path.parse(filePath);
const schema = JSON.parse(fs.readFileSync(filePath, "utf-8"));
const template = await generateInterfaces(parsedPath, schema, options);
await writeFile(parsedPath, template, options);
if (parsedPath.ext === ".yaml" || parsedPath.ext === ".yml") {
const schema = yaml.safeLoad(fs.readFileSync(filePath, "utf-8"));
const template = await generateInterfaces(
parsedPath,
schema,
options,
true
);
await writeFile(parsedPath, template, options);
} else {
const schema = JSON.parse(fs.readFileSync(filePath, "utf-8"));
const template = await generateInterfaces(parsedPath, schema, options);
await writeFile(parsedPath, template, options);
}
}
}

0 comments on commit 8087963

Please sign in to comment.