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 b4fc4fa..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'; @@ -48,6 +48,16 @@ export const EditorScreen = () => { 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) { @@ -185,7 +195,14 @@ export const EditorScreen = () => {
-
Policy
+
+
Policy
+
+ + Node-Casbin v{casbinVersion} + +
+
{ + 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; },