Skip to content

Commit

Permalink
🐞add some logs to creating declaration of schema
Browse files Browse the repository at this point in the history
  • Loading branch information
etefaghian committed May 25, 2021
1 parent da1c155 commit c95df5c
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 82 deletions.
10 changes: 10 additions & 0 deletions declarations/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getSchemaDeclarations } from "./schema/mod.ts";
import { getRequestDeclarations } from "./request/mod.ts";

export const generateDeclarations = async (
generateSchema: boolean,
generateRequest: boolean
) => {
//generateRequest && (await getRequestDeclarations());
generateSchema && (await getSchemaDeclarations());
};
5 changes: 2 additions & 3 deletions declarations/request/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Project, log } from "../../deps.ts";
import { ensureDir } from "../../deps.ts";
import { Project, log, emptyDir } from "../../deps.ts";
import {
jsonObToTsType,
convertFvObToTsOb,
Expand All @@ -16,7 +15,7 @@ export const getRequestDeclarations = async (dirPath?: string) => {

const __dirname = dirPath || Deno.cwd();

await ensureDir("declarations/request");
await emptyDir("declarations/request");

project.addSourceFilesAtPaths(`${__dirname}/**/*.ts`);

Expand Down
101 changes: 54 additions & 47 deletions declarations/schema/mod.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,69 @@
import { Project, log } from "../../deps.ts";
import { ensureDir } from "../../deps.ts";
import { Project, log, emptyDir } from "../../deps.ts";
import { rgb24 } from "https://deno.land/[email protected]/fmt/colors.ts";
import { denoResolutionHost } from "../utils/mod.ts";
import { addFunQLInterfaceToSourceFile } from "./utils/addInterfaceToSrcFile.ts";

export const getSchemaDeclarations = async (dirPath?: string) => {
log.info("Generating of declarations of schema is started");
const project = new Project({
resolutionHost: denoResolutionHost,
});

const __dirname = dirPath || Deno.cwd();
await ensureDir("declarations/schema");
project.addSourceFilesAtPaths(`${__dirname}/**/*.ts`);
//handle differentiate between schema and schemas
const dir =
project.getDirectory(`${__dirname}/schema`) ||
project.getDirectory(`${__dirname}/schemas`);

const createdSourceFile = project.createSourceFile(
`${__dirname}/declarations/schema/schema.ts`,
undefined,
{
overwrite: true,
}
);

const sourceFiles = dir?.getSourceFiles();

//get all of interfaces
sourceFiles?.map((sourceFile) => {
const selectedInterfaces = sourceFile
.getInterfaces()
.filter(
(inter) =>
inter.getName().startsWith("Pu") ||
inter.getName().startsWith("Em") ||
inter.getName().startsWith("In") ||
inter.getName().startsWith("OutRel") ||
inter.getName().startsWith("I")
);
try {
log.info("Generating of declarations of schema is started");
const project = new Project({
resolutionHost: denoResolutionHost,
});

const __dirname = dirPath || Deno.cwd();
await emptyDir("declarations/schema");
project.addSourceFilesAtPaths(`${__dirname}/**/*.ts`);
//handle differentiate between schema and schemas
const dir =
project.getDirectory(`${__dirname}/schema`) ||
project.getDirectory(`${__dirname}/schemas`);

selectedInterfaces.map((inter) =>
addFunQLInterfaceToSourceFile(inter, createdSourceFile)
const createdSourceFile = project.createSourceFile(
`${__dirname}/declarations/schema/schema.ts`,
undefined,
{
overwrite: true,
}
);
});

//console.log(newSourceFile.getText());
await createdSourceFile.save();
const sourceFiles = dir!.getSourceFiles();

//get all of interfaces
sourceFiles?.map((sourceFile) => {
const selectedInterfaces = sourceFile
.getInterfaces()
.filter(
(inter) =>
inter.getName().startsWith("Pu") ||
inter.getName().startsWith("Em") ||
inter.getName().startsWith("In") ||
inter.getName().startsWith("OutRel") ||
inter.getName().startsWith("I")
);

selectedInterfaces.map((inter) =>
addFunQLInterfaceToSourceFile(inter, createdSourceFile)
);
});

//console.log(newSourceFile.getText());
await createdSourceFile.save();

log.info(`creating of declaration files for schema was successful
log.info(`creating of declaration files for schema was successful
${rgb24(
`
-------------------------------------------------------------
| schema: file://${__dirname}/declarations/schema/schema.ts
-------------------------------------------------------------
`,
-------------------------------------------------------------
| schema: file://${__dirname}/declarations/schema/schema.ts
-------------------------------------------------------------
`,
0xadfc03
)}
`);
} catch (error) {
log.error(
`creating of schema was unsuccessful please review your project
${error}
`
);
}
};
3 changes: 2 additions & 1 deletion declarations/schema/utils/addInterfaceToSrcFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function addFunQLInterfaceToSourceFile(
) {
//checks interface name is duplicate or not and also is Date or not
if (
//ignore date type
//ignore date type or similar
isInternalType(myInterface.getName()) ||
//when interface was inserted to source file
myInterface.getName().startsWith("FunQl")
Expand All @@ -32,6 +32,7 @@ export function addFunQLInterfaceToSourceFile(
const createdInterface = createdSourceFile.addInterface({
name: myInterface.getName(),
isExported: true,
docs: myInterface.getJsDocs().map((doc) => doc.getStructure()),
});

//finds all props that include in body of interface or specified with inheritance
Expand Down
47 changes: 34 additions & 13 deletions declarations/schema/utils/handlePropType.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { PropertySignature, SourceFile, SyntaxKind } from "../../../deps.ts";
import {
PropertySignature,
SourceFile,
SyntaxKind,
log,
} from "../../../deps.ts";
import { getInterfaceFromType, getEnumFromType } from "./ts-morph/mod.ts";
import { addFunQLInterfaceToSourceFile } from "./addInterfaceToSrcFile.ts";
import { addFunQLEnumToSourceFile } from "./mod.ts";
Expand All @@ -8,25 +13,41 @@ import { addFunQLEnumToSourceFile } from "./mod.ts";
* find and add associated declaration to this type to source file
* @param type
* @param createdSourceFile
* @todo handle type alias type
*/
export function handlePropType(
prop: PropertySignature,
createdSourceFile: SourceFile
) {
const typeReferences = prop.getDescendantsOfKind(SyntaxKind.TypeReference);
typeReferences.map((reference) => {
//get type of references
const typeOfReference = reference.getType();

//if type is interface we should find interface and process it again
if (typeOfReference.isInterface()) {
const foundedInterface = getInterfaceFromType(typeOfReference);
addFunQLInterfaceToSourceFile(foundedInterface, createdSourceFile);
}
//if type is enum we should find enum and add it to source file
if (typeOfReference.isEnum()) {
const foundedEnum = getEnumFromType(typeOfReference);
addFunQLEnumToSourceFile(foundedEnum, createdSourceFile);
try {
//get type of references
const typeOfReference = reference.getType();
//if type is interface we should find interface and process it again
if (typeOfReference.isInterface()) {
const foundedInterface = getInterfaceFromType(typeOfReference);
addFunQLInterfaceToSourceFile(foundedInterface, createdSourceFile);
return;
}
//if type is enum we should find enum and add it to source file
else if (typeOfReference.isEnum()) {
const foundedEnum = getEnumFromType(typeOfReference);
addFunQLEnumToSourceFile(foundedEnum, createdSourceFile);
return;
} else {
throw Error(
"please use only interface and enum types for your type referencing"
);
}
} catch (error) {
log.error(
`we have some problem in finding type: ${reference.getText()} in file: ${reference
.getSourceFile()
.getBaseName()}
${error}
`
);
}
});
}
28 changes: 19 additions & 9 deletions declarations/schema/utils/ts-morph/changeNameAndRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
InterfaceDeclaration,
EnumDeclaration,
TypeAliasDeclaration,
log,
} from "../../../../deps.ts";

/**
Expand All @@ -12,14 +13,23 @@ import {
export function changeNameAndItsRefs(
node: InterfaceDeclaration | EnumDeclaration | TypeAliasDeclaration
) {
//we use split for remove some postfixes for example authority.embedded.ts
const fileName = node
.getSourceFile()
.getBaseNameWithoutExtension()
.split(".")[0];
//generate new name
const newName = `FunQl_${node.getName()}_${fileName}`;
try {
//we use split for remove some postfixes for example authority.embedded.ts
const fileName = node
.getSourceFile()
.getBaseNameWithoutExtension()
.split(".")[0];
//generate new name
const newName = `FunQl_${node.getName()}_${fileName}`;

//rename node and its refs
node.rename(newName, { renameInStrings: true, usePrefixAndSuffixText: true });
//rename node and its refs
node.rename(newName, {
renameInStrings: true,
usePrefixAndSuffixText: true,
});
} catch (error) {
log.warning(
`we have some problem in renaming declaration: ${node.getName()}. please check declaration and file name again `
);
}
}
25 changes: 18 additions & 7 deletions declarations/schema/utils/ts-morph/findInterfaceProps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { InterfaceDeclaration, PropertySignature } from "../../../../deps.ts";
import {
InterfaceDeclaration,
PropertySignature,
log,
} from "../../../../deps.ts";

/**
* @function
Expand All @@ -7,10 +11,17 @@ import { InterfaceDeclaration, PropertySignature } from "../../../../deps.ts";
export function findAllPropsOfInterface(
myInterface: InterfaceDeclaration
): PropertySignature[] {
return <PropertySignature[]>myInterface
.getSymbolOrThrow()
.getDeclarations()[0]
.getType()
.getProperties()
.map((prop) => prop.getDeclarations()[0]);
try {
return <PropertySignature[]>myInterface
.getSymbolOrThrow()
.getDeclarations()[0]
.getType()
.getProperties()
.map((prop) => prop.getDeclarations()[0]);
} catch (error) {
log.error(
`we can not find props of interface: ${myInterface.getName()}. please check it`
);
return [];
}
}
4 changes: 2 additions & 2 deletions funql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "./config/mod.ts";
import { upgrade } from "./cli/mod.ts";
import { log } from "./deps.ts";
import { getSchemaDeclarations } from "./declarations/schema/mod.ts";
import { generateDeclarations } from "./declarations/mod.ts";

export interface CommandArgs {
init?: boolean | string;
Expand All @@ -23,7 +24,6 @@ const createProject = async (init: string | boolean) => {
};

args.init && (await createProject(args.init));
args.declaration && (await getRequestDeclarations());
args.declaration && (await getSchemaDeclarations());
args.declaration && (await generateDeclarations(true, true));

args.upgrade && (await upgrade(args.upgrade));

0 comments on commit c95df5c

Please sign in to comment.