Skip to content

Commit 20dfe19

Browse files
Steven PetrykGary ZhaoKayleigh FoleyMatt ZasoMolly Elfers
committed
Initial commit
Co-Authored-By: Gary Zhao <[email protected]> Co-Authored-By: Kayleigh Foley <[email protected]> Co-Authored-By: Matt Zaso <[email protected]> Co-Authored-By: Molly Elfers <[email protected]>
0 parents  commit 20dfe19

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+9283
-0
lines changed

.circleci/config.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
version: 2
2+
3+
defaults: &defaults
4+
working_directory: ~/repo
5+
docker:
6+
- image: circleci/node:10.15.0
7+
8+
jobs:
9+
test:
10+
<<: *defaults
11+
steps:
12+
- checkout
13+
14+
- restore_cache:
15+
keys:
16+
- v1-dependencies-{{arch}}-{{ checksum "package.json" }}
17+
- v1-dependencies-{{arch}}-
18+
19+
- run: yarn install
20+
21+
- save_cache:
22+
key: v1-dependencies-{{arch}}-{{ checksum "package.json" }}
23+
paths:
24+
- node_modules
25+
26+
- run:
27+
name: Check format
28+
command: yarn format:check
29+
30+
- run:
31+
name: Run linter
32+
command: yarn lint
33+
34+
- run:
35+
name: Run tests
36+
command: yarn test --ci
37+
38+
39+
release:
40+
<<: *defaults
41+
42+
steps:
43+
- checkout
44+
- run: yarn install
45+
- run: yarn build
46+
- run: yarn semantic-release
47+
48+
workflows:
49+
version: 2
50+
51+
test:
52+
jobs:
53+
- test
54+
- release:
55+
requires:
56+
- test

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
node_modules
2+
coverage
3+
.nyc_output
4+
.DS_Store
5+
*.log
6+
.vscode
7+
.idea
8+
compiled
9+
.awcache
10+
.rpt2_cache
11+
docs
12+
dist

.prettierrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"semi": false,
3+
"printWidth": 100,
4+
"trailingComma": "all"
5+
}

CONTRIBUTING.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# Contributing
2+
3+
Thank you for your interest in contributing to an Intercom project.
4+
5+
## Table of contents
6+
7+
- [Development](#development)
8+
- [Installation](#installation)
9+
- [Project structure](#project-structure)
10+
- [Code of Conduct](#code-of-conduct)
11+
- [Our pledge](#our-pledge)
12+
- [Our standards](#our-standards)
13+
- [Our responsibilities](#our-responsibilities)
14+
- [Scope](#scope)
15+
- [Enforcement](#enforcement)
16+
- [Attribution](#attribution)
17+
18+
## Development
19+
20+
### Installation
21+
22+
This project is a Rollup-based TypeScript NodeJS library. To get the source and make changes:
23+
24+
```bash
25+
git clone [email protected]:intercom/contentful-typescript-codegen.git
26+
cd contentful-typescript-codegen
27+
yarn install
28+
```
29+
30+
To ensure everything is set up correctly:
31+
32+
```bash
33+
yarn test
34+
```
35+
36+
### Project structure
37+
38+
This project consists of three main parts, and the `src/` folder mostly reflects this:
39+
40+
1. The **CLI entry point**, which is responsible for parsing command line arguments, loading the
41+
Contentful environment from the parent module's setup, and printing the generated TypeScript to
42+
the specified output location.
43+
1. The **TypeScript code generators**, which are helpers that print out things like interfaces,
44+
union types, and other TypeScript type structures.
45+
1. The **Contentful code generators**, which receive Contentful objects (like Content Types and
46+
Fields) and use the **TypeScript code generators** to turn them into interfaces.
47+
48+
The TypeScript code generators utilize one another to eventually resolve a giant string. This big,
49+
ugly string is ultimately passed through Prettier and sent back to the CLI layer to print to a file.
50+
51+
All generators have roughly the following shape:
52+
53+
```ts
54+
interface Generator {
55+
(thing: SomeParticularObject, options?: OptionsForGenerator): string
56+
}
57+
```
58+
59+
Testing mostly consists of snapshot testing. Tests use `prettier`, where applicable, to make the
60+
snapshots easier to read and to make sure they parse as valid TypeScript. Note that using `prettier`
61+
on too "narrow" of a test (i.e., _just_ a field) will cause a parser error. In such cases, just let
62+
the snapshot be ugly and let "broader" tests handle regressions involving potentially unparseable
63+
code.
64+
65+
## Code of Conduct
66+
67+
### Our pledge
68+
69+
In the interest of fostering an open and welcoming environment, we as
70+
contributors and maintainers pledge to making participation in our project and
71+
our community a harassment-free experience for everyone, regardless of age, body
72+
size, disability, ethnicity, sex characteristics, gender identity and expression,
73+
level of experience, education, socio-economic status, nationality, personal
74+
appearance, race, religion, or sexual identity and orientation.
75+
76+
### Our standards
77+
78+
Examples of behavior that contributes to creating a positive environment
79+
include:
80+
81+
- Using welcoming and inclusive language
82+
- Being respectful of differing viewpoints and experiences
83+
- Gracefully accepting constructive criticism
84+
- Focusing on what is best for the community
85+
- Showing empathy towards other community members
86+
87+
Examples of unacceptable behavior by participants include:
88+
89+
- The use of sexualized language or imagery and unwelcome sexual attention or
90+
advances
91+
- Trolling, insulting/derogatory comments, and personal or political attacks
92+
- Public or private harassment
93+
- Publishing others' private information, such as a physical or electronic
94+
address, without explicit permission
95+
- Other conduct which could reasonably be considered inappropriate in a
96+
professional setting
97+
98+
### Our responsibilities
99+
100+
Project maintainers are responsible for clarifying the standards of acceptable
101+
behavior and are expected to take appropriate and fair corrective action in
102+
response to any instances of unacceptable behavior.
103+
104+
Project maintainers have the right and responsibility to remove, edit, or
105+
reject comments, commits, code, wiki edits, issues, and other contributions
106+
that are not aligned to this Code of Conduct, or to ban temporarily or
107+
permanently any contributor for other behaviors that they deem inappropriate,
108+
threatening, offensive, or harmful.
109+
110+
### Scope
111+
112+
This Code of Conduct applies within all project spaces, and it also applies when
113+
an individual is representing the project or its community in public spaces.
114+
Examples of representing a project or community include using an official
115+
project e-mail address, posting via an official social media account, or acting
116+
as an appointed representative at an online or offline event. Representation of
117+
a project may be further defined and clarified by project maintainers.
118+
119+
### Enforcement
120+
121+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
122+
reported by contacting the project team at [email protected]. All
123+
complaints will be reviewed and investigated and will result in a response that
124+
is deemed necessary and appropriate to the circumstances. The project team is
125+
obligated to maintain confidentiality with regard to the reporter of an incident.
126+
Further details of specific enforcement policies may be posted separately.
127+
128+
Project maintainers who do not follow or enforce the Code of Conduct in good
129+
faith may face temporary or permanent repercussions as determined by other
130+
members of the project's leadership.
131+
132+
### Attribution
133+
134+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
135+
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
136+
137+
[homepage]: https://www.contributor-covenant.org
138+
139+
For answers to common questions about this code of conduct, see
140+
https://www.contributor-covenant.org/faq

LICENSE.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2019 Intercom
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# contentful-typescript-codegen
2+
3+
Generate typings from your Contentful environment.
4+
5+
- Content Types become interfaces.
6+
- Locales (and your default locale) become string types.
7+
- Assets and Rich Text link to Contentful's types.
8+
9+
At Intercom, we use this in our [marketing site] to increase developer confidence and productivity,
10+
ensure that breaking changes to our Content Types don't cause an outage, and because it's neat.
11+
12+
[marketing site]: https://www.intercom.com
13+
14+
## Usage
15+
16+
```sh
17+
yarn install --dev contentful-typescript-codegen
18+
```
19+
20+
Then, add the following to your `package.json`:
21+
22+
```jsonc
23+
{
24+
// ...
25+
"scripts": {
26+
"contentful-typescript-codegen": "contentful-typescript-codegen --output @types/generated/contentful.d.ts"
27+
}
28+
}
29+
```
30+
31+
Feel free to change the output path to whatever you like.
32+
33+
Next, the codegen will expect you to have created a file called `getContentfulEnvironment.js` in the
34+
root of your project directory, and it should export a promise that resolves with your Contentful
35+
environment.
36+
37+
The reason for this is that you can do whatever you like to set up your Contentful Management
38+
Client. Here's an example:
39+
40+
```js
41+
const contentfulManagement = require("contentful-management")
42+
43+
module.exports = function() {
44+
const contentfulClient = contentfulManagement.createClient({
45+
accessToken: process.env.CONTENTFUL_MANAGEMENT_API_ACCESS_TOKEN,
46+
})
47+
48+
return contentfulClient
49+
.getSpace(process.env.CONTENTFUL_SPACE_ID)
50+
.then(space => space.getEnvironment(process.env.CONTENTFUL_ENVIRONMENT))
51+
}
52+
```
53+
54+
### Command line options
55+
56+
```
57+
Usage
58+
$ contentful-typescript-codegen --output <file> <options>
59+
60+
Options
61+
--output, -o Where to write to
62+
--poll, -p Continuously refresh types
63+
--interval N, -i The interval in seconds at which to poll (defaults to 15)
64+
```
65+
66+
## Example output
67+
68+
Here's an idea of what the output will look like for a Content Type:
69+
70+
```ts
71+
interface IBlogPostFields {
72+
/** Title */
73+
title: string
74+
75+
/** Body */
76+
body: Document
77+
78+
/** Author link */
79+
author: IAuthor
80+
81+
/** Image */
82+
image: Asset
83+
84+
/** Published? */
85+
published: boolean | null
86+
87+
/** Tags */
88+
tags: string[]
89+
90+
/** Blog CTA variant */
91+
ctaVariant: "new-cta" | "old-cta"
92+
}
93+
94+
/**
95+
* A blog post.
96+
*/
97+
export interface IBlogPost extends Entry<IBlogPostFields> {}
98+
```
99+
100+
You can see that a few things are handled for you:
101+
102+
- Documentation comments are automatically generated from Contentful descriptions.
103+
- Links, like `author`, are resolved to other TypeScript interfaces.
104+
- Assets are handled properly.
105+
- Validations on symbols and text fields are expanded to unions.
106+
- Non-required attributes automatically have `| null` appended to their type.
107+
- The output is formatted using **your** Prettier config.

jest.config.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
transform: {
3+
".(ts|tsx)": "ts-jest",
4+
},
5+
testEnvironment: "node",
6+
testRegex: "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
7+
moduleFileExtensions: ["ts", "tsx", "js"],
8+
coveragePathIgnorePatterns: ["/node_modules/", "/test/", "/src/contentful-typescript-codegen.ts"],
9+
coverageThreshold: {
10+
global: {
11+
branches: 90,
12+
functions: 95,
13+
lines: 95,
14+
statements: 95,
15+
},
16+
},
17+
collectCoverageFrom: ["src/**/*.{js,ts}"],
18+
}

lint-staged.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
"{src,test}/**/*.ts": ["prettier --write", "git add"],
3+
}

0 commit comments

Comments
 (0)