From 5291ac39837df039f4ea2abdcb1b618c2b017d24 Mon Sep 17 00:00:00 2001 From: hashcookie Date: Tue, 21 May 2024 21:42:07 +0800 Subject: [PATCH 1/3] feat: Add function to fetch and display Casbin version --- app/components/editor/index.tsx | 19 ++++++++++++++++++- app/utils/getCasbinVersion.js | 6 ++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 app/utils/getCasbinVersion.js diff --git a/app/components/editor/index.tsx b/app/components/editor/index.tsx index b4fc4fa..1e043ba 100755 --- a/app/components/editor/index.tsx +++ b/app/components/editor/index.tsx @@ -32,6 +32,7 @@ import useShareInfo from '@/app/components/editor/hooks/useShareInfo'; import useCopy from '@/app/components/editor/hooks/useCopy'; import useSetupEnforceContext from '@/app/components/editor/hooks/useSetupEnforceContext'; import useIndex from '@/app/components/editor/hooks/useIndex'; +import { getCasbinVersion } from '@/app/utils/getCasbinVersion'; export const EditorScreen = () => { const { @@ -48,6 +49,15 @@ export const EditorScreen = () => { onChange: setEnforceContextDataPersistent, data: enforceContextData, }); + const [casbinVersion, setCasbinVersion] = useState(''); + + useEffect(() => { + const fetchVersion = async () => { + const version = await getCasbinVersion(); + setCasbinVersion(version); + }; + fetchVersion(); + }, []); useEffect(() => { if (modelKind) { @@ -185,7 +195,14 @@ export const EditorScreen = () => {
-
Policy
+
+
Policy
+
+ + Node-Casbin v{casbinVersion} + +
+
{ + const version = packageJson.dependencies.casbin; + return version.replace('^', ''); +}; From 600e4704007aacc90d4a04705c2fe5c8dcad4a42 Mon Sep 17 00:00:00 2001 From: hashcookie Date: Wed, 22 May 2024 09:54:47 +0800 Subject: [PATCH 2/3] refactor: Remove redundant code for fetching casbin version --- app/components/editor/index.tsx | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/app/components/editor/index.tsx b/app/components/editor/index.tsx index 1e043ba..b406a5d 100755 --- a/app/components/editor/index.tsx +++ b/app/components/editor/index.tsx @@ -34,7 +34,7 @@ import useSetupEnforceContext from '@/app/components/editor/hooks/useSetupEnforc import useIndex from '@/app/components/editor/hooks/useIndex'; import { getCasbinVersion } from '@/app/utils/getCasbinVersion'; -export const EditorScreen = () => { +export const EditorScreen = ({ casbinVersion }: { casbinVersion: string }) => { const { modelKind, setModelKind, modelText, setModelText, policy, setPolicy, request, setRequest, echo, setEcho, requestResult, setRequestResult, customConfig, setCustomConfig, share, setShare, @@ -49,15 +49,6 @@ export const EditorScreen = () => { onChange: setEnforceContextDataPersistent, data: enforceContextData, }); - const [casbinVersion, setCasbinVersion] = useState(''); - - useEffect(() => { - const fetchVersion = async () => { - const version = await getCasbinVersion(); - setCasbinVersion(version); - }; - fetchVersion(); - }, []); useEffect(() => { if (modelKind) { From 7af647f6c4db16c1e02a2b82382d78abb4454670 Mon Sep 17 00:00:00 2001 From: hashcookie Date: Wed, 22 May 2024 10:01:52 +0800 Subject: [PATCH 3/3] build: Add GenerateCasbinVersionPlugin to next.config.mjs --- .gitignore | 1 + app/components/editor/index.tsx | 15 ++++++++++++--- app/utils/getCasbinVersion.js | 6 ------ generateCasbinVersionPlugin.js | 33 +++++++++++++++++++++++++++++++++ next.config.mjs | 5 +++++ 5 files changed, 51 insertions(+), 9 deletions(-) delete mode 100644 app/utils/getCasbinVersion.js create mode 100644 generateCasbinVersionPlugin.js diff --git a/.gitignore b/.gitignore index 71cb306..1d8670c 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ # next.js /.next/ /out/ +/public/casbin-version.json # production /build diff --git a/app/components/editor/index.tsx b/app/components/editor/index.tsx index b406a5d..f93a433 100755 --- a/app/components/editor/index.tsx +++ b/app/components/editor/index.tsx @@ -15,7 +15,7 @@ 'use client'; import React, { isValidElement, useState, useEffect } from 'react'; import { example, ModelKind } from './casbin-mode/example'; -import { e, m, p, r } from '@/app/components/editor/hooks/useSetupEnforceContext'; // prettier-ignore +import { e, m, p, r } from '@/app/components/editor/hooks/useSetupEnforceContext'; import { clsx } from 'clsx'; import CodeMirror from '@uiw/react-codemirror'; import { monokai } from '@uiw/codemirror-theme-monokai'; @@ -32,9 +32,8 @@ import useShareInfo from '@/app/components/editor/hooks/useShareInfo'; import useCopy from '@/app/components/editor/hooks/useCopy'; import useSetupEnforceContext from '@/app/components/editor/hooks/useSetupEnforceContext'; import useIndex from '@/app/components/editor/hooks/useIndex'; -import { getCasbinVersion } from '@/app/utils/getCasbinVersion'; -export const EditorScreen = ({ casbinVersion }: { casbinVersion: string }) => { +export const EditorScreen = () => { const { modelKind, setModelKind, modelText, setModelText, policy, setPolicy, request, setRequest, echo, setEcho, requestResult, setRequestResult, customConfig, setCustomConfig, share, setShare, @@ -49,6 +48,16 @@ export const EditorScreen = ({ casbinVersion }: { casbinVersion: string }) => { onChange: setEnforceContextDataPersistent, data: enforceContextData, }); + const [casbinVersion, setCasbinVersion] = useState(''); + + useEffect(() => { + const fetchCasbinVersion = async () => { + const response = await fetch('/casbin-version.json'); + const data = await response.json(); + setCasbinVersion(data.casbinVersion); + }; + fetchCasbinVersion(); + }, []); useEffect(() => { if (modelKind) { diff --git a/app/utils/getCasbinVersion.js b/app/utils/getCasbinVersion.js deleted file mode 100644 index 103b811..0000000 --- a/app/utils/getCasbinVersion.js +++ /dev/null @@ -1,6 +0,0 @@ -import packageJson from '../../package.json'; - -export const getCasbinVersion = async () => { - const version = packageJson.dependencies.casbin; - return version.replace('^', ''); -}; diff --git a/generateCasbinVersionPlugin.js b/generateCasbinVersionPlugin.js new file mode 100644 index 0000000..604a5df --- /dev/null +++ b/generateCasbinVersionPlugin.js @@ -0,0 +1,33 @@ +const fs = require('fs'); +const path = require('path'); + +class GenerateCasbinVersionPlugin { + apply(compiler) { + compiler.hooks.emit.tapAsync('GenerateCasbinVersionPlugin', (compilation, callback) => { + const packageJsonPath = path.resolve(__dirname, 'node_modules/casbin/package.json'); + fs.readFile(packageJsonPath, 'utf-8', (err, data) => { + if (err) { + console.error('Error reading package.json:', err); + callback(); + return; + } + + const packageJson = JSON.parse(data); + const casbinVersion = packageJson.version; + const outputPath = path.resolve(__dirname, 'public/casbin-version.json'); + const jsonContent = JSON.stringify({ casbinVersion }); + + fs.writeFile(outputPath, jsonContent, (err) => { + if (err) { + console.error('Error writing casbin-version.json:', err); + } else { + console.log('Casbin version generated:', casbinVersion); + } + callback(); + }); + }); + }); + } +} + +module.exports = GenerateCasbinVersionPlugin; diff --git a/next.config.mjs b/next.config.mjs index 8e2a087..102d43f 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -1,3 +1,4 @@ +import GenerateCasbinVersionPlugin from './generateCasbinVersionPlugin.js'; /** @type {import('next').NextConfig} */ const nextConfig = { /** @@ -35,6 +36,10 @@ const nextConfig = { }) ); + if (!isServer) { + config.plugins.push(new GenerateCasbinVersionPlugin()); + } + return config; },