From 88218e038ab1fb7bc32e97d09ea3b7363546659a Mon Sep 17 00:00:00 2001 From: Luke Bonaccorsi Date: Mon, 28 Nov 2022 13:01:24 +0000 Subject: [PATCH 1/4] testing netlify functions --- .eleventy.js | 27 +++++++++--- netlify/functions/build.js | 9 ++++ package-lock.json | 75 ++++++++++++++++++++++++++++++++++ package.json | 3 ++ src/_data/site.js | 11 +++++ src/_scripts/new.js | 21 +++++++--- src/_scripts/templates/post.md | 2 +- src/pages/nextBuild.11ty.js | 39 ++++++++++++++++++ src/posts/future-post-test.md | 7 ++++ 9 files changed, 181 insertions(+), 13 deletions(-) create mode 100644 netlify/functions/build.js create mode 100644 src/pages/nextBuild.11ty.js create mode 100644 src/posts/future-post-test.md diff --git a/.eleventy.js b/.eleventy.js index ce92ead..d92f579 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -5,6 +5,7 @@ const readingTime = require("eleventy-plugin-reading-time"); const nunjucksDate = require("nunjucks-date-filter"); const slugify = require("slugify"); const prettier = require("prettier"); +const { zonedTimeToUtc } = require("date-fns-tz"); const postcss = require("./src/_utils/postcss.js"); const minifycss = require("./src/_utils/minifycss.js"); @@ -201,20 +202,34 @@ module.exports = function (eleventyConfig) { }, }); + function getUTCPostDate(date) { + return zonedTimeToUtc( + `${date.getFullYear()}-${ + date.getMonth() + 1 + }-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`, + site.timezone + ); + } + // Collections const now = new Date(); - const livePosts = (post) => post.date <= now && !post.data.draft; + const livePosts = (post) => + getUTCPostDate(post.date, post.data.time) <= now && !post.data.draft; - const posts = (collectionApi) => - collectionApi - .getFilteredByGlob("./src/posts/*") - .filter(livePosts) - .reverse(); + const futurePosts = (post) => + getUTCPostDate(post.date, post.data.time) > now && !post.data.draft; + + const posts = (collectionApi, filter = livePosts) => + collectionApi.getFilteredByGlob("./src/posts/*").filter(filter).reverse(); eleventyConfig.addCollection("posts", (collectionApi) => posts(collectionApi) ); + eleventyConfig.addCollection("futurePosts", (collectionApi) => + posts(collectionApi, futurePosts).reverse() + ); + eleventyConfig.addCollection("tagList", function (collection) { let tagList = []; collection diff --git a/netlify/functions/build.js b/netlify/functions/build.js new file mode 100644 index 0000000..cfca1a4 --- /dev/null +++ b/netlify/functions/build.js @@ -0,0 +1,9 @@ +const { schedule } = require("@netlify/functions"); +const { cron } = require("../../dist/nextbuild.json"); + +exports.handler = async function (event, context) { + return { + statusCode: 200, + body: JSON.stringify({ message: cron }), + }; +}; diff --git a/package-lock.json b/package-lock.json index 31ad2fd..b9f842c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,8 +13,11 @@ "@11ty/eleventy-img": "^2.0.1", "@11ty/eleventy-plugin-rss": "^1.1.0", "@11ty/eleventy-plugin-syntaxhighlight": "^3.0.6", + "@iarna/toml": "^2.2.5", + "@netlify/functions": "^1.3.0", "autoprefixer": "^10.2.3", "csso": "^4.2.0", + "date-fns-tz": "^1.3.7", "eleventy-plugin-reading-time": "0.0.1", "eleventy-plugin-webmentions": "^1.1.0", "inquirer": "^8.0.0", @@ -426,6 +429,24 @@ "integrity": "sha512-trnsAYxU3xnS1gPHPyU961coFyLkh4gAD/0zQ5mymY4yOZ+CYvsPqUbOFSw0aDM4y0tV7tiFxL/1XfXPNC6IPg==", "dev": true }, + "node_modules/@netlify/functions": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/@netlify/functions/-/functions-1.3.0.tgz", + "integrity": "sha512-hN/Fgpz8XIOBfsBPLYUMxVKBlCopgeqGB0popayicnmkFLnvKByTTMYgF01wcF9DBtBQdV0H2h1kPFpMl34I8w==", + "dev": true, + "dependencies": { + "is-promise": "^4.0.0" + }, + "engines": { + "node": ">=8.3.0" + } + }, + "node_modules/@netlify/functions/node_modules/is-promise": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/is-promise/-/is-promise-4.0.0.tgz", + "integrity": "sha512-hvpoI6korhJMnej285dSg6nu1+e6uxs7zG3BYAm5byqDsgJNWwxzM6z6iZiAgQR4TJ30JmBTOwqZUw3WlyH3AQ==", + "dev": true + }, "node_modules/@nodelib/fs.scandir": { "version": "2.1.4", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.4.tgz", @@ -1931,6 +1952,29 @@ "node": ">=10" } }, + "node_modules/date-fns": { + "version": "2.29.3", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.29.3.tgz", + "integrity": "sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA==", + "dev": true, + "peer": true, + "engines": { + "node": ">=0.11" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/date-fns" + } + }, + "node_modules/date-fns-tz": { + "version": "1.3.7", + "resolved": "https://registry.npmjs.org/date-fns-tz/-/date-fns-tz-1.3.7.tgz", + "integrity": "sha512-1t1b8zyJo+UI8aR+g3iqr5fkUHWpd58VBx8J/ZSQ+w7YrGlw80Ag4sA86qkfCXRBLmMc4I2US+aPMd4uKvwj5g==", + "dev": true, + "peerDependencies": { + "date-fns": ">=2.0.0" + } + }, "node_modules/dateformat": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/dateformat/-/dateformat-3.0.3.tgz", @@ -31313,6 +31357,23 @@ "integrity": "sha512-trnsAYxU3xnS1gPHPyU961coFyLkh4gAD/0zQ5mymY4yOZ+CYvsPqUbOFSw0aDM4y0tV7tiFxL/1XfXPNC6IPg==", "dev": true }, + "@netlify/functions": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/@netlify/functions/-/functions-1.3.0.tgz", + "integrity": "sha512-hN/Fgpz8XIOBfsBPLYUMxVKBlCopgeqGB0popayicnmkFLnvKByTTMYgF01wcF9DBtBQdV0H2h1kPFpMl34I8w==", + "dev": true, + "requires": { + "is-promise": "^4.0.0" + }, + "dependencies": { + "is-promise": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/is-promise/-/is-promise-4.0.0.tgz", + "integrity": "sha512-hvpoI6korhJMnej285dSg6nu1+e6uxs7zG3BYAm5byqDsgJNWwxzM6z6iZiAgQR4TJ30JmBTOwqZUw3WlyH3AQ==", + "dev": true + } + } + }, "@nodelib/fs.scandir": { "version": "2.1.4", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.4.tgz", @@ -32517,6 +32578,20 @@ "whatwg-url": "^8.0.0" } }, + "date-fns": { + "version": "2.29.3", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.29.3.tgz", + "integrity": "sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA==", + "dev": true, + "peer": true + }, + "date-fns-tz": { + "version": "1.3.7", + "resolved": "https://registry.npmjs.org/date-fns-tz/-/date-fns-tz-1.3.7.tgz", + "integrity": "sha512-1t1b8zyJo+UI8aR+g3iqr5fkUHWpd58VBx8J/ZSQ+w7YrGlw80Ag4sA86qkfCXRBLmMc4I2US+aPMd4uKvwj5g==", + "dev": true, + "requires": {} + }, "dateformat": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/dateformat/-/dateformat-3.0.3.tgz", diff --git a/package.json b/package.json index fb7de19..b5c7ab8 100644 --- a/package.json +++ b/package.json @@ -29,8 +29,11 @@ "@11ty/eleventy-img": "^2.0.1", "@11ty/eleventy-plugin-rss": "^1.1.0", "@11ty/eleventy-plugin-syntaxhighlight": "^3.0.6", + "@iarna/toml": "^2.2.5", + "@netlify/functions": "^1.3.0", "autoprefixer": "^10.2.3", "csso": "^4.2.0", + "date-fns-tz": "^1.3.7", "eleventy-plugin-reading-time": "0.0.1", "eleventy-plugin-webmentions": "^1.1.0", "inquirer": "^8.0.0", diff --git a/src/_data/site.js b/src/_data/site.js index fe97180..7f41962 100644 --- a/src/_data/site.js +++ b/src/_data/site.js @@ -3,6 +3,11 @@ module.exports = () => { const month = today.getMonth(); const day = today.getDate(); + const nextCssNakedDay = new Date(`${today.getFullYear()}-04-09 00:00:00`); + if (nextCssNakedDay < today) { + nextCssNakedDay.setFullYear(nextCssNakedDay.getFullYear() + 1); + } + return { name: "Luke Bonaccorsi", description: @@ -14,5 +19,11 @@ module.exports = () => { maxPostsPerPage: 10, webmentionToken: "pDjIX81PRC-fGTpGYOXOMQ", isCssNakedDay: month === 3 && day === 9, + timezone: "Europe/London", + publishTime: "10:30", + rebuildDates: [ + nextCssNakedDay, + new Date(`${today.getFullYear() + 1}-01-01 00:00:00`), // New Year's Day + ], }; }; diff --git a/src/_scripts/new.js b/src/_scripts/new.js index 5cc3099..84e5640 100644 --- a/src/_scripts/new.js +++ b/src/_scripts/new.js @@ -2,10 +2,16 @@ const inquirer = require("inquirer"); const slugify = require("slugify"); const fs = require("fs").promises; const path = require("path"); +const config = require("../_data/site")(); inquirer.registerPrompt("datetime", require("inquirer-datepicker-prompt")); async function run() { + const [postHours, postMinutes] = config.publishTime.split(":"); + const dateInitial = new Date(); + dateInitial.setHours(postHours); + dateInitial.setMinutes(postMinutes); + const answers = await inquirer.prompt([ { type: "list", @@ -21,8 +27,9 @@ async function run() { { type: "datetime", name: "date", - message: "What is the publish date?", - format: ["yyyy", "-", "mm", "-", "dd"], + message: "What is the publish date/time?", + format: ["yyyy", "-", "mm", "-", "dd", " ", "HH", ":", "MM"], + initial: dateInitial, when: (answers) => { return answers.type === "Post"; }, @@ -59,10 +66,12 @@ async function run() { ); file = file.replace("Default title", answers.title); - file = file.replace( - "1970-01-01", - answers.date.toLocaleDateString("en-CA", { timeZone: "Europe/London" }) - ); + + const formattedDate = `${answers.date.getFullYear()}-${ + answers.date.getMonth() + 1 + }-${answers.date.getDate()} ${answers.date.getHours()}:${answers.date.getMinutes()}:00`; + + file = file.replace("1970-01-01 00:00:00", formattedDate); const filename = slug(answers.title); await fs.writeFile( diff --git a/src/_scripts/templates/post.md b/src/_scripts/templates/post.md index 552e9b6..f57a92b 100644 --- a/src/_scripts/templates/post.md +++ b/src/_scripts/templates/post.md @@ -1,6 +1,6 @@ --- title: Default title -date: 1970-01-01 10:30:00 +date: 1970-01-01 00:00:00 tags: --- diff --git a/src/pages/nextBuild.11ty.js b/src/pages/nextBuild.11ty.js new file mode 100644 index 0000000..e795859 --- /dev/null +++ b/src/pages/nextBuild.11ty.js @@ -0,0 +1,39 @@ +class NextBuild { + data() { + return { + permalink: "/nextbuild.json", + }; + } + dateToCron(date) { + const minutes = date.getMinutes(); + const hours = date.getHours(); + const days = date.getDate(); + const months = date.getMonth() + 1; + const dayOfWeek = date.getDay(); + + return `${minutes} ${hours} ${days} ${months} ${dayOfWeek}`; + } + + render({ collections, site }) { + const nextYear = new Date(); + nextYear.setFullYear(nextYear.getFullYear() + 1); + nextYear.setHours(0); + nextYear.setMinutes(0); + nextYear.setSeconds(0); + + const postDates = collections.futurePosts.map((post) => { + return post.date; + }); + + postDates.push(...site.rebuildDates, nextYear); + const filteredPostDates = postDates.filter((date) => date <= nextYear); + filteredPostDates.sort((a, b) => a - b); + + return JSON.stringify({ + date: postDates[0], + cron: this.dateToCron(postDates[0]), + }); + } +} + +module.exports = NextBuild; diff --git a/src/posts/future-post-test.md b/src/posts/future-post-test.md new file mode 100644 index 0000000..5b354f1 --- /dev/null +++ b/src/posts/future-post-test.md @@ -0,0 +1,7 @@ +--- +title: Future post test +date: 2022-12-25 10:30:00 +tags: +--- + + From c1b45d8693eae359202bef24297e1792def2d0f7 Mon Sep 17 00:00:00 2001 From: Luke Bonaccorsi Date: Mon, 28 Nov 2022 13:06:50 +0000 Subject: [PATCH 2/4] Add missing dep --- package-lock.json | 5 ++--- package.json | 1 + 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index b9f842c..cc0af86 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,7 @@ "@netlify/functions": "^1.3.0", "autoprefixer": "^10.2.3", "csso": "^4.2.0", + "date-fns": "^2.29.3", "date-fns-tz": "^1.3.7", "eleventy-plugin-reading-time": "0.0.1", "eleventy-plugin-webmentions": "^1.1.0", @@ -1957,7 +1958,6 @@ "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.29.3.tgz", "integrity": "sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA==", "dev": true, - "peer": true, "engines": { "node": ">=0.11" }, @@ -32582,8 +32582,7 @@ "version": "2.29.3", "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.29.3.tgz", "integrity": "sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA==", - "dev": true, - "peer": true + "dev": true }, "date-fns-tz": { "version": "1.3.7", diff --git a/package.json b/package.json index b5c7ab8..354290e 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "@netlify/functions": "^1.3.0", "autoprefixer": "^10.2.3", "csso": "^4.2.0", + "date-fns": "^2.29.3", "date-fns-tz": "^1.3.7", "eleventy-plugin-reading-time": "0.0.1", "eleventy-plugin-webmentions": "^1.1.0", From 9bf23bc486d38ba92c6e91be3850216f8b0582bb Mon Sep 17 00:00:00 2001 From: Luke Bonaccorsi Date: Mon, 28 Nov 2022 13:10:56 +0000 Subject: [PATCH 3/4] testing --- netlify/functions/build.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netlify/functions/build.js b/netlify/functions/build.js index cfca1a4..b6c10b4 100644 --- a/netlify/functions/build.js +++ b/netlify/functions/build.js @@ -1,4 +1,4 @@ -const { schedule } = require("@netlify/functions"); +// const { schedule } = require("@netlify/functions"); const { cron } = require("../../dist/nextbuild.json"); exports.handler = async function (event, context) { From 826ab6a059c32358df01d5df985480fb86b27c67 Mon Sep 17 00:00:00 2001 From: Luke Bonaccorsi Date: Mon, 28 Nov 2022 13:38:29 +0000 Subject: [PATCH 4/4] testing build hook --- netlify/functions/build.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/netlify/functions/build.js b/netlify/functions/build.js index b6c10b4..bf8273a 100644 --- a/netlify/functions/build.js +++ b/netlify/functions/build.js @@ -1,9 +1,13 @@ -// const { schedule } = require("@netlify/functions"); -const { cron } = require("../../dist/nextbuild.json"); +const { request } = require("node:https"); +const { schedule } = require("@netlify/functions"); +// const { cron } = require("../../dist/nextbuild.json"); -exports.handler = async function (event, context) { - return { - statusCode: 200, - body: JSON.stringify({ message: cron }), +exports.handler = schedule("40 13 28 11 1", (event, context) => { + const options = { + hostname: "api.netlify.com", + path: `/build_hooks/${process.env.BUILD_HOOK}`, + method: "POST", }; -}; + + request(options); +});