Skip to content

Commit

Permalink
Added optional decapCMSVersion configuration via plugin or env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
dorukgezici committed Sep 28, 2024
1 parent ccdd5df commit 02d15d8
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 16 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,15 @@ On GitHub, go to Settings > Developer Settings > OAuth apps > New OAuth app. Or
```bash
OAUTH_GITHUB_CLIENT_ID=
OAUTH_GITHUB_CLIENT_SECRET=
# optional
PUBLIC_DECAP_CMS_VERSION=
```

## Configuration Options

```js
export interface DecapCMSOptions {
decapCMSVersion?: string;
adminDisabled?: boolean;
adminRoute?: string;
oauthDisabled?: boolean;
Expand All @@ -85,6 +88,7 @@ export interface DecapCMSOptions {
}
const defaultOptions: DecapCMSOptions = {
decapCMSVersion: "3.3.3",
adminDisabled: false,
adminRoute: "/admin",
oauthDisabled: false,
Expand All @@ -93,6 +97,7 @@ const defaultOptions: DecapCMSOptions = {
};
```

To override default version of Decap CMS used, set `PUBLIC_DECAP_CMS_VERSION` env variable (takes precedence) or `decapCMSVersion` in `astro.config.mjs`.
To disable injecting Decap CMS admin route, set `adminDisabled` to `true` in `astro.config.mjs`.
To disable injecting OAuth routes, set `oauthDisabled` to `true` in `astro.config.mjs`.

Expand All @@ -102,7 +107,7 @@ import decapCmsOauth from "astro-decap-cms-oauth";
export default defineConfig({
...,
integrations: [decapCmsOauth({ adminDisabled: true, oauthDisabled: true })],
integrations: [decapCmsOauth({ decapCMSVersion: "3.3.3", adminDisabled: false, oauthDisabled: true })],
output: "server",
});
```
1 change: 1 addition & 0 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default defineConfig({
schema: {
OAUTH_GITHUB_CLIENT_ID: envField.string({ context: "server", access: "secret" }),
OAUTH_GITHUB_CLIENT_SECRET: envField.string({ context: "server", access: "secret" }),
PUBLIC_DECAP_CMS_VERSION: envField.string({ context: "client", access: "public" }),
},
},
},
Expand Down
2 changes: 2 additions & 0 deletions demo/.env.example → demo/.env.template
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
OAUTH_GITHUB_CLIENT_ID=
OAUTH_GITHUB_CLIENT_SECRET=
# optional
PUBLIC_DECAP_CMS_VERSION=
6 changes: 3 additions & 3 deletions demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"build": "pnpm -w build && astro build"
},
"dependencies": {
"@astrojs/node": "^8.2.5",
"@astrojs/vercel": "^7.5.3",
"astro": "^4.6.3",
"@astrojs/node": "^8.3.4",
"@astrojs/vercel": "^7.8.1",
"astro": "^4.15.9",
"astro-decap-cms-oauth": "workspace:*"
}
}
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "astro-decap-cms-oauth",
"version": "0.3.1",
"version": "0.4.0",
"description": "Add Decap CMS’s admin dashboard and a custom OAuth backend to your Astro project",
"keywords": [
"astro-integration",
Expand All @@ -23,22 +23,22 @@
"src",
"README.md"
],
"main": "./dist/astro-decap-cms-oauth.umd.cjs",
"module": "./dist/astro-decap-cms-oauth.js",
"main": "./dist/main.cjs",
"module": "./dist/main.js",
"types": "./dist/astro-decap-cms-oauth.d.ts",
"exports": {
".": {
"types": "./dist/astro-decap-cms-oauth.d.ts",
"import": "./dist/astro-decap-cms-oauth.js",
"require": "./dist/astro-decap-cms-oauth.umd.cjs"
"import": "./dist/main.js",
"require": "./dist/main.cjs"
},
"./src/oauth/callback.ts": "./src/oauth/callback.ts",
"./src/oauth/index.ts": "./src/oauth/index.ts",
"./src/admin.astro": "./src/admin.astro"
},
"scripts": {
"dev": "vite build --watch",
"build": "tsc && vite build",
"build": "pnpm run sync && tsc && vite build",
"prepublishOnly": "pnpm run build",
"sync": "pnpm astro sync"
},
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

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

6 changes: 5 additions & 1 deletion src/admin.astro
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
import { PUBLIC_DECAP_CMS_VERSION } from "astro:env/client";
---

<!doctype html>
<html>
<head>
Expand All @@ -9,6 +13,6 @@
</head>
<body>
<!-- Include the script that builds the page and powers Decap CMS -->
<script src="https://unpkg.com/decap-cms@^3.3.3/dist/decap-cms.js"></script>
<script is:inline src={`https://unpkg.com/decap-cms@^${PUBLIC_DECAP_CMS_VERSION}/dist/decap-cms.js`}></script>
</body>
</html>
23 changes: 21 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { AstroIntegration } from "astro";
import { envField } from "astro/config";

export interface DecapCMSOptions {
decapCMSVersion?: string;
adminDisabled?: boolean;
adminRoute?: string;
oauthDisabled?: boolean;
Expand All @@ -9,6 +11,7 @@ export interface DecapCMSOptions {
}

const defaultOptions: DecapCMSOptions = {
decapCMSVersion: "3.3.3",
adminDisabled: false,
adminRoute: "/admin",
oauthDisabled: false,
Expand All @@ -17,7 +20,7 @@ const defaultOptions: DecapCMSOptions = {
};

export default function decapCMS(options: DecapCMSOptions): AstroIntegration {
const { adminDisabled, adminRoute, oauthDisabled, oauthLoginRoute, oauthCallbackRoute } = {
const { decapCMSVersion, adminDisabled, adminRoute, oauthDisabled, oauthLoginRoute, oauthCallbackRoute } = {
...defaultOptions,
...options,
};
Expand All @@ -29,8 +32,24 @@ export default function decapCMS(options: DecapCMSOptions): AstroIntegration {
return {
name: "astro-decap-cms-oauth",
hooks: {
"astro:config:setup": async ({ injectRoute }) => {
"astro:config:setup": async ({ config, injectRoute, updateConfig }) => {
if (!adminDisabled) {
// apply env schema & version
updateConfig({
experimental: {
env: {
schema: {
...config.experimental.env?.schema,
PUBLIC_DECAP_CMS_VERSION: envField.string({
context: "client",
access: "public",
default: decapCMSVersion,
}),
},
},
},
});

// mount DecapCMS admin route
injectRoute({
pattern: adminRoute,
Expand Down
1 change: 1 addition & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default defineConfig({
name: "AstroDecapCMSOAuth",
fileName: "astro-decap-cms-oauth",
},
ssr: true,
},
plugins: [dts({ rollupTypes: true })],
});

0 comments on commit 02d15d8

Please sign in to comment.