-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
44 lines (36 loc) · 1.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"use strict";
const dotenv = require('dotenv');
const contentful = require('contentful');
const fs = require('fs');
const path = require('path');
dotenv.config();
const BASE_URL = 'https://narative.co';
const root = path.dirname('./');
const redirectPath = path.join(root, '_redirects');
const contents = fs.readFileSync(redirectPath, 'utf8');
let newContents = contents; // Create a clinet to talk with Contentful
const client = contentful.createClient({
space: process.env.CONTENTFUL_SPACE_ID,
accessToken: process.env.CONTENTFUL_PREVIEW_API_KEY,
host: 'preview.contentful.com',
environment: process.env.CONTENTFUL_ENVIRONMENT || 'master',
dynamic_antries: 'auto'
});
async function createRedirects(event, context) {
const entries = await client.getEntries({
include: 10,
limit: 1000
});
const articles = entries.items.filter(entry => entry.sys.contentType.sys.id === 'article');
const redirects = articles.reduce((curr, next) => {
const {
shortUrl,
slug
} = next.fields;
const longUrl = `${BASE_URL}/articles/${slug}`;
return `/${shortUrl} ${longUrl}\n` + curr;
}, `/* ${BASE_URL}\n`);
console.log(redirects);
fs.writeFileSync(redirectPath, redirects);
}
createRedirects();