Skip to content

Commit

Permalink
feat: more be specific stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewLeedham committed Mar 4, 2021
1 parent 8474ef8 commit 646e85b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/cf-definitions-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,12 @@ export default class CFDefinitionsBuilder {
}

private addEntryTypeAlias = (file: SourceFile, aliasName: string, entryType: string) => {
file.addStatements([`export const typeId = '${aliasName}';`]);
const typeIdName = moduleTypeIdName(file.getBaseNameWithoutExtension());
file.addStatements([`export const ${typeIdName} = '${aliasName}';`]);
file.addTypeAlias({
isExported: true,
name: moduleName(aliasName),
type: renderGenericType('CMSEntry', `typeof typeId, ${entryType}`),
type: renderGenericType('CMSEntry', `typeof ${typeIdName}, ${entryType}`),
});
};

Expand Down Expand Up @@ -225,7 +226,7 @@ export default class CFDefinitionsBuilder {
namespaceImport: 'Contentful',
});
file.addImportDeclaration({
moduleSpecifier: '@common/helpers/contentful/getCMSEntry',
moduleSpecifier: '@src/types/contentful/static',
namedImports: ['CMSEntry'],
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/cf-render-prop-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {renderUnionType} from './render-union-type';

const linkContentType = (field: Pick<Field, 'validations'>): string => {
const validations = linkContentTypeValidations(field);
return validations?.length > 0 ? renderUnionType(validations.map(moduleName)) : 'CMSEntry<string, Record<string, any>>';
return validations?.length > 0 ? renderUnionType(validations.map(moduleName)) : 'CMSEntries';
};

export const renderPropLink = (field: Pick<Field, 'validations' | 'linkType'>) => {
Expand Down
7 changes: 7 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ export const moduleName = (name: string): string => {
return pipe([replaceDash, upperFirst, addPrefix, removeSpace])(name);
};

export const moduleManagementName = (name: string): string => {
const removeSpace = (input: string): string => input.replace(/\s/g, '');
const replaceDash = (input: string): string => input.replace(/-/g, '__');
const addPrefix = (input: string): string => input.endsWith('CMSManagementEntry') ? input : `${input}CMSManagementEntry`;
return pipe([replaceDash, upperFirst, addPrefix, removeSpace])(name);
};

export const moduleFieldsName = (name: string): string => moduleName(name) + 'Fields';

export const moduleTypeIdName = (name: string): string => camelCase(moduleName(name).replace(/CMSEntry$/, '')) + 'TypeId';
Expand Down

0 comments on commit 646e85b

Please sign in to comment.