Skip to content

Commit

Permalink
fix(export): module name (#12)
Browse files Browse the repository at this point in the history
Ensure valid module names for any given input string.
  • Loading branch information
marcolink authored Jun 20, 2020
1 parent 1f90fc8 commit 421ebe5
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 50 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,22 +214,22 @@ As input a [json file](https://github.com/contentful/contentful-cli/tree/master/
import * as CFRichTextTypes from "@contentful/rich-text-types";
import * as Contentful from "contentful";

export interface ArtistFields {
export interface TypeArtistFields {
name: Contentful.EntryFields.Symbol;
profilePicture?: Contentful.Asset;
bio?: CFRichTextTypes.Block | CFRichTextTypes.Inline;
}

export type Artist = Contentful.Entry<ArtistFields>;
export type TypeArtist = Contentful.Entry<TypeArtistFields>;

export interface ArtworkFields {
export interface TypeArtworkFields {
name: Contentful.EntryFields.Symbol;
type?: "print" | "drawing" | "painting";
preview?: Contentful.Asset[];
artist: Contentful.Entry<ArtistFields>;
artist: Contentful.Entry<TypeArtistFields>;
}

export type Artwork = Contentful.Entry<ArtworkFields>;
export type TypeArtwork = Contentful.Entry<ArtworkFields>;
```
This all only works if you add the [`contentful`](https://www.npmjs.com/package/contentful) package to your target project to get all relevant type definitions.

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"contentful": "^7.14.0",
"contentful-export": "^7.8.0",
"fs-extra": "^8.1.0",
"lodash": "^4.17.15",
"ts-morph": "^7.0.0",
"ts-node": "^8.6.2",
"tslib": "^1"
Expand All @@ -39,6 +40,7 @@
"@semantic-release/changelog": "^5.0.1",
"@types/chai": "^4",
"@types/fs-extra": "^8.1.0",
"@types/lodash": "^4.14.155",
"@types/mocha": "^5",
"chai": "^4",
"eslint": "^5.13",
Expand Down
9 changes: 8 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import {FieldItem, FieldValidation} from 'contentful';
import {upperFirst} from 'lodash';
import {pipe} from 'lodash/fp';

export const moduleName = (name: string): string => name.charAt(0).toUpperCase() + name.slice(1);
export const moduleName = (name: string): string => {
const removeSpace = (input: string): string => input.replace(' ', '');
const replaceDash = (input: string): string => input.replace('-', '__');
const addPrefix = (input: string): string => input.startsWith('Type') ? input : `Type${input}`;
return pipe([replaceDash, upperFirst, addPrefix, removeSpace])(name);
};

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

Expand Down
79 changes: 39 additions & 40 deletions test/cf-definitions-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ describe('A Contentful definitions builder', () => {
expect('\n' + builder.toString()).to.equal(stripIndent(`
import * as Contentful from "contentful";
export interface SysIdFields {
export interface TypeSysIdFields {
}
export type SysId = Contentful.Entry<SysIdFields>;
export type TypeSysId = Contentful.Entry<TypeSysIdFields>;
`));
});

Expand All @@ -73,11 +73,11 @@ describe('A Contentful definitions builder', () => {
expect('\n' + builder.toString()).to.equal(stripIndent(`
import * as Contentful from "contentful";
export interface SysIdFields {
export interface TypeSysIdFields {
symbolFieldId: Contentful.EntryFields.Symbol;
}
export type SysId = Contentful.Entry<SysIdFields>;
export type TypeSysId = Contentful.Entry<TypeSysIdFields>;
`));
});

Expand All @@ -101,11 +101,11 @@ describe('A Contentful definitions builder', () => {
expect('\n' + builder.toString()).to.equal(stripIndent(`
import * as Contentful from "contentful";
export interface SysIdFields {
export interface TypeSysIdFields {
symbolFieldId?: Contentful.EntryFields.Symbol;
}
export type SysId = Contentful.Entry<SysIdFields>;
export type TypeSysId = Contentful.Entry<TypeSysIdFields>;
`));
});

Expand All @@ -129,11 +129,11 @@ describe('A Contentful definitions builder', () => {
expect('\n' + builder.toString()).to.equal(stripIndent(`
import * as Contentful from "contentful";
export interface SysIdFields {
export interface TypeSysIdFields {
boolFieldId?: Contentful.EntryFields.Boolean;
}
export type SysId = Contentful.Entry<SysIdFields>;
export type TypeSysId = Contentful.Entry<TypeSysIdFields>;
`));
});

Expand Down Expand Up @@ -163,13 +163,13 @@ describe('A Contentful definitions builder', () => {
});
expect('\n' + builder.toString()).to.equal(stripIndent(`
import * as Contentful from "contentful";
import { LinkedTypeFields } from "./LinkedType";
import { TypeLinkedTypeFields } from "./TypeLinkedType";
export interface SysIdFields {
linkFieldId?: Contentful.Entry<LinkedTypeFields>;
export interface TypeSysIdFields {
linkFieldId?: Contentful.Entry<TypeLinkedTypeFields>;
}
export type SysId = Contentful.Entry<SysIdFields>;
export type TypeSysId = Contentful.Entry<TypeSysIdFields>;
`));
});

Expand Down Expand Up @@ -204,14 +204,14 @@ describe('A Contentful definitions builder', () => {
});
expect('\n' + builder.toString()).to.equal(stripIndent(`
import * as Contentful from "contentful";
import { ArtistFields } from "./Artist";
import { ArtworkFields } from "./Artwork";
import { TypeArtistFields } from "./TypeArtist";
import { TypeArtworkFields } from "./TypeArtwork";
export interface SysIdFields {
arrayFieldId?: Contentful.Entry<ArtistFields | ArtworkFields>[];
export interface TypeSysIdFields {
arrayFieldId?: Contentful.Entry<TypeArtistFields | TypeArtworkFields>[];
}
export type SysId = Contentful.Entry<SysIdFields>;
export type TypeSysId = Contentful.Entry<TypeSysIdFields>;
`));
});

Expand Down Expand Up @@ -242,11 +242,11 @@ describe('A Contentful definitions builder', () => {
expect('\n' + builder.toString()).to.equal(stripIndent(`
import * as Contentful from "contentful";
export interface SysIdFields {
export interface TypeSysIdFields {
stringFieldId?: "hello" | "world";
}
export type SysId = Contentful.Entry<SysIdFields>;
export type TypeSysId = Contentful.Entry<TypeSysIdFields>;
`));
});

Expand Down Expand Up @@ -278,11 +278,11 @@ describe('A Contentful definitions builder', () => {
expect('\n' + builder.toString()).to.equal(stripIndent(`
import * as Contentful from "contentful";
export interface SysIdFields {
export interface TypeSysIdFields {
numericFieldId?: 1 | 2 | 3;
}
export type SysId = Contentful.Entry<SysIdFields>;
export type TypeSysId = Contentful.Entry<TypeSysIdFields>;
`));
});

Expand All @@ -291,15 +291,15 @@ describe('A Contentful definitions builder', () => {

await builder.write(fixturesPath);

const result = await fs.readFile(path.resolve(fixturesPath, 'SysId.ts'));
const result = await fs.readFile(path.resolve(fixturesPath, 'TypeSysId.ts'));

expect('\n' + result.toString()).to.equal(stripIndent(`
import * as Contentful from "contentful";
export interface SysIdFields {
export interface TypeSysIdFields {
}
export type SysId = Contentful.Entry<SysIdFields>;
export type TypeSysId = Contentful.Entry<TypeSysIdFields>;
`));
});

Expand All @@ -316,25 +316,25 @@ describe('A Contentful definitions builder', () => {

await builder.write(fixturesPath);

const result1 = await fs.readFile(path.resolve(fixturesPath, 'SysId.ts'));
const result2 = await fs.readFile(path.resolve(fixturesPath, 'MyType.ts'));
const result1 = await fs.readFile(path.resolve(fixturesPath, 'TypeSysId.ts'));
const result2 = await fs.readFile(path.resolve(fixturesPath, 'TypeMyType.ts'));

expect('\n' + result1.toString()).to.equal(stripIndent(`
import * as Contentful from "contentful";
export interface SysIdFields {
export interface TypeSysIdFields {
}
export type SysId = Contentful.Entry<SysIdFields>;
export type TypeSysId = Contentful.Entry<TypeSysIdFields>;
`));

expect('\n' + result2.toString()).to.equal(stripIndent(`
import * as Contentful from "contentful";
export interface MyTypeFields {
export interface TypeMyTypeFields {
}
export type MyType = Contentful.Entry<MyTypeFields>;
export type TypeMyType = Contentful.Entry<TypeMyTypeFields>;
`));
});

Expand Down Expand Up @@ -369,27 +369,27 @@ describe('A Contentful definitions builder', () => {

await builder.write(fixturesPath);

const result1 = await fs.readFile(path.resolve(fixturesPath, 'SysId.ts'));
const result2 = await fs.readFile(path.resolve(fixturesPath, 'MyType.ts'));
const result1 = await fs.readFile(path.resolve(fixturesPath, 'TypeSysId.ts'));
const result2 = await fs.readFile(path.resolve(fixturesPath, 'TypeMyType.ts'));

expect('\n' + result1.toString()).to.equal(stripIndent(`
import * as Contentful from "contentful";
export interface SysIdFields {
export interface TypeSysIdFields {
}
export type SysId = Contentful.Entry<SysIdFields>;
export type TypeSysId = Contentful.Entry<TypeSysIdFields>;
`));

expect('\n' + result2.toString()).to.equal(stripIndent(`
import * as Contentful from "contentful";
import { SysIdFields } from "./SysId";
import { TypeSysIdFields } from "./TypeSysId";
export interface MyTypeFields {
linkFieldId?: Contentful.Entry<SysIdFields>;
export interface TypeMyTypeFields {
linkFieldId?: Contentful.Entry<TypeSysIdFields>;
}
export type MyType = Contentful.Entry<MyTypeFields>;
export type TypeMyType = Contentful.Entry<TypeMyTypeFields>;
`));
});

Expand All @@ -398,9 +398,8 @@ describe('A Contentful definitions builder', () => {
await builder.write(fixturesPath);

const result1 = await fs.readFile(path.resolve(fixturesPath, 'index.ts'));
console.log(result1.toString());
expect('\n' + result1.toString()).to.equal(stripIndent(`
export { SysId, SysIdFields } from "./SysId";
export { TypeSysId, TypeSysIdFields } from "./TypeSysId";
`));
});
});
4 changes: 2 additions & 2 deletions test/cf-property-imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ describe('A typeImports function', () => {
}
`);
expect(propertyImports(field)).to.eql([{
moduleSpecifier: './TopicCategory',
namedImports: ['TopicCategoryFields'],
moduleSpecifier: './TypeTopicCategory',
namedImports: ['TypeTopicCategoryFields'],
}]);
});

Expand Down
4 changes: 2 additions & 2 deletions test/cf-render-prop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('A renderProp function', () => {
}
`);

expect(renderProp(field)).to.equal('Contentful.Entry<TopicCategoryFields>');
expect(renderProp(field)).to.equal('Contentful.Entry<TypeTopicCategoryFields>');
});

it('can evaluate an "Array" of "Symbol"', () => {
Expand Down Expand Up @@ -151,6 +151,6 @@ describe('A renderProp function', () => {
}
`);

expect(renderProp(field)).to.equal('Contentful.Entry<ComponentCtaFields | ComponentFaqFields | WrapperImageFields | WrapperVideoFields>[]');
expect(renderProp(field)).to.equal('Contentful.Entry<TypeComponentCtaFields | TypeComponentFaqFields | TypeWrapperImageFields | TypeWrapperVideoFields>[]');
});
});
21 changes: 21 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {expect} from "@oclif/test";
import {moduleName} from "../src/utils";

describe('a moduleName function', () => {

const specs = [
{input: 'helloWorld', expect: 'TypeHelloWorld'},
{input: 'hello-World', expect: 'TypeHello__World'},
{input: '0helloWorld', expect: 'Type0helloWorld'},
{input: '0-helloWorld', expect: 'Type0__helloWorld'},
{input: 'hello World', expect: 'TypeHelloWorld'},
{input: 'hello-World', expect: 'TypeHello__World'},
{input: '12345', expect: 'Type12345'},
];

specs.forEach(spec => {
it('transforms \'' + spec.input + '\' to valid module name \'' + spec.expect + '\'', () => {
expect(moduleName(spec.input)).to.equal(spec.expect);
});
})
});
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,11 @@
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.149.tgz#1342d63d948c6062838fbf961012f74d4e638440"
integrity sha512-ijGqzZt/b7BfzcK9vTrS6MFljQRPn5BFWOx8oE0GYxribu6uV+aA9zZuXI1zc/etK9E8nrgdoF2+LgUw7+9tJQ==

"@types/lodash@^4.14.155":
version "4.14.155"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.155.tgz#e2b4514f46a261fd11542e47519c20ebce7bc23a"
integrity sha512-vEcX7S7aPhsBCivxMwAANQburHBtfN9RdyXFk84IJmu2Z4Hkg1tOFgaslRiEqqvoLtbCBi6ika1EMspE+NZ9Lg==

"@types/minimatch@*", "@types/minimatch@^3.0.3":
version "3.0.3"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"
Expand Down

0 comments on commit 421ebe5

Please sign in to comment.