Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code-gen: flatten output #460

Merged
merged 1 commit into from
Nov 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions packages/code-gen/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import {
addToData,
hoistNamedItems,
} from "./generate.js";
import { codeGenValidators, validatorSetError } from "./generated/index.js";
import {
validateCodeGenStructure,
validateCodeGenType,
validatorSetError,
} from "./generated/index.js";
import { generate } from "./generator/index.js";
import { getInternalRoutes } from "./generator/router/index.js";
import { lowerCaseFirst } from "./utils.js";
Expand Down Expand Up @@ -98,7 +102,7 @@ export class App {
* @returns {App}
*/
static new(options = {}) {
if (!isNil(codeGenValidators?.type)) {
if (!isNil(validateCodeGenType)) {
validatorSetError(AppError.validationError);
}
return new App(options);
Expand Down Expand Up @@ -163,8 +167,8 @@ export class App {
* @returns {this}
*/
addRaw(obj) {
if (!isNil(codeGenValidators?.type)) {
const { data, errors } = codeGenValidators.type(obj);
if (!isNil(validateCodeGenType)) {
const { data, errors } = validateCodeGenType(obj);
if (errors) {
this.logger.error(AppError.format(errors[0]));
process.exit(1);
Expand All @@ -184,8 +188,8 @@ export class App {
* @returns {this}
*/
extend(data) {
if (!isNil(codeGenValidators?.type)) {
const { data: value, errors } = codeGenValidators.structure(data);
if (!isNil(validateCodeGenType)) {
const { data: value, errors } = validateCodeGenStructure(data);
if (errors) {
this.logger.error(AppError.format(errors[0]));
process.exit(1);
Expand Down Expand Up @@ -300,8 +304,8 @@ export class App {
addGroupsToGeneratorInput(generatorInput, dataCopy, opts.enabledGroups);

// validators may not be present, fallback to just stringify
if (!isNil(codeGenValidators.structure)) {
const { errors } = codeGenValidators.structure(generatorInput);
if (!isNil(validateCodeGenStructure)) {
const { errors } = validateCodeGenStructure(generatorInput);
if (errors) {
this.logger.error(AppError.format(errors[0]));
process.exit(1);
Expand Down
26 changes: 23 additions & 3 deletions packages/code-gen/src/generated/index.js

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import { CancelToken, AxiosInstance } from "axios";
{{ } }}

{{ if (options.isNodeServer) { }}
{{ for (const group of Object.keys(structure)) { }}
import * as {{= group }}Validators from "./{{= group }}/validators{{= importExtension }}";
{{ } }}
import * as validators from "./validators{{= importExtension }}";
import { AppError, streamToBuffer } from "@lbu/stdlib";
((newline))
function handleError(e, group, name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ options = {},
return response.data;
{{ } else { }}
{{ if (item.response) { }}
{{ const { name, group } = item.response.reference ?? {}; }}
{{ if (!isNil(name)) { }}
{{ const { uniqueName } = item.response.reference ?? {}; }}
{{ if (!isNil(uniqueName)) { }}
{{ /* Validates response, but does not use the validated result. This implies that dates will be ISO strings, and objects don't have a 'null' prototype. */ }}
{{= group }}Validators.{{= name }}(response.data);
validators.validate{{= uniqueName }}(response.data);
{{ } }}
{{ } }}
return response.data;
Expand Down
8 changes: 4 additions & 4 deletions packages/code-gen/src/generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import {
import { createWhereTypes } from "./sql/where-type.js";
import {
addRootExportsForStructureFiles,
generateStructureFiles,
generateStructureFile,
} from "./structure.js";
import {
generateTypeFile,
getTypeNameForType,
setupMemoizedTypes,
} from "./types.js";
import { generateValidatorFiles } from "./validator.js";
import { generateValidatorFile } from "./validator.js";

/**
*
Expand Down Expand Up @@ -70,7 +70,7 @@ export async function generate(logger, options, structure) {
// The raw structure that is used to generate all the files.
// This contains all information needed to generate again, even if different options
// are needed.
generateStructureFiles(context);
generateStructureFile(context);
addRootExportsForStructureFiles(context);

exitOnErrorsOrReturn(context);
Expand All @@ -94,7 +94,7 @@ export async function generate(logger, options, structure) {
exitOnErrorsOrReturn(context);

if (context.options.enabledGenerators.indexOf("validator") !== -1) {
generateValidatorFiles(context);
generateValidatorFile(context);
exitOnErrorsOrReturn(context);
}
if (context.options.enabledGenerators.indexOf("router") !== -1) {
Expand Down
Loading