Skip to content

Commit 7c2e330

Browse files
committed
recommit
1 parent 977c392 commit 7c2e330

24 files changed

+13599
-0
lines changed

bash/xargs-play

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash -xe
2+
echo 'one two three' | xargs mkdir
3+
ls
4+
echo 'one two three' | xargs rmdir

contentful/LICENSE

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

contentful/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Contentful JavaScript boilerplate project
2+
3+
Boilerplate project for getting started using JavaScript with Contentful
4+
5+
## Prerequisites
6+
7+
- **Node** v4.7.2 or greater
8+
9+
## Start the project
10+
11+
### :one: Clone the project using the following command:
12+
13+
```bash
14+
git clone https://github.com/contentful/boilerplate-javascript.git
15+
```
16+
### :two: Install dependencies and start it:
17+
18+
```shell
19+
npm install && npm start
20+
```

contentful/boilerplate-javascript.zip

5.75 KB
Binary file not shown.

contentful/index.js

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
'use strict'
2+
3+
const contentful = require('contentful')
4+
const chalk = require('chalk')
5+
const Table = require('cli-table2')
6+
7+
const SPACE_ID = 't7kcqcuo46f7'
8+
const ACCESS_TOKEN = '4mVIdqO-akfxhSiCF2kYCzhE5LAFiHbK0kPGKdrqvLo'
9+
10+
const client = contentful.createClient({
11+
// This is the space ID. A space is like a project folder in Contentful terms
12+
space: SPACE_ID,
13+
// This is the access token for this space. Normally you get both ID and the token in the Contentful web app
14+
accessToken: ACCESS_TOKEN
15+
})
16+
17+
console.log(chalk.green.bold('\nWelcome to the Contentful JS Boilerplate\n'))
18+
console.log('This is a simplified example to demonstrate the usage of the Contentful CDA\n')
19+
20+
// Entry point of the boilerplate, called at the end.
21+
function runBoilerplate () {
22+
displayContentTypes()
23+
.then(displayEntries)
24+
.then(() => {
25+
console.log('Want to go further? Feel free to check out this guide:')
26+
console.log(chalk.cyan('https://www.contentful.com/developers/docs/javascript/tutorials/using-js-cda-sdk/\n'))
27+
})
28+
.catch((error) => {
29+
console.log(chalk.red('\nError occurred:'))
30+
if (error.stack) {
31+
console.error(error.stack)
32+
return
33+
}
34+
console.error(error)
35+
})
36+
}
37+
38+
function displayContentTypes () {
39+
console.log(chalk.green('Fetching and displaying Content Types ...'))
40+
41+
return fetchContentTypes()
42+
.then((contentTypes) => {
43+
// Display a table with Content Type information
44+
const table = new Table({
45+
head: ['Id', 'Title', 'Fields']
46+
})
47+
contentTypes.forEach((contentType) => {
48+
const fieldNames = contentType.fields
49+
.map((field) => field.name)
50+
.sort()
51+
table.push([contentType.sys.id, contentType.name, fieldNames.join(', ')])
52+
})
53+
console.log(table.toString())
54+
55+
return contentTypes
56+
})
57+
}
58+
59+
function displayEntries (contentTypes) {
60+
console.log(chalk.green('Fetching and displaying Entries ...'))
61+
62+
return Promise.all(contentTypes.map((contentType) => {
63+
return fetchEntriesForContentType(contentType)
64+
.then((entries) => {
65+
console.log(`\These are the first 100 Entries for Content Type ${chalk.cyan(contentType.name)}:\n`)
66+
67+
// Display a table with Entry information
68+
const table = new Table({
69+
head: ['Id', 'Title']
70+
})
71+
entries.forEach((entry) => {
72+
table.push([entry.sys.id, entry.fields[contentType.displayField] || '[empty]'])
73+
})
74+
console.log(table.toString())
75+
})
76+
}))
77+
}
78+
79+
// Load all Content Types in your space from Contentful
80+
function fetchContentTypes () {
81+
return client.getContentTypes()
82+
.then((response) => response.items)
83+
.catch((error) => {
84+
console.log(chalk.red('\nError occurred while fetching Content Types:'))
85+
console.error(error)
86+
})
87+
}
88+
89+
// Load all entries for a given Content Type from Contentful
90+
function fetchEntriesForContentType (contentType) {
91+
return client.getEntries({
92+
content_type: contentType.sys.id
93+
})
94+
.then((response) => response.items)
95+
.catch((error) => {
96+
console.log(chalk.red(`\nError occurred while fetching Entries for ${chalk.cyan(contentType.name)}:`))
97+
console.error(error)
98+
})
99+
}
100+
101+
// Start the boilerplate code
102+
runBoilerplate()

0 commit comments

Comments
 (0)