import * as _ from "lodash"; export const generateDbTypeBlock = (schema: any) => { const getType = (type: string) => { switch (type) { default: return type; } }; // loop through [schema] & generate type based on schema mappings // join array const ret = _.join( _.map(schema, (dbType) => ` ${dbType.name}: ${getType(dbType.type)}`), "\n" ); // save in [gql_block] string return ret; }; export const generateTypeBlock = (schema: any) => { // filter [schema] for props const props = schema; const getType = (type: string) => { if (type !== "" && type.indexOf("enum") > -1) { // generate enum block // extract values, const regExp = /\[([^)]+)\]/; const matches: any = regExp.exec(type); const paramString: any = _.isArray(matches) ? matches[1] : ""; const enums: any = paramString.split(","); return `{ ${_.join( _.map(enums, (n) => _.trim(n) + ": string"), ", " )} }`; // create a type nameEnum [ map out list ], // attach to type // test: { Yes: string, No: string } } switch (type) { case "elements": return "React.ReactNode"; case "element": return "React.ReactNode"; case "function": return "() => any"; case "event": return "() => any"; case "method": return "() => any"; case "array": return "any[]"; case "bool": return "Boolean"; case "int": return "number"; default: return type; } }; // loop through [schema] & generate type based on schema mappings (e.g. element = JSX.Element, required = '?', etc.) // join array const ret = _.join( _.map( props, (prop) => ` /** * ${prop.description || "TODO: Add a description"} */ ${ prop.schema_type === "method" ? prop.name : `${prop.name}${!prop.required ? "?" : ""}: ${getType(prop.type)}` }` ), "\n" ); // save in [typeBlock] string return ret; }; export const generateMethodBlock = ( schema: any, includeConst: Boolean = false ) => { // filter [schema] for methods const methods = _.filter(schema, (s: any) => s.schema_type === "method"); const parseMethodName = (str: string) => { const name = _.trim(_.split(str, "(")[0]); // get value between parenthes const regExp = /\(([^)]+)\)/; const matches: any = regExp.exec(str); const paramString: any = _.isArray(matches) ? matches[1] : ""; const params: any[] = _.map(_.split(paramString, ","), (p) => _.split(p, ":") ); const returnType: string = "void"; return { name, params, paramString, returnType }; }; const getReturnType = (str: string) => { const returnValue = _.toLower(_.trim(_.last(str.split(":")))); switch (returnValue) { case "boolean": return "true"; case "object": return "{}"; case "string": return "''"; case "number": return "0"; default: return "''"; } }; const ret = _.join( _.map(methods, (method: any) => { const functionParams = parseMethodName(method.name); return ` /** * ${method.description || "TODO: Add Description Here..."} * ${_.join( _.map(functionParams["params"], (p) => `@params ${_.trim(p[0])}`), "\n\t * " )} */ ${ includeConst ? `const ${functionParams["name"]} = (${functionParams["paramString"]}): ${functionParams["returnType"]} => {}` : `${method.name} { return ${getReturnType(method.name)} }` }`; }), "" ); // save in [methodBlock] string return ret; }; export const generatePropBlock = (schema: any) => { // filter [schema] for props const props = _.filter(schema, (s) => s.schema_type === "prop"); const getType = (type: string) => { if (type === "elements") return "element"; if (type === "element") return "element"; if (type === "function") return "func"; if (type.indexOf("enum") > -1) return "oneOfType([PropTypes.string])"; return type; }; // loop through [schema] & generate type based on schema mappings (e.g. element = JSX.Element, required = '?', etc.) // join array const ret = _.join( _.map( props, (prop) => ` /** * ${prop.description} */ ${prop.name}: PropTypes.${getType(prop.type)}${ prop.required ? ".isRequired" : "" }, ` ), "" ); // save in [propBlock] string return ret; }; export const generateAttributes = (schema: any) => { // filter [schema] for props const props = _.filter( schema, (s) => s.schema_type === "prop" && s.required === true ); const getAttributeDefaultValue = (prop: any) => { if (prop.type.indexOf("enum") > -1) return `{0}`; switch (prop.type) { case "elements": return `{
}`; case "element": return `{
}`; case "string": return `""`; case "number": return `{0}`; case "enum": return `{0}`; case "array" || "any[]": return `{[]}`; default: return `""`; } }; const ret = _.join( _.map( props, (prop) => ` ${prop.name}=${getAttributeDefaultValue(prop)}` ), "\n" ); // save in [attributesBlock] string return ret; };