just some code cleanup and organization

This commit is contained in:
Adam Matthiesen 2024-01-21 06:57:59 -08:00
parent 915ed14396
commit 7391509587
34 changed files with 119 additions and 102 deletions

View File

@ -6,24 +6,40 @@ import { loadEnv } from 'vite';
import { fromZodError } from "zod-validation-error";
import { viteGhostCMS } from "./src/utils/virtual-imports";
// LOAD ENVIRONMENT VARIABLES
const mode = 'all';
const prefixes = 'CONTENT_API';
const env = loadEnv(mode, process.cwd(), prefixes);
/** INTERNAL CONSTANTS */
const IC = {
/** INTERNAL PACKAGE NAME */
PKG:'@matthiesenxyz/astro-ghostcms',
/** INTERNAL STRING */
CHECK_ENV:"Checking for Environment Variables...",
/** SET ENV GRABBER MODE */
MODE: 'all',
/** SET ENV GRABBER PREFIXES */
PREFIXES: 'CONTENT_API',
/** INTERNAL STRING */
KEY_MISSING:"CONTENT_API_KEY Missing from .env",
/** INTERNAL STRING */
URL_MISSING:"CONTENT_API_URL Missing from .env",
/** INTERNAL STRING */
IT:"Injecting Theme: ",
/** INTERNAL STRING */
IR:"Injecting Routes...",
/** INTERNAL STRING */
IRD:"Route Injection Disabled - Skipping...",
/** INTERNAL STRING */
IIR:"Injecting Integration Route: ",
/** INTERNAL STRING */
II:"Injecting Integration: ",
/** INTERNAL STRING */
AIbU:"Already Imported by User: ",
/** INTERNAL STRING */
CF:"Checking for ",
/** INTERNAL STRING */
CONFSETUPDONE:"GhostCMS Injection Complete. Integration is now ready.",
}
// INTERNAL CONSTANTS
const pkg = '@matthiesenxyz/astro-ghostcms';
const CHECK_ENV = "Checking for Environment Variables...";
const KEY_MISSING = "CONTENT_API_KEY Missing from .env";
const URL_MISSING = "CONTENT_API_URL Missing from .env";
const IT = "Injecting Theme: "
const IR = "Injecting Routes...";
const IRD = "Route Injection Disabled - Skipping...";
const IIR = "Injecting Integration Route: "
const II = "Injecting Integration: ";
const AIbU = "Already Imported by User: ";
const CF = "Checking for ";
const CONFSETUPDONE = "GhostCMS Injection Complete. Integration is now ready."
/** CONTENT API ENVIRONMENT VARIABLES */
const ENV = loadEnv(IC.MODE, process.cwd(), IC.PREFIXES);
/** Astro-GhostCMS Integration
* @ For more information and to see the docs check
@ -31,7 +47,7 @@ const CONFSETUPDONE = "GhostCMS Injection Complete. Integration is now ready."
*/
export default function GhostCMS(options: UserConfig): AstroIntegration {
return {
name: pkg,
name: IC.PKG,
hooks: {
'astro:config:setup': async ({
injectRoute,
@ -47,36 +63,39 @@ export default function GhostCMS(options: UserConfig): AstroIntegration {
logger.error(`Config Error - ${ validationError }`);
throw validationError;
}
/** INTERNAL USERCONFIG ALIAS */
const uconf = o.data;
/** CONFIG OPTION: ROUTE INJECTION */
const injection = uconf.disableRouteInjection;
/** CONFIG OPTION: THEME */
const entry = uconf.theme;
/** CONFIG OPTION: CONSOLE OUTPUT */
const logs = uconf.disableConsoleOutput;
// Check For ENV Variables
if(!logs) {logger.info(CHECK_ENV)}
if(!logs) {logger.info(IC.CHECK_ENV)}
// CHECK FOR API KEY
if(env.CONTENT_API_KEY === undefined){
logger.error(KEY_MISSING);
throw KEY_MISSING;
if(ENV.CONTENT_API_KEY === undefined){
logger.error(IC.KEY_MISSING);
throw IC.KEY_MISSING;
}
// CHECK FOR API URL
if(env.CONTENT_API_URL === undefined){
logger.error(URL_MISSING);
throw URL_MISSING;
if(ENV.CONTENT_API_URL === undefined){
logger.error(IC.URL_MISSING);
throw IC.URL_MISSING;
}
if(!injection){
// THEME SELECTOR
if (entry === pkg) {
if(!logs) {logger.info(IT + pkg)}
if (entry === IC.PKG) {
if(!logs) {logger.info(IC.IT + IC.PKG)}
} else {
if(!logs) {logger.info(IT + entry)}
if(!logs) {logger.info(IC.IT + entry)}
}
// INJECT ROUTES
if(!logs) {logger.info(IR)}
if(!logs) {logger.info(IC.IR)}
injectRoute({
pattern: '/',
@ -118,35 +137,35 @@ export default function GhostCMS(options: UserConfig): AstroIntegration {
entrypoint: `${entry}/archives/[...page].astro`
});
} else {
if(!logs) {logger.info(IRD)}
if(!logs) {logger.info(IC.IRD)}
}
// IMPORT INTEGRATIONS & INTEGRATION ROUTES
const int = [...config.integrations];
// IMPORT INTEGRATION: @ASTROJS/RSS
if(!logs) {logger.info(IIR + "@astrojs/rss")}
if(!logs) {logger.info(IC.IIR + "@astrojs/rss")}
injectRoute({
pattern: '/rss.xml',
entrypoint: `${pkg}/rss.xml.js`
entrypoint: `${IC.PKG}/rss.xml.js`
});
// IMPORT INTEGRATION: @ASTROJS/SITEMAP
if(!logs) {logger.info(CF + "@astrojs/sitemap")}
if(!logs) {logger.info(IC.CF + "@astrojs/sitemap")}
if (!int.find(({ name }) => name === '@astrojs/sitemap')) {
if(!logs) {logger.info(II + "@astrojs/sitemap")}
if(!logs) {logger.info(IC.II + "@astrojs/sitemap")}
int.push(ghostSitemap(uconf));
} else {
if(!logs) {logger.info(AIbU + "@astrojs/sitemap")}
if(!logs) {logger.info(IC.AIbU + "@astrojs/sitemap")}
};
// IMPORT INTEGRATION: ASTRO-ROBOTS-TXT
if(!logs) {logger.info(CF + "astro-robots-txt")}
if(!logs) {logger.info(IC.CF + "astro-robots-txt")}
if (!int.find(({ name }) => name === 'astro-robots-txt')) {
if(!logs) {logger.info(II + "astro-robots-txt")}
if(!logs) {logger.info(IC.II + "astro-robots-txt")}
int.push(ghostRobots(uconf));
} else {
if(!logs) {logger.info(AIbU + "astro-robots-txt")}
if(!logs) {logger.info(IC.AIbU + "astro-robots-txt")}
};
try {updateConfig({
integrations: [
@ -161,7 +180,7 @@ export default function GhostCMS(options: UserConfig): AstroIntegration {
},
'astro:config:done': async ({ logger }) => {
logger.info(CONFSETUPDONE);
logger.info(IC.CONFSETUPDONE);
}
}
}

View File

@ -1,7 +1,7 @@
{
"name": "@matthiesenxyz/astro-ghostcms",
"description": "Astro GhostCMS integration to allow easier importing of GhostCMS Content",
"version": "2.1.7",
"version": "2.1.8",
"author": "MatthiesenXYZ (https://matthiesen.xyz)",
"type": "module",
"license": "MIT",
@ -17,15 +17,15 @@
"exports": {
".": "./index.ts",
"./api": "./src/api/index.ts",
"./index.astro": "./src/routes/index.astro",
"./404.astro": "./src/routes/404.astro",
"./[slug].astro": "./src/routes/[slug].astro",
"./tags.astro": "./src/routes/tags.astro",
"./authors.astro": "./src/routes/authors.astro",
"./tag/[slug].astro": "./src/routes/tag/[slug].astro",
"./author/[slug].astro": "./src/routes/author/[slug].astro",
"./archives/[...page].astro": "./src/routes/archives/[...page].astro",
"./rss.xml.js": "./src/routes/rss.xml.js"
"./index.astro": "./src/theme/routes/index.astro",
"./404.astro": "./src/theme/routes/404.astro",
"./[slug].astro": "./src/theme/routes/[slug].astro",
"./tags.astro": "./src/theme/routes/tags.astro",
"./authors.astro": "./src/theme/routes/authors.astro",
"./tag/[slug].astro": "./src/theme/routes/tag/[slug].astro",
"./author/[slug].astro": "./src/theme/routes/author/[slug].astro",
"./archives/[...page].astro": "./src/theme/routes/archives/[...page].astro",
"./rss.xml.js": "./src/theme/routes/rss.xml.js"
},
"main": "index.ts",
"types": "src/api/tryghost-content-api.d.ts",
@ -57,25 +57,23 @@
},
"dependencies": {
"@astrojs/check": "^0.3.4",
"typescript": "^5.3.3",
"zod-validation-error": "^3.0.0",
"axios": "^1.0.0",
"astro-font": "^0.0.72",
"@astrojs/renderer-svelte": "0.5.2",
"@astrojs/rss": "^4.0.2",
"@astrojs/sitemap": "^3.0.4",
"@astrojs/sitemap": "^3.0.5",
"@typescript-eslint/eslint-plugin": "^6.19.0",
"@typescript-eslint/parser": "^6.19.0",
"astro": "^4.2.1",
"astro-font": "^0.0.72",
"astro-robots-txt": "^1.0.0",
"@snowpack/plugin-dotenv": "^2.2.0",
"@typescript-eslint/eslint-plugin": "^6.5.0",
"@typescript-eslint/parser": "^6.5.0",
"astro": "^4.1.2",
"eslint": "^8.48.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-astro": "^0.29.0",
"prettier": "^3.0.3",
"prettier-plugin-astro": "^0.12.0",
"sass": "^1.66.1",
"axios": "^1.6.5",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-astro": "^0.29.1",
"prettier": "^3.2.4",
"prettier-plugin-astro": "^0.12.3",
"sass": "^1.70.0",
"tiny-invariant": "^1.3.1",
"vite": "^4.4.9"
"typescript": "^5.3.3",
"vite": "^4.5.2",
"zod-validation-error": "^3.0.0"
}
}

View File

@ -25,57 +25,57 @@ const api = GhostContentAPI({ key, url, version });
// SET Include params
const include:ArrayOrValue<IncludeParam> = ['authors', 'tags'];
// Get Posts (General "ALL")
/** Get Posts (General "ALL") */
export const getGhostPosts = async () => {
const ghostPosts:PostsOrPages = await api.posts.browse({include,filter:'visibility:public'})
return ghostPosts; };
// Get Posts (Recent "setLimit?")
/** Get Posts (Recent "setLimit?") */
export const getGhostRecentPosts = async (setLimit?:ArrayOrValue<LimitParam>) => {
const ghostRecentPosts:PostsOrPages = await api.posts.browse({limit:setLimit?setLimit:"6",include,filter:'visibility:public'});
return ghostRecentPosts; };
// Get Posts (Featured "setLimit?")
/** Get Posts (Featured "setLimit?") */
export const getGhostFeaturedPosts = async (setLimit?:ArrayOrValue<LimitParam>) => {
const ghostFeaturedPosts:PostsOrPages = await api.posts.browse({limit:setLimit?setLimit:"1",include,filter:'featured:true'});
return ghostFeaturedPosts; };
// Get Post (By Slug)
/** Get Post (By Slug) */
export const getGhostPostbySlug = async (slug:Nullable<string>) => {
const ghostPostbySlug:PostOrPage = await api.posts.read({slug},{include});
return ghostPostbySlug; };
// Get Post (By Tag)
/** Get Post (By Tag) */
export const getGhostPostsbyTag = async (slug:Nullable<string>) => {
const ghostPostsbyTag:PostsOrPages = await api.posts.browse({filter:`tag:${slug}`,include});
return ghostPostsbyTag; };
// Get Tags (General "ALL")
/** Get Tags (General "ALL") */
export const getGhostTags = async () => {
const ghostTags:Tags = await api.tags.browse({include:`count.posts`});
return ghostTags; };
// Get Tag (By Slug)
/** Get Tag (By Slug) */
export const getGhostTagbySlug = async (slug:Nullable<string>) => {
const ghostTagbySlug:Tag = await api.tags.read({slug},{include:`count.posts`});
return ghostTagbySlug; };
// Get Authors (General "ALL")
/** Get Authors (General "ALL") */
export const getGhostAuthors = async () => {
const ghostAuthors:Authors = await api.authors.browse({include:`count.posts`});
return ghostAuthors; };
// Get Pages (ALL)
/** Get Pages (ALL) */
export const getGhostPages = async () => {
const ghostPages:PostsOrPages = await api.pages.browse();
return ghostPages; };
// Get Page (by Slug)
/** Get Page (by Slug) */
export const getGhostPage = async (slug:Nullable<string>) => {
const ghostPage:PostOrPage = await api.pages.read({slug});
return ghostPage; };
// Get Settings
/** Get Settings */
export const getGhostSettings = async () => {
const ghostSettings:Settings = await api.settings.browse();
return ghostSettings; };

View File

@ -1,6 +1,6 @@
---
import { getGhostImgPath } from "../utils";
import type { Settings, Author } from "../api";
import type { Settings, Author } from "../../api";
export type Props = {
author: Author;
wide?: boolean;

View File

@ -1,6 +1,6 @@
---
import { getGhostImgPath } from "../utils";
import type { Settings, PostOrPage } from "../api";
import type { Settings, PostOrPage } from "../../api";
export type Props = {
post: PostOrPage;
settings: Settings;

View File

@ -1,6 +1,6 @@
---
import { getGhostImgPath } from "../utils";
import type { Settings } from "../api";
import type { Settings } from "../../api";
export type Props = {
image: string;
alt?: string;

View File

@ -1,5 +1,5 @@
---
import type { Settings } from "../api";
import type { Settings } from "../../api";
export type Props = {
settings: Settings;
};

View File

@ -1,5 +1,5 @@
---
import type { Settings } from "../api";
import type { Settings } from "../../api";
export type Props = {
settings: Settings;
};

View File

@ -1,6 +1,6 @@
---
import { getGhostImgPath } from "../utils";
import type { Settings } from "../api";
import type { Settings } from "../../api";
export type Props = {
featureImg?: string;
mainTitle?: string;

View File

@ -1,7 +1,7 @@
---
import Header from "./Header.astro";
import Footer from "./Footer.astro";
import type { Settings } from "../api";
import type { Settings } from "../../api";
export type Props = {
settings: Settings;
};

View File

@ -1,6 +1,6 @@
---
import FeatureImage from "./FeatureImage.astro";
import type { Settings, PostOrPage } from "../api";
import type { Settings, PostOrPage } from "../../api";
export type Props = {
page: PostOrPage;
settings: Settings;

View File

@ -2,7 +2,7 @@
import PostHero from "./PostHero.astro";
import PostFooter from "./PostFooter.astro";
import invariant from "tiny-invariant";
import type {PostOrPage, PostsOrPages, Settings } from "../api";
import type {PostOrPage, PostsOrPages, Settings } from "../../api";
export type Props = {
post: PostOrPage;
settings: Settings;

View File

@ -1,6 +1,6 @@
---
import PostPreview from "./PostPreview.astro";
import type { Settings, PostOrPage, PostsOrPages } from "../api";
import type { Settings, PostOrPage, PostsOrPages } from "../../api";
export type Props = {
post: PostOrPage;
settings: Settings;

View File

@ -2,7 +2,7 @@
import FeatureImage from "./FeatureImage.astro";
import AuthorList from "./AuthorList.astro";
import { formatDate } from "../utils";
import type { Settings, PostOrPage } from "../api";
import type { Settings, PostOrPage } from "../../api";
export type Props = {
post: PostOrPage;
settings: Settings;

View File

@ -1,7 +1,7 @@
---
import { getGhostImgPath, formatDate } from "../utils";
import AuthorList from "./AuthorList.astro";
import type { Settings, PostOrPage, Tag } from "../api";
import type { Settings, PostOrPage, Tag } from "../../api";
export type Props = {
post: PostOrPage;
settings: Settings;

View File

@ -1,6 +1,6 @@
---
import PostPreview from "./PostPreview.astro";
import type { Settings, PostOrPage } from "../api";
import type { Settings, PostOrPage } from "../../api";
export type Props = {
posts: PostOrPage[];
settings: Settings;

View File

@ -1,6 +1,6 @@
---
import { getGhostImgPath } from "../utils";
import type { Settings, Tag } from "../api";
import type { Settings, Tag } from "../../api";
export type Props = {
tag: Tag;
addClass?: string;

View File

@ -1,5 +1,5 @@
---
import type { Settings } from "../api";
import type { Settings } from "../../api";
import { AstroFont } from "astro-font";
import ViewTransitions from "astro/components/ViewTransitions.astro";
import MainLayout from "../components/MainLayout.astro";

View File

@ -3,7 +3,7 @@ import type { InferGetStaticPropsType } from 'astro';
import DefaultPageLayout from "../layouts/default.astro";
import Page from "../components/Page.astro";
import Post from "../components/Post.astro";
import { getGhostSettings, getGhostPages, getGhostPosts } from "../api";
import { getGhostSettings, getGhostPages, getGhostPosts } from "../../api";
import invariant from 'tiny-invariant';
export async function getStaticPaths() {

View File

@ -5,8 +5,7 @@ import DefaultPageLayout from "../../layouts/default.astro";
import PostPreviewList from "../../components/PostPreviewList.astro";
import HeroContent from "../../components/HeroContent.astro";
import Paginator from "../../components/Paginator.astro";
import { getGhostSettings, getGhostPosts } from "../../api";
import type { PostOrPage } from '../../api';
import { getGhostSettings, getGhostPosts, type PostOrPage } from "../../../api";
export async function getStaticPaths({ paginate }:GetStaticPathsOptions) {
const posts = await getGhostPosts();

View File

@ -2,7 +2,7 @@
import type { InferGetStaticParamsType, InferGetStaticPropsType } from 'astro';
import DefaultPageLayout from "../../layouts/default.astro";
import PostPreviewList from "../../components/PostPreviewList.astro";
import { getGhostPosts, getGhostAuthors, getGhostSettings } from "../../api";
import { getGhostPosts, getGhostAuthors, getGhostSettings } from "../../../api";
import invariant from "tiny-invariant";
export async function getStaticPaths() {

View File

@ -1,7 +1,7 @@
---
import DefaultPageLayout from "../layouts/default.astro";
import AuthorCard from "../components/AuthorCard.astro";
import { getGhostAuthors, getGhostSettings } from "../api";
import { getGhostAuthors, getGhostSettings } from "../../api";
import invariant from "tiny-invariant";
let title = "All Authors";

View File

@ -2,7 +2,7 @@
import DefaultPageLayout from "../layouts/default.astro";
import PostPreviewList from "../components/PostPreviewList.astro";
import HeroContent from "../components/HeroContent.astro";
import { getGhostPosts, getGhostSettings } from "../api";
import { getGhostPosts, getGhostSettings } from "../../api";
import invariant from "tiny-invariant";
const posts = await getGhostPosts();
const settings = await getGhostSettings();

View File

@ -1,5 +1,5 @@
import rss from "@astrojs/rss";
import { getGhostPosts, getGhostSettings } from '../api';
import { getGhostPosts, getGhostSettings } from '../../api';
import invariant from "tiny-invariant";
export async function GET(context) {

View File

@ -2,7 +2,7 @@
import type { InferGetStaticParamsType, InferGetStaticPropsType } from 'astro';
import DefaultPageLayout from "../../layouts/default.astro";
import PostPreview from "../../components/PostPreview.astro";
import { getGhostPosts, getGhostTags, getGhostSettings } from "../../api";
import { getGhostPosts, getGhostTags, getGhostSettings } from "../../../api";
import { getGhostImgPath } from "../../utils";
import invariant from "tiny-invariant";

View File

@ -1,7 +1,7 @@
---
import DefaultPageLayout from "../layouts/default.astro";
import TagCard from "../components/TagCard.astro";
import { getGhostSettings, getGhostTags } from "../api";
import { getGhostSettings, getGhostTags } from "../../api";
import invariant from 'tiny-invariant';

View File

@ -24,4 +24,5 @@ export const UserConfigSchema = z.object({
robotstxt: S.RobotsTxtSchema.optional(),
});
/** USER CONFIGURATION SCHEMA */
export type UserConfig = z.infer<typeof UserConfigSchema>