Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing netlify functions #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions netlify/functions/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { request } = require("node:https");
const { schedule } = require("@netlify/functions");
// const { cron } = require("../../dist/nextbuild.json");

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);
});
74 changes: 74 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@
"@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": "^2.29.3",
"date-fns-tz": "^1.3.7",
"eleventy-plugin-reading-time": "0.0.1",
"eleventy-plugin-webmentions": "^1.1.0",
"inquirer": "^8.0.0",
Expand Down
11 changes: 11 additions & 0 deletions src/_data/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
],
};
};
21 changes: 15 additions & 6 deletions src/_scripts/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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";
},
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/_scripts/templates/post.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Default title
date: 1970-01-01 10:30:00
date: 1970-01-01 00:00:00
tags:
---

Expand Down
39 changes: 39 additions & 0 deletions src/pages/nextBuild.11ty.js
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 7 additions & 0 deletions src/posts/future-post-test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: Future post test
date: 2022-12-25 10:30:00
tags:
---

<!-- excerpt -->