Skip to content

Commit 7497ebd

Browse files
committed
2 parents aec3546 + 7b7b6df commit 7497ebd

File tree

12 files changed

+2379
-3665
lines changed

12 files changed

+2379
-3665
lines changed

.eslintrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
"indent": [
1818
"error",
1919
4
20-
]
20+
],
21+
"unicorn/consistent-function-scoping": "off",
22+
"unicorn/no-array-callback-reference": "off"
2123
},
2224
"extends": [
2325
"oclif",

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ const stringContent = new CFDefinitionsBuilder()
256256
### Browser Usage
257257
You can use `CFDefinitionsBuilder` also in a browser environment.
258258
> Example: [TS Content Types Generator App](https://github.com/marcolink/cf-content-types-generator-app)
259-

package.json

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
"@oclif/errors": "latest",
2727
"@oclif/plugin-help": "^3",
2828
"cli-ux": "^5.5.1",
29-
"contentful": "^8.1.7",
30-
"contentful-export": "^7.11.6",
29+
"contentful": "^9.1.28",
30+
"contentful-export": "^7.16.0",
3131
"fs-extra": "^9.1.0",
3232
"lodash": "^4.17.15",
3333
"ts-morph": "9.1.0",
@@ -38,18 +38,15 @@
3838
"@oclif/dev-cli": "^1",
3939
"@oclif/test": "^1.2.5",
4040
"@semantic-release/changelog": "^5.0.1",
41-
"@types/chai": "^4",
4241
"@types/fs-extra": "^9.0.1",
4342
"@types/lodash": "^4.14.155",
44-
"@types/mocha": "^8",
45-
"chai": "^4",
46-
"eslint": "^7.12",
47-
"eslint-config-oclif": "^3.1",
48-
"eslint-config-oclif-typescript": "^0.2",
43+
"@typescript-eslint/eslint-plugin": "^5.22.0",
44+
"eslint": "^8.14.0",
45+
"eslint-config-oclif": "^4.0.0",
46+
"eslint-config-oclif-typescript": "^1.0.2",
47+
"eslint-plugin-unicorn": "^42.0.0",
4948
"jest-fixtures": "^0.6.0",
50-
"mocha": "^5",
51-
"nyc": "^15",
52-
"semantic-release": "^17.0.7",
49+
"semantic-release": "^19.0.2",
5350
"strip-indent": "^3.0.0",
5451
"typescript": "4.2.2"
5552
},
@@ -90,6 +87,11 @@
9087
"@semantic-release/github"
9188
]
9289
},
90+
"resolutions": {
91+
"ansi-regex": ">=3.0.1 <6.0.0",
92+
"minimist": ">=1.2.6",
93+
"path-parse": ">=1.0.7"
94+
},
9395
"publishConfig": {
9496
"registry": "https://npm.pkg.github.com/xeroxinteractive"
9597
}

src/cf-definitions-builder.ts

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Field} from 'contentful';
2-
import * as path from 'path';
2+
import * as path from 'node:path';
33
import {
44
forEachStructureChild,
55
ImportDeclarationStructure,
@@ -52,7 +52,7 @@ export default class CFDefinitionsBuilder {
5252

5353
const interfaceDeclaration = this.createInterfaceDeclaration(file, moduleFieldsName(model.sys.id));
5454

55-
model.fields.forEach(field => this.addProperty(file, interfaceDeclaration, field));
55+
for (const field of model.fields) this.addProperty(file, interfaceDeclaration, field);
5656

5757
this.addEntryTypeAlias(file, model.sys.id, moduleFieldsName(model.sys.id));
5858

@@ -81,34 +81,33 @@ export default class CFDefinitionsBuilder {
8181
const imports: OptionalKind<ImportDeclarationStructure>[] = [];
8282
const types: string[] = [];
8383

84-
this.project.getSourceFiles()
85-
.filter(sourceFile => sourceFile.getBaseNameWithoutExtension() !== mergeFileName)
86-
.forEach(sourceFile => forEachStructureChild(sourceFile.getStructure(),
87-
childStructure => {
88-
switch (childStructure.kind) {
89-
case StructureKind.ImportDeclaration:
90-
imports.push(childStructure);
91-
break;
92-
case StructureKind.Interface:
93-
types.push(childStructure.name);
94-
mergeFile.addInterface(childStructure);
95-
break;
96-
case StructureKind.TypeAlias:
97-
types.push(childStructure.name);
98-
mergeFile.addTypeAlias(childStructure);
99-
break;
100-
default:
101-
throw new Error(`Unhandled node type '${StructureKind[childStructure.kind]}'.`);
102-
}
103-
}));
84+
for (const sourceFile of this.project.getSourceFiles()
85+
.filter(sourceFile => sourceFile.getBaseNameWithoutExtension() !== mergeFileName)) forEachStructureChild(sourceFile.getStructure(),
86+
childStructure => {
87+
switch (childStructure.kind) {
88+
case StructureKind.ImportDeclaration:
89+
imports.push(childStructure);
90+
break;
91+
case StructureKind.Interface:
92+
types.push(childStructure.name);
93+
mergeFile.addInterface(childStructure);
94+
break;
95+
case StructureKind.TypeAlias:
96+
types.push(childStructure.name);
97+
mergeFile.addTypeAlias(childStructure);
98+
break;
99+
default:
100+
throw new Error(`Unhandled node type '${StructureKind[childStructure.kind]}'.`);
101+
}
102+
});
104103

105104
// only import modules not present in merge file
106-
imports.forEach(importD => {
105+
for (const importD of imports) {
107106
const name = importD.moduleSpecifier.slice(2);
108107
if (!types.includes(name)) {
109108
mergeFile.addImportDeclaration(importD);
110109
}
111-
});
110+
}
112111

113112
mergeFile.organizeImports({
114113
ensureNewLineAtEndOfFile: true,
@@ -154,7 +153,7 @@ export default class CFDefinitionsBuilder {
154153
namedImports: ['CMSManagementEntry'],
155154
});
156155

157-
files.forEach(fileName => {
156+
for (const fileName of files) {
158157
indexFile.addImportDeclaration({
159158
isTypeOnly: true,
160159
namedImports: [moduleName(fileName), moduleFieldsName(fileName)],
@@ -169,7 +168,7 @@ export default class CFDefinitionsBuilder {
169168
moduleSpecifier: `./${fileName}`,
170169
});
171170
cmsEntries.push(moduleName(fileName));
172-
});
171+
}
173172

174173
indexFile.addTypeAlias({isExported: true, name: 'CMSEntries', type: renderUnionType(cmsEntries)});
175174
indexFile.addTypeAlias({isExported: true, name: 'CMSManagementEntries', type: renderUnionType(cmsEntries.map(name => renderGenericType('CMSManagementEntry', name)))});

src/cf-property-imports.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ export const propertyImports = (field: Field, ignoreModule?: string): OptionalKi
1717
.filter(filterIgnoredModule)
1818
.map(moduleImport) : [{moduleSpecifier: './index', namedImports: ['CMSEntries']}];
1919
}
20+
2021
if (field.type === 'Array' && field.items) {
2122
return field.items?.validations?.length > 0 ? linkContentTypeValidations(field.items)
2223
.filter(filterIgnoredModule)
2324
.map(moduleImport) : [{moduleSpecifier: './index', namedImports: ['CMSEntries']}];
2425
}
26+
2527
return [];
2628
};

src/cf-property-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ export const anyType = (field: Field): string => {
1414
) {
1515
return renderLiteralType;
1616
}
17+
1718
return (value: string) => value.toString();
1819
};
20+
1921
return renderUnionType(includesValidation.in.map(mapper()));
2022
}
2123
}

src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import {Command, flags} from '@oclif/command';
22
import * as fs from 'fs-extra';
33
import {writeFile} from 'fs-extra';
4-
import * as path from 'path';
4+
import * as path from 'node:path';
55
import CFDefinitionsBuilder from './cf-definitions-builder';
66

7+
// Does not appear to work as an import, not sure why and I'm just clearing up the audit
8+
// quickly so don't want to spent time messing about if it works.
9+
// eslint-disable-next-line unicorn/prefer-module
710
const contentfulExport = require('contentful-export');
811

912
class ContentfulMdg extends Command {
@@ -43,6 +46,7 @@ class ContentfulMdg extends Command {
4346
spaceId: flags.spaceId,
4447
managementToken: flags.token,
4548
environmentId: flags.environment,
49+
// https://github.com/contentful/contentful-export/issues/966
4650
skipEditorInterfaces: true,
4751
skipContent: true,
4852
skipRoles: true,
@@ -52,13 +56,16 @@ class ContentfulMdg extends Command {
5256
}
5357

5458
const builder = new CFDefinitionsBuilder();
59+
60+
// eslint-disable-next-line unicorn/no-array-for-each
5561
content.contentTypes.forEach(builder.appendType);
5662

5763
if (flags.out) {
5864
const outDir = path.resolve(flags.out);
5965
if (!flags.preserve && fs.existsSync(outDir)) {
6066
await fs.remove(outDir);
6167
}
68+
6269
await fs.ensureDir(outDir);
6370
await builder.write(flags.out, writeFile);
6471
} else {

src/renderer/cf-render-prop-array.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const renderPropArray = (field: Field): string => {
1919
if (validation?.length > 0) {
2020
return `(${renderUnionType(validation.map(renderLiteralType))})[]`;
2121
}
22+
2223
return 'Contentful.EntryFields.Symbol[]';
2324
}
2425

src/renderer/cf-render-prop-link.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ export const renderPropLink = (field: Pick<Field, 'validations' | 'linkType'>) =
1111
if (field.linkType === 'Entry') {
1212
return linkContentType(field);
1313
}
14+
1415
if (field.linkType === 'Asset') {
1516
return 'AssetLink';
1617
}
18+
1719
return 'Contentful.' + field.linkType!;
1820
};

src/renderer/render-union-type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const renderUnionType = (types: string[]): string => types.join(' | ');
1+
export const renderUnionType = (types: string[]): string => types.sort().join(' | ');

0 commit comments

Comments
 (0)