-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
160 lines (136 loc) · 4.62 KB
/
build.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
/* eslint-disable no-console */
import formatAirtableRowData from "./src/utils/formatAirtableRowData";
require("dotenv").config();
const React = require("react");
const fs = require("fs");
const Airtable = require("airtable");
const https = require("https");
const trimFieldOrder = require("./src/utils/trimFieldOrder");
const RowDisplay = require("./src/components/RowDisplay").default;
const RowPage = require("./src/modules/birthday/RowPage").default;
const tableHasPublishedColumn = require("./src/utils/tableHasPublishedColumn")
.default;
process.env.FIELD_ORDER = trimFieldOrder(process.env.FIELD_ORDER);
process.env.HOMEPAGE_FIELD_ORDER = trimFieldOrder(
process.env.HOMEPAGE_FIELD_ORDER
);
const renderAsHTMLPage = require(`./src/utils/renderAsHTMLPage`).default;
const { AIRTABLE_API_KEY, BASE_ID, TABLE_ID, VIEW } = process.env;
const base = new Airtable({ apiKey: AIRTABLE_API_KEY }).base(BASE_ID);
const allRows = [];
const downloadFile = (url, filepath, onSuccess, onError) => {
const file = fs.createWriteStream(filepath);
https
.get(url, response => {
response.pipe(file);
file.on("finish", () => {
file.close();
onSuccess && onSuccess();
});
})
.on("error", fileErr => {
console.log(fileErr);
fs.unlink(filepath, error => onError && onError(error));
});
};
// used to make sure multiple pages aren't created for same slug
const alreadySeenSlugs = {};
const alreadyDownloadedAttachments = {};
let currentPage = 0;
let recordsOnCurrentPage = 0;
tableHasPublishedColumn(base, includePublished =>
base(TABLE_ID)
.select({
view: VIEW,
...(includePublished ? { filterByFormula: "{Published}" } : {})
})
.eachPage(
function page(records, fetchNextPage) {
records.forEach(row => {
/*
if (!allRows[currentPage]) {
allRows.push([]);
}
*/
const formattedRow = formatAirtableRowData(row);
const attachmentFields = formattedRow.fields.filter(
field =>
Array.isArray(field.value) &&
field.value[0] &&
field.value[0].size
);
attachmentFields.forEach(attachmentField => {
attachmentField.value.forEach(attachment => {
if (!alreadyDownloadedAttachments[attachment.url]) {
const newUrl = `/assets/${attachment.id}-${attachment.filename}`;
downloadFile(attachment.url, `dist${newUrl}`);
alreadyDownloadedAttachments[attachment.url] = true;
attachment.url = newUrl;
}
});
});
const slugFieldValue = row.fields.Slug;
const slug =
slugFieldValue !== undefined && !alreadySeenSlugs[slugFieldValue]
? slugFieldValue
: formattedRow.id;
alreadySeenSlugs[slug] = true;
const pageTitle = row.fields[process.env.PAGE_TITLE_COLUMN];
const filepath = `dist/${slug}.html`;
allRows.push(formattedRow);
// write individual resource page files
fs.writeFile(
filepath,
renderAsHTMLPage(<RowPage rowData={formattedRow} />, pageTitle),
error => {
if (error) {
console.error(`Error writing ${filepath}`);
} else {
console.log(`${filepath} written`);
}
}
);
});
// calls page function again while there are still pages left
fetchNextPage();
},
err => {
if (err) {
console.log(err);
}
const writeFile = (idx, filepath) =>
fs.writeFile(
filepath,
renderAsHTMLPage(<Index rows={allRows[idx]} />),
error => {
if (error) {
console.error(`Error writing ${filepath}`);
}
}
);
allRows.forEach((row, idx) => {
const indexFilepath = `dist/index.html`;
if (idx === 0) {
// write index page at /
writeFile(idx, indexFilepath);
}
});
}
)
);
fs.copyFile("public/default.css", "dist/main.css", () =>
fs.readFile("custom/styles.css", "utf-8", (err, data) => {
fs.writeFile("dist/main.css", data, { flag: "a" }, error => {
if (error) {
console.error("Error writing custom CSS to dist/main.css");
} else {
console.log("custom CSS appended to /dist/main.css");
}
});
})
);
fs.copyFile("custom/favicon.ico", "dist/favicon.ico", err => {
if (err) {
console.log("No favicon.ico file found");
}
});