|
| 1 | +import { join, parse, relative } from "path"; |
| 2 | +import Attributes from "./Attributes"; |
| 3 | + |
| 4 | +export default class Interface { |
| 5 | + private BaseName: string; |
| 6 | + private Relations: Interface[] = []; // Components and relations |
| 7 | + private RelationNames: Record<string, [string, Interface]> = {}; |
| 8 | + private RelationNamesCounter: Record<string, number> = {}; |
| 9 | + private NamePrefix: string = ""; |
| 10 | + private Attributes: any; |
| 11 | + private RelativeDirectoryPath: string; |
| 12 | + private StrapiName: string; |
| 13 | + |
| 14 | + constructor(baseName: string, attributes: any, relativeDirectoryPath: string, prefix: string = "") { |
| 15 | + this.BaseName = baseName; |
| 16 | + this.StrapiName = `api::${baseName}.${baseName}`; |
| 17 | + this.NamePrefix = prefix; |
| 18 | + this.Attributes = attributes; |
| 19 | + this.RelativeDirectoryPath = relativeDirectoryPath; |
| 20 | + } |
| 21 | + |
| 22 | + getBaseName() { |
| 23 | + return this.BaseName; |
| 24 | + } |
| 25 | + |
| 26 | + getStrapiName() { |
| 27 | + return this.StrapiName; |
| 28 | + } |
| 29 | + |
| 30 | + getDependencies() { |
| 31 | + const attrs = new Attributes(this.Attributes, this.RelationNames); |
| 32 | + return attrs.getDependencies(); |
| 33 | + } |
| 34 | + |
| 35 | + getFullInterfaceName() { |
| 36 | + return `${this.NamePrefix}${this.BaseName}`; |
| 37 | + } |
| 38 | + |
| 39 | + // For typescript import from index file |
| 40 | + getRelativeRootPath() { |
| 41 | + const path = join(this.RelativeDirectoryPath, this.getBaseName()); |
| 42 | + const relativePath = (/^\.?\.\//).test(path) ? path : "./" + path; |
| 43 | + return relativePath; |
| 44 | + } |
| 45 | + |
| 46 | + getRelativeRootPathFile() { |
| 47 | + return `${this.getRelativeRootPath()}.ts` |
| 48 | + } |
| 49 | + |
| 50 | + setRelations(relations: Interface[]) { |
| 51 | + this.Relations = relations; |
| 52 | + this.RelationNames = {}; |
| 53 | + this.Relations.forEach((inter: Interface) => { |
| 54 | + let name = inter.getBaseName(); |
| 55 | + // Avoid duplicate names |
| 56 | + if (name in this.RelationNamesCounter) { |
| 57 | + name += ++this.RelationNamesCounter[name]; |
| 58 | + } else { |
| 59 | + this.RelationNamesCounter[name] = 0; |
| 60 | + } |
| 61 | + this.RelationNames[inter.getStrapiName()] = [name, inter]; |
| 62 | + }) |
| 63 | + } |
| 64 | + |
| 65 | + private getTsImports() { |
| 66 | + return Object.keys(this.RelationNames).map((strapiName: string) => { |
| 67 | + const relationName = this.RelationNames[strapiName][0]; |
| 68 | + const inter = this.RelationNames[strapiName][1]; |
| 69 | + const importPath = relative(this.getRelativeRootPath(), inter.getRelativeRootPath()); |
| 70 | + const fullName = inter.getFullInterfaceName(); |
| 71 | + const importNameString = fullName === relationName ? fullName : `${fullName} as ${relationName}`; |
| 72 | + return `import { ${importNameString} } from '${importPath}';`; |
| 73 | + }).join("\n"); |
| 74 | + } |
| 75 | + |
| 76 | + attributesToString() { |
| 77 | + let str = ''; |
| 78 | + for (const attributeName in this.Attributes) { |
| 79 | + const attr = this.Attributes[attributeName]; |
| 80 | + switch (attr.type) { |
| 81 | + default: |
| 82 | + |
| 83 | + break; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + getInerfaceString() { |
| 89 | + let str = 'export interface ${this.getFullInterfaceName()} {\n'; |
| 90 | + str += this.getInterfaceFieldsString(); |
| 91 | + str += `}` |
| 92 | + return str; |
| 93 | + } |
| 94 | + |
| 95 | + getInterfaceFieldsString() { |
| 96 | + let str: string; |
| 97 | + str += ` id: number;\n`; |
| 98 | + str += ` attributes: ${this.attributesToString()}\n`; |
| 99 | + return str; |
| 100 | + } |
| 101 | + |
| 102 | + toString() { |
| 103 | + return this.getTsImports() + this.getInerfaceString(); |
| 104 | + } |
| 105 | +} |
0 commit comments