From 494cc7651ad15876524bdd674333171b820878b2 Mon Sep 17 00:00:00 2001 From: aperron Date: Tue, 4 May 2021 02:38:39 +0200 Subject: [PATCH] remove old hack for component --- README.md | 4 +++ package-lock.json | 16 +----------- package.json | 3 +-- .../models/testobject.settings.json | 18 +++++++++++++ src/test/test1.assert.ts | 24 +++++++++++++++-- src/test/test2.assert.ts | 23 ++++++++++++++-- src/test/test3.assert.ts | 23 ++++++++++++++-- src/ts-exporter.ts | 26 +++++-------------- 8 files changed, 94 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 86402c9..f11c3c9 100644 --- a/README.md +++ b/README.md @@ -140,3 +140,7 @@ The input folder is recursively processed and each model file is read. When done npm install && npm run build # output files generated in dist folder ``` + + +console warn deprecated +template handlebrake/... \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 4d4a86a..0793c4f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,8 +10,7 @@ "license": "MIT", "dependencies": { "command-line-args": "^5.1.1", - "command-line-usage": "^5.0.5", - "pluralize": "^8.0.0" + "command-line-usage": "^5.0.5" }, "bin": { "sts": "bin/run.js" @@ -221,14 +220,6 @@ "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==", "dev": true }, - "node_modules/pluralize": { - "version": "8.0.0", - "resolved": "https://registry.npmjs.org/pluralize/-/pluralize-8.0.0.tgz", - "integrity": "sha512-Nc3IT5yHzflTfbjgqWcCPpo7DaKy4FnpB0l/zCAW0Tc7jxAiuqSxHasntB3D7887LSrA93kDJ9IXovxJYxyLCA==", - "engines": { - "node": ">=4" - } - }, "node_modules/reduce-flatten": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/reduce-flatten/-/reduce-flatten-1.0.1.tgz", @@ -506,11 +497,6 @@ "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==", "dev": true }, - "pluralize": { - "version": "8.0.0", - "resolved": "https://registry.npmjs.org/pluralize/-/pluralize-8.0.0.tgz", - "integrity": "sha512-Nc3IT5yHzflTfbjgqWcCPpo7DaKy4FnpB0l/zCAW0Tc7jxAiuqSxHasntB3D7887LSrA93kDJ9IXovxJYxyLCA==" - }, "reduce-flatten": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/reduce-flatten/-/reduce-flatten-1.0.1.tgz", diff --git a/package.json b/package.json index 750cd70..027b9ff 100644 --- a/package.json +++ b/package.json @@ -38,8 +38,7 @@ "homepage": "https://github.com/erikvullings/strapi-to-typescript#readme", "dependencies": { "command-line-args": "^5.1.1", - "command-line-usage": "^5.0.5", - "pluralize": "^8.0.0" + "command-line-usage": "^5.0.5" }, "devDependencies": { "@types/command-line-args": "^5.0.0", diff --git a/src/test/api/testobject/models/testobject.settings.json b/src/test/api/testobject/models/testobject.settings.json index 3605f96..332f508 100644 --- a/src/test/api/testobject/models/testobject.settings.json +++ b/src/test/api/testobject/models/testobject.settings.json @@ -97,6 +97,24 @@ "type": "uid", "required": true }, + "component_complex_repeatable": { + "component": "content.complex", + "repeatable": true, + "type": "component", + "required": true + }, + "component_complex_optional": { + "component": "content.complex", + "repeatable": false, + "type": "component", + "required": false + }, + "component_complex": { + "component": "content.complex", + "repeatable": false, + "type": "component", + "required": true + }, "single_dynamiczone": { "type": "dynamiczone", "repeatable": false, diff --git a/src/test/test1.assert.ts b/src/test/test1.assert.ts index 435a513..e8ab11e 100644 --- a/src/test/test1.assert.ts +++ b/src/test/test1.assert.ts @@ -1,10 +1,24 @@ import { ITestobject, ITestobjectrelation, EnumITestobjectenum_field } from './out1'; import { IFile } from './out1/file' -import { IComplex } from "./out1/content/complex"; +import { EnumIComplexvariant, IComplex } from "./out1/content/complex"; import { ISimple } from "./out1/content/simple"; import { IWithDash } from "./out1/content/camel-case"; import { IJustACompleteOtherName } from "./out1/content/another"; + +class IComplexImpl implements IComplex { + id: string; + variant?: EnumIComplexvariant; + key?: string; + single?: IComplex | ISimple; + repeatable: (IComplex | ISimple)[]; + + constructor(){ + this.id = "id"; + this.repeatable = []; + } +} + // implementation of Itestobject test required and type fields class ItestobjectImpl implements ITestobject { id: string; @@ -29,7 +43,10 @@ class ItestobjectImpl implements ITestobject { json_field: { [key: string]: any; }; uid_field: any; created_by: string; - single_dynamiczone?: [(IComplex | ISimple | IWithDash | IJustACompleteOtherName)]; + component_complex: IComplex; + component_complex_optional?: IComplex; + component_complex_repeatable:IComplex[]; + single_dynamiczone?: IComplex | ISimple | IWithDash | IJustACompleteOtherName; repeatable_dynamiczone: (IComplex | ISimple | IWithDash | IJustACompleteOtherName)[]; testobjectrelation?: ITestobjectrelation; @@ -61,6 +78,9 @@ class ItestobjectImpl implements ITestobject { this.datetime_field = {}; this.time_field = {}; this.uid_field = {}; + + this.component_complex = new IComplexImpl(); + this.component_complex_repeatable = []; } } diff --git a/src/test/test2.assert.ts b/src/test/test2.assert.ts index 57353b4..24e88b8 100644 --- a/src/test/test2.assert.ts +++ b/src/test/test2.assert.ts @@ -1,10 +1,23 @@ import { ITestobject, ITestobjectrelation, EnumITestobjectenum_field } from './out2'; import { IFile } from './out2/file/file'; -import { IComplex } from "./out2/content/complex"; +import { IComplex, EnumIComplexvariant } from "./out2/content/complex"; import { ISimple } from "./out2/content/simple"; import { IWithDash } from "./out2/content/camel-case"; import { IJustACompleteOtherName } from "./out2/content/another"; +class IComplexImpl implements IComplex { + id: string; + variant?: EnumIComplexvariant; + key?: string; + single?: IComplex | ISimple; + repeatable: (IComplex | ISimple)[]; + + constructor(){ + this.id = "id"; + this.repeatable = []; + } +} + // implementation of Itestobject test required and type fields class ItestobjectImpl implements ITestobject { id: string; @@ -29,7 +42,10 @@ class ItestobjectImpl implements ITestobject { json_field: { [key: string]: any; }; uid_field: any; created_by: string; - single_dynamiczone?: [(IComplex | ISimple | IWithDash | IJustACompleteOtherName)]; + component_complex: IComplex; + component_complex_optional?: IComplex; + component_complex_repeatable:IComplex[]; + single_dynamiczone?: IComplex | ISimple | IWithDash | IJustACompleteOtherName; repeatable_dynamiczone: (IComplex | ISimple | IWithDash | IJustACompleteOtherName)[]; testobjectrelation?: ITestobjectrelation; @@ -61,6 +77,9 @@ class ItestobjectImpl implements ITestobject { this.datetime_field = {}; this.time_field = {}; this.uid_field = {}; + + this.component_complex = new IComplexImpl(); + this.component_complex_repeatable = []; } } diff --git a/src/test/test3.assert.ts b/src/test/test3.assert.ts index 536f6a1..f53972c 100644 --- a/src/test/test3.assert.ts +++ b/src/test/test3.assert.ts @@ -1,10 +1,23 @@ import { Xtestobject, Xtestobjectrelation, EnumXtestobjectenum_field } from './out3'; import { Xfile } from './out3/Xfile'; -import { Xcomplex } from "./out3/content/Xcomplex"; +import { Xcomplex, EnumXcomplexvariant } from "./out3/content/Xcomplex"; import { Xsimple } from "./out3/content/Xsimple"; import { XWithDash } from "./out3/content/XWithDash"; import { XJustaCompleteOtherName } from "./out3/content/XJustaCompleteOtherName"; +class XcomplexImpl implements Xcomplex { + id: string; + variant?: EnumXcomplexvariant; + key?: string; + single?: Xcomplex | Xsimple; + repeatable: (Xcomplex | Xsimple)[]; + + constructor(){ + this.id = "id"; + this.repeatable = []; + } +} + // implementation of Itestobject test required and type fields class ItestobjectImpl implements Xtestobject { id: string; @@ -29,7 +42,10 @@ class ItestobjectImpl implements Xtestobject { json_field: { [key: string]: any; }; uid_field: any; created_by: string; - single_dynamiczone?: [(Xcomplex | Xsimple | XWithDash | XJustaCompleteOtherName)]; + component_complex: Xcomplex; + component_complex_optional?: Xcomplex; + component_complex_repeatable:Xcomplex[]; + single_dynamiczone?: Xcomplex | Xsimple | XWithDash | XJustaCompleteOtherName; repeatable_dynamiczone: (Xcomplex | Xsimple | XWithDash | XJustaCompleteOtherName)[]; testobjectrelation?: Xtestobjectrelation; @@ -61,6 +77,9 @@ class ItestobjectImpl implements Xtestobject { this.datetime_field = {}; this.time_field = {}; this.uid_field = {}; + + this.component_complex = new XcomplexImpl(); + this.component_complex_repeatable = []; } } diff --git a/src/ts-exporter.ts b/src/ts-exporter.ts index b7aee75..373f1f8 100644 --- a/src/ts-exporter.ts +++ b/src/ts-exporter.ts @@ -1,6 +1,5 @@ import * as fs from 'fs'; import * as path from 'path'; -import { singular } from 'pluralize' import { IStrapiModel, IStrapiModelAttribute } from './models/strapi-model'; import { IConfigOptions } from '..'; @@ -106,20 +105,6 @@ const findModel = (structure: IStrapiModelExtended[], name: string): IStrapiMode return structure.filter((s) => s.modelName.toLowerCase() === name.toLowerCase()).shift(); }; -/** - * Transform a Strapi Attribute of component. - * - * @param attr IStrapiModelAttribute - */ -const componentCompatible = (attr: IStrapiModelAttribute) => { - if (attr.type === 'component') { - let model = singular(attr.component!.split('.')[1]) - return attr.repeatable ? { collection: model } : { model: model } - } - return attr; -} - - class Converter { strapiModels: IStrapiModelExtended[] = []; @@ -238,10 +223,10 @@ class Converter { if (!m.attributes.hasOwnProperty(aName)) continue; - const a = componentCompatible(m.attributes[aName]); + const a = m.attributes[aName]; if ((a.collection || a.model) === m.modelName) continue; - const proposedImport = toImportDefinition(a.collection || a.model || '') + const proposedImport = toImportDefinition(a.collection || a.model || a.component || '') if (proposedImport) imports.push(proposedImport); imports.push(...(a.components || []) @@ -275,16 +260,17 @@ class Converter { }; const required = !a.required && !(!this.config.collectionCanBeUndefined && (a.collection || a.repeatable)) ? '?' : ''; - a = componentCompatible(a); - const collection = a.collection ? '[]' : ''; + const collection = a.collection || a.repeatable ? '[]' : ''; let propType = 'unknown'; if (a.collection) { propType = findModelName(a.collection); + } else if (a.component) { + propType = findModelName(a.component); } else if (a.model) { propType = findModelName(a.model); } else if (a.type === "dynamiczone") { - propType = `(${a.components!.map(findModelName).join("|")})[]` + propType = `(${a.components!.map(findModelName).join("|")})` } else if (a.type) { propType = util.toPropertyType(interfaceName, name, a, this.config.enum) }