-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite-publisher.js
163 lines (134 loc) · 4.42 KB
/
site-publisher.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"use strict";
const _get = require("lodash.get");
const fs = require("fs-extra");
const path = require("path");
require("dotenv").config();
const { getBasePages, getEntries } = require("./prismic-client");
const baseMeta = require("./meta.config");
const {
mergeOnto,
cleanKeys,
flattenNodes,
objectifyEdges,
cleanBodies,
flattenMetaImages,
tagEntries,
} = require("./utils/general-utils");
const log = require("./utils/chalk");
const EntryHolder = require("./utils/entry-holder");
const createHome = require("./generators/create-home");
const createLanding = require("./generators/create-landing");
const createPage = require("./generators/create-page");
const createGuestbook = require("./generators/create-guestbook");
const createBlog = require("./generators/create-blog");
const createAbout = require("./generators/create-about");
const createLists = require("./generators/create-lists");
const fetchPrismicData = async () => {
try {
const data = await getBasePages();
const home = _get(data, "allHomes.edges[0].node", {});
const landing = _get(data, "allLandings.edges[0].node", {});
const metaInformation = flattenMetaImages(mergeOnto(baseMeta, home));
const cleanHome = cleanKeys(home, baseMeta);
const blog = _get(data, "allBlogs.edges[0].node", {});
const guestbook = _get(data, "allGuestbooks.edges[0].node", {});
const about = _get(data, "allAbouts.edges[0].node", {});
const lists = _get(data, "allListss.edges[0].node", {});
const allEntries = await getEntries();
// remove node key from object
const entries = flattenNodes(allEntries);
// get rid of unneeded _meta info
const parsedEntries = objectifyEdges(cleanBodies(entries));
// separate each entry into tagged objects
const taggedEntries = tagEntries(parsedEntries);
EntryHolder.setEntries(parsedEntries);
return {
landing,
blog,
lists,
guestbook,
about,
metaInformation,
home: cleanHome,
miniEntries: taggedEntries.mini,
blogEntries: taggedEntries.blog,
listEntries: taggedEntries.list,
};
} catch (error) {
console.error(error.message);
}
};
const injectMeta = (meta) => {
return (handler, ...data) => {
handler(...data, { ...meta });
};
};
const createPagesAndInjectData = async (pages) => {
try {
const {
home,
landing,
lists,
miniEntries,
blogEntries,
listEntries,
metaInformation,
blog,
guestbook,
about,
} = pages;
const buildPath = path.resolve(__dirname, "build");
log.header("Cleaning build folder");
await fs.emptyDir(buildPath);
log.header(`Creating ${process.env.NODE_ENV} build`);
const pageHandler = injectMeta(metaInformation);
// in these calls, we spread certain objects so that we're not changing them as we move through the pages
// the spread creates a "new" object
log.header("Creating Home");
pageHandler(createHome, home);
log.header("Creating Landing");
pageHandler(createLanding, landing);
log.header("Creating Blog");
pageHandler(createBlog, blog, { ...blogEntries });
log.header("Creating Lists");
pageHandler(createLists, lists, { ...listEntries });
log.header("Creating Guestbook");
pageHandler(createGuestbook, guestbook);
log.header("Creating About");
pageHandler(createAbout, about);
log.header("Creating mini pages");
for (const value of Object.values(miniEntries)) {
const { slug } = value;
log.subtitle(`Creating ${slug}`);
pageHandler(createPage, slug, value, "home");
}
log.header("Creating blog pages");
for (const value of Object.values(blogEntries)) {
const { slug } = value;
log.subtitle(`Creating ${slug}`);
pageHandler(createPage, slug, value, "blog");
}
log.header("Creating list pages");
for (const value of Object.values(listEntries)) {
const { slug } = value;
log.subtitle(`Creating ${slug}`);
pageHandler(createPage, slug, value, "lists");
}
log.header("Copying over static files");
// copy static files into build
fs.copySync(path.resolve(__dirname, "static"), buildPath);
} catch (error) {
console.error(error.message);
}
};
const publish = () => {
return new Promise((res, rej) => {
fetchPrismicData()
.then((data) => {
createPagesAndInjectData(data);
res();
})
.catch(rej);
});
};
module.exports = publish;