Newer
Older
ServerGenerator / src / app.ts
import { Collection } from "./MySchema";
import { join } from "path";
import * as fs from "fs";
import { generateModel } from "./ModelGenerator";

interface Options {
    typeScriptOutputFolder: string;
    openapiOutput: string;
}
export interface Description {
    collections: Collection[];
}
export interface FileWrite {
    location: string;
    content: string;
}

function describeTestOutput(): Description {
    return {
        collections: [
            {
                name: "Characters"
                , entities: {
                    name: "Character",
                    properties: [
                        { key: "name", type: "string", isNullable: false },
                        { key: "backstory", type: "string", isNullable: false }
                    ]
                }
            }
        ]
    }
}

function generate(options: Options, description: Description): FileWrite[] {
    // generate model files
    // generate data access files
    // generate middleware file
    // generate openapi file
    //  -> see https://swagger.io/docs/specification/describing-responses/
    const writes = generateModel(description.collections);
    writes.forEach(w => {w.location = join(options.typeScriptOutputFolder, w.location)})
    return writes;
}

function execute() {
    const example = describeTestOutput();
    const options: Options = {
        openapiOutput: join(__dirname, "..", "src", "TestOutput", "openapi.json"),
        typeScriptOutputFolder: join(__dirname, "..", "src", "TestOutput")
    };
    const writes = generate(options, example);
    for (const write of writes) {
        fs.writeFileSync(write.location, write.content);
        console.log(`Written file '${write.location}'`)
    }
}

execute();