chore: formatting

This commit is contained in:
Cory Dransfeldt 2024-10-19 19:56:23 -07:00
parent 55286f8c3b
commit 2fb4c362f3
No known key found for this signature in database
2 changed files with 177 additions and 143 deletions

View file

@ -1,107 +1,116 @@
import { createRequire } from 'module'
import dotenvFlow from 'dotenv-flow'
import filters from './config/filters/index.js'
import htmlmin from 'html-minifier-terser'
import markdownIt from 'markdown-it'
import markdownItAnchor from 'markdown-it-anchor'
import markdownItFootnote from 'markdown-it-footnote'
import markdownItPrism from 'markdown-it-prism'
import syntaxHighlight from '@11ty/eleventy-plugin-syntaxhighlight'
import tablerIcons from '@cdransf/eleventy-plugin-tabler-icons'
import { copyErrorPages, minifyJsComponents } from './config/events/index.js'
import { albumReleasesCalendar } from './config/collections/index.js'
import { cssConfig } from './config/plugins/css-config.js'
import { createRequire } from "module";
import dotenvFlow from "dotenv-flow";
import filters from "./config/filters/index.js";
import htmlmin from "html-minifier-terser";
import markdownIt from "markdown-it";
import markdownItAnchor from "markdown-it-anchor";
import markdownItFootnote from "markdown-it-footnote";
import markdownItPrism from "markdown-it-prism";
import syntaxHighlight from "@11ty/eleventy-plugin-syntaxhighlight";
import tablerIcons from "@cdransf/eleventy-plugin-tabler-icons";
import { copyErrorPages, minifyJsComponents } from "./config/events/index.js";
import { albumReleasesCalendar } from "./config/collections/index.js";
import { cssConfig } from "./config/plugins/css-config.js";
// load .env
dotenvFlow.config()
dotenvFlow.config();
// get app version
const require = createRequire(import.meta.url)
const appVersion = require('./package.json').version
const require = createRequire(import.meta.url);
const appVersion = require("./package.json").version;
export default async function (eleventyConfig) {
eleventyConfig.addPlugin(syntaxHighlight)
eleventyConfig.addPlugin(tablerIcons)
eleventyConfig.addPlugin(syntaxHighlight);
eleventyConfig.addPlugin(tablerIcons);
if (process.env.ELEVENTY_PRODUCTION) eleventyConfig.addPlugin(cssConfig)
if (process.env.ELEVENTY_PRODUCTION) eleventyConfig.addPlugin(cssConfig);
eleventyConfig.setServerOptions({ domdiff: false })
eleventyConfig.setWatchThrottleWaitTime(200)
eleventyConfig.setQuietMode(true)
eleventyConfig.configureErrorReporting({ allowMissingExtensions: true })
eleventyConfig.setServerOptions({ domdiff: false });
eleventyConfig.setWatchThrottleWaitTime(200);
eleventyConfig.setQuietMode(true);
eleventyConfig.configureErrorReporting({ allowMissingExtensions: true });
eleventyConfig.setLiquidOptions({
jsTruthy: true,
})
});
eleventyConfig.addPassthroughCopy('src/assets')
eleventyConfig.addPassthroughCopy('_redirects')
eleventyConfig.addPassthroughCopy('_headers')
eleventyConfig.addPassthroughCopy("src/assets");
eleventyConfig.addPassthroughCopy("_redirects");
eleventyConfig.addPassthroughCopy("_headers");
eleventyConfig.addPassthroughCopy({
'node_modules/@cdransf/api-text/api-text.js': 'assets/scripts/components/api-text.js',
'node_modules/@cdransf/select-pagination/select-pagination.js': 'assets/scripts/components/select-pagination.js',
'node_modules/@cdransf/theme-toggle/theme-toggle.js': 'assets/scripts/components/theme-toggle.js',
'node_modules/@daviddarnes/mastodon-post/mastodon-post.js': 'assets/scripts/components/mastodon-post.js',
'node_modules/minisearch/dist/umd/index.js': 'assets/scripts/components/minisearch.js',
'node_modules/youtube-video-element/youtube-video-element.js': 'assets/scripts/components/youtube-video-element.js'
})
"node_modules/@cdransf/api-text/api-text.js":
"assets/scripts/components/api-text.js",
"node_modules/@cdransf/select-pagination/select-pagination.js":
"assets/scripts/components/select-pagination.js",
"node_modules/@cdransf/theme-toggle/theme-toggle.js":
"assets/scripts/components/theme-toggle.js",
"node_modules/@daviddarnes/mastodon-post/mastodon-post.js":
"assets/scripts/components/mastodon-post.js",
"node_modules/minisearch/dist/umd/index.js":
"assets/scripts/components/minisearch.js",
"node_modules/youtube-video-element/youtube-video-element.js":
"assets/scripts/components/youtube-video-element.js",
});
eleventyConfig.addCollection('albumReleasesCalendar', albumReleasesCalendar)
eleventyConfig.addCollection("albumReleasesCalendar", albumReleasesCalendar);
const md = markdownIt({ html: true, linkify: true })
const md = markdownIt({ html: true, linkify: true });
md.use(markdownItAnchor, {
level: [1, 2],
permalink: markdownItAnchor.permalink.headerLink({
safariReaderFix: true,
}),
})
md.use(markdownItFootnote)
md.use(markdownItPrism)
eleventyConfig.setLibrary('md', md)
});
md.use(markdownItFootnote);
md.use(markdownItPrism);
eleventyConfig.setLibrary("md", md);
eleventyConfig.addLiquidFilter('markdown', (content) => {
if (!content) return
return md.render(content)
})
eleventyConfig.addLiquidFilter("markdown", (content) => {
if (!content) return;
return md.render(content);
});
Object.keys(filters).forEach((filterName) => {
eleventyConfig.addLiquidFilter(filterName, filters[filterName])
})
eleventyConfig.addLiquidFilter(filterName, filters[filterName]);
});
eleventyConfig.addShortcode('appVersion', () => appVersion)
eleventyConfig.addShortcode("appVersion", () => appVersion);
// events
if (process.env.ELEVENTY_PRODUCTION) eleventyConfig.on('afterBuild', copyErrorPages)
if (process.env.ELEVENTY_PRODUCTION) eleventyConfig.on('afterBuild', minifyJsComponents)
if (process.env.ELEVENTY_PRODUCTION)
eleventyConfig.on("afterBuild", copyErrorPages);
if (process.env.ELEVENTY_PRODUCTION)
eleventyConfig.on("afterBuild", minifyJsComponents);
// transforms
if (process.env.ELEVENTY_PRODUCTION) eleventyConfig.addTransform('html-minify', (content, path) => {
if (path && path.endsWith('.html')) {
return htmlmin.minify(content, {
collapseBooleanAttributes: true,
collapseWhitespace: true,
decodeEntities: true,
includeAutoGeneratedTags: false,
minifyCSS: true,
minifyJS: true,
minifyURLs: true,
noNewlinesBeforeTagClose: true,
quoteCharacter: '"',
removeComments: true,
sortAttributes: true,
sortClassName: true,
useShortDoctype: true,
})
}
return content
})
if (process.env.ELEVENTY_PRODUCTION)
eleventyConfig.addTransform("html-minify", (content, path) => {
if (path && path.endsWith(".html")) {
return htmlmin.minify(content, {
collapseBooleanAttributes: true,
collapseWhitespace: true,
decodeEntities: true,
includeAutoGeneratedTags: false,
minifyCSS: true,
minifyJS: true,
minifyURLs: true,
noNewlinesBeforeTagClose: true,
quoteCharacter: '"',
removeComments: true,
sortAttributes: true,
sortClassName: true,
useShortDoctype: true,
});
}
return content;
});
return {
passthroughFileCopy: true,
dir: {
input: 'src',
includes: 'includes',
data: 'data',
output: '_site',
input: "src",
includes: "includes",
data: "data",
output: "_site",
},
}
}
};
}

161
server.js
View file

@ -1,60 +1,79 @@
import express from 'express'
import { createProxyMiddleware } from 'http-proxy-middleware'
import path from 'path'
import { fileURLToPath } from 'url'
import { spawn } from 'child_process'
import express from "express";
import { createProxyMiddleware } from "http-proxy-middleware";
import path from "path";
import { fileURLToPath } from "url";
import { spawn } from "child_process";
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const PORT = 8080
const ELEVENTY_PORT = 8081
const WORKER_PORT = 8787
const WORKER_ENTRY = './workers/dynamic-pages/index.js'
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const PORT = 8080;
const ELEVENTY_PORT = 8081;
const WORKER_PORT = 8787;
const WORKER_ENTRY = "./workers/dynamic-pages/index.js";
const startProcess = (command, args, name) => {
console.log(`Starting ${name}...`)
const process = spawn(command, args, { stdio: 'inherit' })
console.log(`Starting ${name}...`);
const process = spawn(command, args, { stdio: "inherit" });
process.on('error', err => {
console.error(`${name} error: ${err.message}`)
process.exit(1)
})
process.on("error", (err) => {
console.error(`${name} error: ${err.message}`);
process.exit(1);
});
process.on('exit', code => {
console.log(`${name} exited with code ${code}`)
if (code !== 0) process.exit(code)
})
process.on("exit", (code) => {
console.log(`${name} exited with code ${code}`);
if (code !== 0) process.exit(code);
});
return process
}
return process;
};
const startEleventy = () =>
startProcess('npx', ['eleventy', '--serve', '--port', ELEVENTY_PORT], 'Eleventy')
startProcess(
"npx",
["eleventy", "--serve", "--port", ELEVENTY_PORT],
"Eleventy"
);
const startWorker = () =>
startProcess('npx', ['wrangler', 'dev', WORKER_ENTRY, '--port', WORKER_PORT], 'Wrangler Worker')
startProcess(
"npx",
["wrangler", "dev", WORKER_ENTRY, "--port", WORKER_PORT],
"Wrangler Worker"
);
const app = express()
const app = express();
app.use((req, res, next) => {
if (req.path === '/js/script.js') {
res.setHeader('Content-Type', 'application/javascript')
return res.send('')
if (req.path === "/js/script.js") {
res.setHeader("Content-Type", "application/javascript");
return res.send("");
}
if (req.path.endsWith('.css')) res.setHeader('Content-Type', 'text/css')
else if (req.path.endsWith('.js')) res.setHeader('Content-Type', 'application/javascript')
else if (req.path.endsWith('.json')) res.setHeader('Content-Type', 'application/json')
else if (req.path.startsWith('/api/')) res.setHeader('Content-Type', 'application/json')
else if (req.path.startsWith('/feeds/all')) res.setHeader('Content-Type', 'application/xml')
else if (req.path.startsWith('/feeds/books')) res.setHeader('Content-Type', 'application/xml')
else if (req.path.startsWith('/feeds/links')) res.setHeader('Content-Type', 'application/xml')
else if (req.path.startsWith('/feeds/movies')) res.setHeader('Content-Type', 'application/xml')
else if (req.path.startsWith('/feeds/posts')) res.setHeader('Content-Type', 'application/xml')
else if (req.path.startsWith('/feeds/syndication')) res.setHeader('Content-Type', 'application/xml')
next()
})
if (req.path.endsWith(".css")) res.setHeader("Content-Type", "text/css");
else if (req.path.endsWith(".js"))
res.setHeader("Content-Type", "application/javascript");
else if (req.path.endsWith(".json"))
res.setHeader("Content-Type", "application/json");
else if (req.path.startsWith("/api/"))
res.setHeader("Content-Type", "application/json");
else if (req.path.startsWith("/feeds/all"))
res.setHeader("Content-Type", "application/xml");
else if (req.path.startsWith("/feeds/books"))
res.setHeader("Content-Type", "application/xml");
else if (req.path.startsWith("/feeds/links"))
res.setHeader("Content-Type", "application/xml");
else if (req.path.startsWith("/feeds/movies"))
res.setHeader("Content-Type", "application/xml");
else if (req.path.startsWith("/feeds/posts"))
res.setHeader("Content-Type", "application/xml");
else if (req.path.startsWith("/feeds/syndication"))
res.setHeader("Content-Type", "application/xml");
next();
});
app.use(express.static(path.join(__dirname, '_site'), { extensions: ['html'] }))
app.use(
express.static(path.join(__dirname, "_site"), { extensions: ["html"] })
);
const proxy = createProxyMiddleware({
target: `http://localhost:${WORKER_PORT}`,
@ -62,48 +81,54 @@ const proxy = createProxyMiddleware({
ws: true,
pathRewrite: (path, req) => req.originalUrl,
onError: (err, req, res) => {
console.error(`Proxy error: ${err.message}`)
res.status(504).send('Worker timeout or unreachable')
console.error(`Proxy error: ${err.message}`);
res.status(504).send("Worker timeout or unreachable");
},
})
});
app.use(
['/watching/movies', '/watching/shows', '/music/artists', '/music/genres', '/books'],
[
"/watching/movies",
"/watching/shows",
"/music/artists",
"/music/genres",
"/books",
],
proxy
)
);
app.use((req, res) => {
res.status(404).sendFile(path.join(__dirname, '_site', '404.html'), err => {
if (err) res.status(404).send('Page not found')
})
})
res.status(404).sendFile(path.join(__dirname, "_site", "404.html"), (err) => {
if (err) res.status(404).send("Page not found");
});
});
const startServer = () => {
const server = app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`)
})
console.log(`Server running at http://localhost:${PORT}`);
});
const shutdown = () => {
console.log('Shutting down...')
console.log("Shutting down...");
server.close(() => {
console.log('Express server closed')
process.exit(0)
})
}
console.log("Express server closed");
process.exit(0);
});
};
process.on('SIGINT', shutdown)
process.on('SIGTERM', shutdown)
}
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
};
const initialize = async () => {
try {
startEleventy()
startWorker()
startServer()
startEleventy();
startWorker();
startServer();
} catch (err) {
console.error(`Initialization error: ${err.message}`)
process.exit(1)
console.error(`Initialization error: ${err.message}`);
process.exit(1);
}
}
};
initialize()
initialize();