Skip to content

Commit 23be300

Browse files
feat: add file extension option
1 parent e0080af commit 23be300

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

bin/fastify-schema-to-typescript.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ function parsePrefix(value) {
1212
return value;
1313
}
1414

15+
function parseExtension(value) {
16+
if (!value.match(/^\./)) {
17+
console.error('File extension needs to start with a "."');
18+
process.exit(-1);
19+
}
20+
21+
return value;
22+
}
23+
1524
program
1625
.option(
1726
"-g, --glob <value>",
@@ -23,8 +32,14 @@ program
2332
"prefix to use before interfaces' name",
2433
parsePrefix,
2534
""
35+
)
36+
.option(
37+
"-e, --ext <value>",
38+
"file extension to use for generated files",
39+
parseExtension,
40+
".ts"
2641
);
2742

2843
program.parse(process.argv);
2944

30-
convert({ glob: program.glob, prefix: program.prefix });
45+
convert({ glob: program.glob, prefix: program.prefix, ext: program.ext });

src/index.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const defaultSchema = { type: "object", additionalProperties: false };
1010
export interface Options {
1111
glob: string;
1212
prefix: string;
13+
ext: string;
1314
}
1415

1516
export async function generateReplyInterfaces(
@@ -86,9 +87,16 @@ export { ${options.prefix}Handler, schema }\
8687
`;
8788
}
8889

89-
async function writeFile(parsedPath: path.ParsedPath, template: string) {
90+
async function writeFile(
91+
parsedPath: path.ParsedPath,
92+
template: string,
93+
options: Options
94+
) {
9095
const write = promisify(fs.writeFile);
91-
return write(path.join(parsedPath.dir, parsedPath.name + ".ts"), template);
96+
return write(
97+
path.join(parsedPath.dir, parsedPath.name + options.ext),
98+
template
99+
);
92100
}
93101

94102
export async function convert(options: Options) {
@@ -97,6 +105,6 @@ export async function convert(options: Options) {
97105
const parsedPath = path.parse(filePath);
98106
const schema = JSON.parse(fs.readFileSync(filePath, "utf-8"));
99107
const template = await generateInterfaces(parsedPath, schema, options);
100-
await writeFile(parsedPath, template);
108+
await writeFile(parsedPath, template, options);
101109
}
102110
}

0 commit comments

Comments
 (0)