Skip to content

Commit

Permalink
refactor: Moved some functions to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
unlight committed Nov 14, 2020
1 parent 28f8784 commit 8ebf3c2
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 87 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
"eslint:fix": "npm run eslint -- --fix",
"build": "sh Taskfile bundle",
"prisma:g": "node node_modules/@prisma/cli/build/index.js generate",
"prettier:format": "npx prettier src --write",
"regen": "rm -rf src/@generated && npm run prisma:g && npm run eslint:fix && npm run prettier:format",
"format": "npx prettier src --write",
"regen": "rm -rf src/@generated && npm run prisma:g && npm run eslint:fix && npm run format",
"example": "ts-node-dev src/example/main.ts",
"clean_cache": "rm -rf node_modules/.cache",
"commit": "cz"
Expand Down
4 changes: 2 additions & 2 deletions src/generate-class.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from 'assert';
import { ClassDeclaration, Node, ObjectLiteralExpression, SourceFile } from 'ts-morph';

import { generateGraphqlImport } from './generate-graphql-import';
import { generateImport } from './generate-import';
import { updateObjectProperty } from './utils';

export type DecoratorPropertyType = {
Expand All @@ -21,7 +21,7 @@ type GenerateClassArgs = {
export function generateClass(args: GenerateClassArgs) {
const { name, sourceFile, decorator } = args;

generateGraphqlImport({
generateImport({
name: decorator.name,
sourceFile,
moduleSpecifier: '@nestjs/graphql',
Expand Down
4 changes: 2 additions & 2 deletions src/generate-enum/generate-enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
SourceFile,
} from 'ts-morph';

import { generateGraphqlImport } from '../generate-graphql-import';
import { generateImport } from '../generate-import';
import { PrismaDMMF } from '../types';
import { updateObjectProperty } from '../utils';

Expand All @@ -19,7 +19,7 @@ type GenerateEnumArgs = {
export function generateEnum(args: GenerateEnumArgs) {
const { enumerable, sourceFile } = args;

generateGraphqlImport({
generateImport({
sourceFile,
name: 'registerEnumType',
moduleSpecifier: '@nestjs/graphql',
Expand Down
34 changes: 0 additions & 34 deletions src/generate-graphql-import.ts

This file was deleted.

33 changes: 33 additions & 0 deletions src/generate-project-import.ts → src/generate-import.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
import getRelativePath from 'get-relative-path';
import { SourceFile } from 'ts-morph';

type GenerateGraphqlImportArgs = {
name: string;
sourceFile: SourceFile;
moduleSpecifier: string | undefined;
};

export function generateImport(args: GenerateGraphqlImportArgs) {
const { moduleSpecifier, name, sourceFile } = args;
// console.log('name', name, moduleSpecifier);
if (!moduleSpecifier) {
return name;
}
let importDeclaration = sourceFile.getImportDeclaration(
(importDeclaration) =>
importDeclaration.getModuleSpecifier().getLiteralValue() === moduleSpecifier,
);
if (!importDeclaration) {
importDeclaration = sourceFile.addImportDeclaration({
moduleSpecifier,
});
}
let importSpecifier = importDeclaration
.getNamedImports()
.find((importSpecifier) => importSpecifier.getName() === name);

if (!importSpecifier) {
importSpecifier = importDeclaration.addNamedImport({
name,
});
}
return importSpecifier.getName();
}

type GenerateProjectImportArgs = {
name: string;
type: string;
Expand Down
61 changes: 25 additions & 36 deletions src/generate-input/generate-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { ClassDeclaration, SourceFile } from 'ts-morph';

import { DecoratorPropertyType, generateClass, generateClassProperty } from '../generate-class';
import { generateDecorator } from '../generate-decorator';
import { generateGraphqlImport } from '../generate-graphql-import';
import { generateProjectImport } from '../generate-project-import';
import { generateImport, generateProjectImport } from '../generate-import';
import { PrismaDMMF } from '../types';
import { toGraphqlImportType, toPropertyType } from '../utils';
import { getMatchingInputType } from './get-matching-input-type';
Expand All @@ -23,10 +22,12 @@ export function generateInput(args: GenerateInputArgs) {
decorator,
name: className,
});
generateGraphqlImport({ name: 'Field', sourceFile, moduleSpecifier: '@nestjs/graphql' });
generateImport({ name: 'Field', sourceFile, moduleSpecifier: '@nestjs/graphql' });
for (const field of inputType.fields) {
const inputType = getMatchingInputType(field.inputTypes);
const propertyType = getPropertyType(field.inputTypes);
const propertyTypes = field.inputTypes.map((t) =>
toPropertyType({ ...t, type: String(t.type) }),
);
// Additional import all objects
const inputTypes = field.inputTypes.filter(
(x) => ['object', 'enum'].includes(x.kind) && x.type !== className,
Expand All @@ -41,7 +42,7 @@ export function generateInput(args: GenerateInputArgs) {
}
generateInputProperty({
inputType,
propertyType,
propertyTypes,
classDeclaration,
sourceFile,
projectFilePath,
Expand All @@ -58,19 +59,31 @@ type GenerateInputPropertyArgs = {
projectFilePath(data: { name: string; type: string }): string;
className: string;
classDeclaration: ClassDeclaration;
propertyType: string;
propertyTypes: string[];
};

function generateInputProperty(args: GenerateInputPropertyArgs) {
const { inputType, sourceFile, className, classDeclaration, projectFilePath } = args;
const {
inputType,
sourceFile,
className,
classDeclaration,
propertyTypes,
projectFilePath,
} = args;
const propertyDeclaration = generateClassProperty({
name: args.name,
type: args.propertyType,
type: propertyTypes.join(' | '),
classDeclaration,
isReadOnly: false,
isRequired: false,
});
let fieldType = getFieldType(inputType);
// TODO: Do import for custom typescript type
// if (propertyTypes.includes('Decimal')) {
// generateImport({sourceFile, name: 'Decimal', moduleSpecifier: 'decimal.js'});
// }
// Get field type
let fieldType = String(inputType.type);
if (inputType.kind === 'scalar') {
const importType = toGraphqlImportType({
kind: inputType.kind,
Expand All @@ -79,21 +92,14 @@ function generateInputProperty(args: GenerateInputPropertyArgs) {
});
// Override return field type value
fieldType = importType.name;
generateGraphqlImport({
generateImport({
sourceFile,
...importType,
});
} else if (inputType.kind === 'object' && inputType.type !== className) {
generateProjectImport({
name: fieldType,
type: 'input',
sourceFile,
projectFilePath,
});
} else if (inputType.kind === 'enum') {
} else if (['object', 'enum'].includes(inputType.kind) && inputType.type !== className) {
generateProjectImport({
name: fieldType,
type: 'enum',
type: inputType.kind === 'object' ? 'input' : inputType.kind,
sourceFile,
projectFilePath,
});
Expand All @@ -104,20 +110,3 @@ function generateInputProperty(args: GenerateInputPropertyArgs) {
nullable: true,
});
}

function getPropertyType(inputTypes: PrismaDMMF.SchemaArgInputType[]): string {
const types: string[] = inputTypes.map((inputType) => {
return toPropertyType({ ...inputType, type: String(inputType.type) });
});
types.sort((a, b) => {
if (b === 'null') {
return -2;
}
return 0;
});
return types.join(' | ');
}

function getFieldType(inputType: PrismaDMMF.SchemaArgInputType) {
return String(inputType.type);
}
4 changes: 2 additions & 2 deletions src/generate-model/generate-model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SourceFile } from 'ts-morph';

import { generateClass } from '../generate-class';
import { generateGraphqlImport } from '../generate-graphql-import';
import { generateImport } from '../generate-import';
import { generateProperty } from '../generate-property';
import { PrismaDMMF } from '../types';

Expand All @@ -24,7 +24,7 @@ export function generateModel(args: GenerateModelArgs) {
sourceFile,
name: model.name,
});
generateGraphqlImport({ name: 'Field', sourceFile, moduleSpecifier: '@nestjs/graphql' });
generateImport({ name: 'Field', sourceFile, moduleSpecifier: '@nestjs/graphql' });
model.fields.forEach((field) => {
generateProperty({
field,
Expand Down
4 changes: 2 additions & 2 deletions src/generate-object.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SourceFile } from 'ts-morph';

import { generateClass } from './generate-class';
import { generateGraphqlImport } from './generate-graphql-import';
import { generateImport } from './generate-import';
import { generateProperty, Model } from './generate-property';

type GenerateObjectArgs = {
Expand All @@ -23,7 +23,7 @@ export function generateObject(args: GenerateObjectArgs) {
sourceFile,
name: model.name,
});
generateGraphqlImport({ name: 'Field', sourceFile, moduleSpecifier: '@nestjs/graphql' });
generateImport({ name: 'Field', sourceFile, moduleSpecifier: '@nestjs/graphql' });
model.fields.forEach((field) => {
generateProperty({
field,
Expand Down
5 changes: 2 additions & 3 deletions src/generate-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { ClassDeclaration, SourceFile } from 'ts-morph';

import { generateClassProperty } from './generate-class';
import { generateDecorator } from './generate-decorator';
import { generateGraphqlImport } from './generate-graphql-import';
import { generateProjectImport } from './generate-project-import';
import { generateImport, generateProjectImport } from './generate-import';
import { toGraphqlImportType, toPropertyType } from './utils';

export type Field = {
Expand Down Expand Up @@ -39,7 +38,7 @@ export function generateProperty(args: GeneratePropertyArgs) {
const propertyType = toPropertyType(field);
let fieldType = field.type;
if (field.isId || field.kind === 'scalar') {
fieldType = generateGraphqlImport({
fieldType = generateImport({
sourceFile,
...toGraphqlImportType({ ...field, isId: Boolean(field.isId) }),
});
Expand Down
10 changes: 7 additions & 3 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ import { existsSync, promises as fs } from 'fs';
import path from 'path';
import { Project, QuoteKind, SourceFile } from 'ts-morph';

import { featureName } from './feature-name';
import { generateArgs } from './generate-args';
import { generateEnum } from './generate-enum';
import { generateFileName } from './generate-file-name';
import { generateInput } from './generate-input';
import { generateModel } from './generate-model';
import { generateObject } from './generate-object';
import { mutateFilters } from './mutate-filters';
import { PrismaDMMF } from './types';
import { getOutputTypeName, schemaFieldToArgument, schemaOutputToInput } from './utils';
import {
featureName,
getOutputTypeName,
schemaFieldToArgument,
schemaOutputToInput,
} from './utils';
import { generateFileName } from './utils/generate-file-name';

type GenerateArgs = GeneratorOptions & {
prismaClientDmmf?: PrismaDMMF.Document;
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 3 additions & 1 deletion src/utils.ts → src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export { featureName } from './feature-name';

import { ObjectLiteralExpression, PropertyAssignment, StructureKind } from 'ts-morph';

import { PrismaDMMF } from './types';
import { PrismaDMMF } from '../types';

type ToGraphqlImportTypeArgs = {
isId: boolean;
Expand Down

0 comments on commit 8ebf3c2

Please sign in to comment.