|
| 1 | +/* eslint-disable no-console */ |
| 2 | + |
| 3 | +import formatAirtableRowData from "./src/utils/formatAirtableRowData"; |
| 4 | + |
| 5 | +require("dotenv").config(); |
| 6 | + |
| 7 | +const React = require("react"); |
| 8 | +const fs = require("fs"); |
| 9 | +const Airtable = require("airtable"); |
| 10 | +const https = require("https"); |
| 11 | + |
| 12 | +const trimFieldOrder = require("./src/utils/trimFieldOrder"); |
| 13 | +const Index = require("./src/components/Index").default; |
| 14 | +const RowPage = require("./src/components/RowPage").default; |
| 15 | +const tableHasPublishedColumn = require("./src/utils/tableHasPublishedColumn") |
| 16 | + .default; |
| 17 | + |
| 18 | +process.env.FIELD_ORDER = trimFieldOrder(process.env.FIELD_ORDER); |
| 19 | +process.env.HOMEPAGE_FIELD_ORDER = trimFieldOrder( |
| 20 | + process.env.HOMEPAGE_FIELD_ORDER |
| 21 | +); |
| 22 | + |
| 23 | +const renderAsHTMLPage = require(`./src/utils/renderAsHTMLPage`).default; |
| 24 | + |
| 25 | +const { AIRTABLE_API_KEY, BASE_ID, TABLE_ID, VIEW } = process.env; |
| 26 | + |
| 27 | +const base = new Airtable({ apiKey: AIRTABLE_API_KEY }).base(BASE_ID); |
| 28 | + |
| 29 | +const allRows = [[]]; |
| 30 | + |
| 31 | +const downloadFile = (url, filepath, onSuccess, onError) => { |
| 32 | + const file = fs.createWriteStream(filepath); |
| 33 | + https |
| 34 | + .get(url, response => { |
| 35 | + response.pipe(file); |
| 36 | + file.on("finish", () => { |
| 37 | + file.close(); |
| 38 | + onSuccess && onSuccess(); |
| 39 | + }); |
| 40 | + }) |
| 41 | + .on("error", fileErr => { |
| 42 | + console.log(fileErr); |
| 43 | + fs.unlink(filepath, error => onError && onError(error)); |
| 44 | + }); |
| 45 | +}; |
| 46 | + |
| 47 | +// used to make sure multiple pages aren't created for same slug |
| 48 | +const alreadySeenSlugs = {}; |
| 49 | + |
| 50 | +const alreadyDownloadedAttachments = {}; |
| 51 | + |
| 52 | +let currentPage = 0; |
| 53 | +let recordsOnCurrentPage = 0; |
| 54 | +tableHasPublishedColumn(base, includePublished => |
| 55 | + base(TABLE_ID) |
| 56 | + .select({ |
| 57 | + view: VIEW, |
| 58 | + ...(includePublished ? { filterByFormula: "{Published}" } : {}) |
| 59 | + }) |
| 60 | + .eachPage( |
| 61 | + function page(records, fetchNextPage) { |
| 62 | + records.forEach(row => { |
| 63 | + if (!allRows[currentPage]) { |
| 64 | + allRows.push([]); |
| 65 | + } |
| 66 | + const formattedRow = formatAirtableRowData(row); |
| 67 | + |
| 68 | + const attachmentFields = formattedRow.fields.filter( |
| 69 | + field => |
| 70 | + Array.isArray(field.value) && |
| 71 | + field.value[0] && |
| 72 | + field.value[0].size |
| 73 | + ); |
| 74 | + |
| 75 | + attachmentFields.forEach(attachmentField => { |
| 76 | + attachmentField.value.forEach(attachment => { |
| 77 | + if (!alreadyDownloadedAttachments[attachment.url]) { |
| 78 | + const newUrl = `/assets/${attachment.id}-${attachment.filename}`; |
| 79 | + downloadFile(attachment.url, `dist${newUrl}`); |
| 80 | + alreadyDownloadedAttachments[attachment.url] = true; |
| 81 | + attachment.url = newUrl; |
| 82 | + } |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + const slugFieldValue = row.fields.Slug; |
| 87 | + const slug = |
| 88 | + slugFieldValue !== undefined && !alreadySeenSlugs[slugFieldValue] |
| 89 | + ? slugFieldValue |
| 90 | + : formattedRow.id; |
| 91 | + alreadySeenSlugs[slug] = true; |
| 92 | + |
| 93 | + const pageTitle = row.fields[process.env.PAGE_TITLE_COLUMN]; |
| 94 | + |
| 95 | + const filepath = `dist/${slug}.html`; |
| 96 | + allRows[currentPage].push(formattedRow); |
| 97 | + recordsOnCurrentPage += 1; |
| 98 | + if (recordsOnCurrentPage >= 10) { |
| 99 | + recordsOnCurrentPage = 0; |
| 100 | + currentPage += 1; |
| 101 | + } |
| 102 | + |
| 103 | + // write individual resource page files |
| 104 | + fs.writeFile( |
| 105 | + filepath, |
| 106 | + renderAsHTMLPage(<RowPage rowData={formattedRow} />, pageTitle), |
| 107 | + error => { |
| 108 | + if (error) { |
| 109 | + console.error(`Error writing ${filepath}`); |
| 110 | + } else { |
| 111 | + console.log(`${filepath} written`); |
| 112 | + } |
| 113 | + } |
| 114 | + ); |
| 115 | + }); |
| 116 | + |
| 117 | + // calls page function again while there are still pages left |
| 118 | + fetchNextPage(); |
| 119 | + }, |
| 120 | + err => { |
| 121 | + if (err) { |
| 122 | + console.log(err); |
| 123 | + } |
| 124 | + |
| 125 | + const writeFile = (idx, filepath, pagination) => |
| 126 | + fs.writeFile( |
| 127 | + filepath, |
| 128 | + renderAsHTMLPage( |
| 129 | + <Index rows={allRows[idx]} pagination={pagination} /> |
| 130 | + ), |
| 131 | + error => { |
| 132 | + if (error) { |
| 133 | + console.error(`Error writing ${filepath}`); |
| 134 | + } |
| 135 | + } |
| 136 | + ); |
| 137 | + |
| 138 | + allRows.forEach((row, idx) => { |
| 139 | + const pageFilepath = `dist/page/${idx + 1}.html`; |
| 140 | + const indexFilepath = `dist/index.html`; |
| 141 | + const pagination = { |
| 142 | + back: idx > 0 ? `/page/${idx}.html` : null, |
| 143 | + next: idx < allRows.length - 1 ? `/page/${idx + 2}.html` : null |
| 144 | + }; |
| 145 | + if (idx === 0) { |
| 146 | + // write index page at / |
| 147 | + writeFile(idx, indexFilepath, pagination); |
| 148 | + } |
| 149 | + // write page files for pagination |
| 150 | + writeFile(idx, pageFilepath, pagination); |
| 151 | + }); |
| 152 | + } |
| 153 | + ) |
| 154 | +); |
| 155 | + |
| 156 | +fs.copyFile("public/default.css", "dist/main.css", () => |
| 157 | + fs.readFile("custom/styles.css", "utf-8", (err, data) => { |
| 158 | + fs.writeFile("dist/main.css", data, { flag: "a" }, error => { |
| 159 | + if (error) { |
| 160 | + console.error("Error writing custom CSS to dist/main.css"); |
| 161 | + } else { |
| 162 | + console.log("custom CSS appended to /dist/main.css"); |
| 163 | + } |
| 164 | + }); |
| 165 | + }) |
| 166 | +); |
| 167 | + |
| 168 | +fs.copyFile("custom/favicon.ico", "dist/favicon.ico", err => { |
| 169 | + if (err) { |
| 170 | + console.log("No favicon.ico file found"); |
| 171 | + } |
| 172 | +}); |
0 commit comments