Skip to content

Commit

Permalink
optimize: get version
Browse files Browse the repository at this point in the history
  • Loading branch information
snowtafir committed Sep 9, 2024
1 parent d8c539a commit 8ea369e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
5 changes: 3 additions & 2 deletions src/components/dynamic/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import Config from '@/utils/config';
import { _paths } from '@/utils/paths';
import path from 'path';

const botVersion = ConfigController.package?.version
const botVersion = ConfigController.package?.version;
const yukiPluginVersion = Config.getPackageJsonKey('version', path.join(_paths.pluginPath, 'package.json'));

const bilibililogo: string = path.join(_paths.pluginResources, 'img/icon/dynamic/bilibili.svg')
const weibilogo: string = path.join(_paths.pluginResources, 'img/icon/dynamic/weibo.svg')
Expand Down Expand Up @@ -45,7 +46,7 @@ const Footer: React.FC<FooterProps> = ({ data }) => {
</div>
<div className="bot-plugin-info" style={{ marginTop: '6px' }}>
Created By {`${BOT_NAME}-v` + `${botVersion}`} & <span className="yuki-plugin-text-title">yuki-plugin</span>-v
<span className="italic">{`${Config.getLatestVersion()}`}</span>
<span className="italic">{yukiPluginVersion}</span>
</div>
</div>
<img src={data.urlImgData} alt="二维码" className="qr-code" />
Expand Down
7 changes: 4 additions & 3 deletions src/components/help/Help.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import Config from '@/utils/config';
import { _paths } from '@/utils/paths';
import path from 'path';

const botVersion = ConfigController.package?.version
const botVersion = ConfigController.package?.version;
const yukiPluginVersion = Config.getPackageJsonKey('version', path.join(_paths.pluginPath, 'package.json'));

const HelpCss: string = path.join(_paths.pluginResources, 'css/help/help.css')
const iconPath = (iconName: string) => path.join(_paths.pluginResources, `img/icon/puplic/${iconName}.png`)
Expand All @@ -28,7 +29,7 @@ export default function App({ data }: HelpPageProps) {
<div className="container" id="container">
<div className="head_box">
<div className="id_text">Yuki-Plugin</div>
<h2 className="day_text">使用说明-v{Config.getLatestVersion()}</h2>
<h2 className="day_text">使用说明-v{yukiPluginVersion}</h2>
</div>
{data.map((val, index) => (
<div className="data_box" key={index}>
Expand All @@ -50,7 +51,7 @@ export default function App({ data }: HelpPageProps) {
))}
<div className="logo" style={{ marginTop: '6px' }}>
Created By {`${BOT_NAME}-v` + `${botVersion}`} & <span className="yuki-plugin-text-title">yuki-plugin</span>-v
<span className="italic">{`${Config.getLatestVersion()}`}</span>
<span className="italic">{yukiPluginVersion}</span>
</div>
</div>
</>
Expand Down
5 changes: 3 additions & 2 deletions src/components/version/Version.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import Config from '../../utils/config';
import { _paths } from '@/utils/paths';
import path from 'path';

const botVersion = ConfigController.package?.version
const botVersion = ConfigController.package?.version;
const yukiPluginVersion = Config.getPackageJsonKey('version', path.join(_paths.pluginPath, 'package.json'));

const VersionCss: string = path.join(_paths.pluginResources, 'css/version/version.css')

Expand Down Expand Up @@ -34,7 +35,7 @@ export default function App({ data }: VersionProps) {
))}
<div className="logo" style={{ marginTop: '6px' }}>
Created By {`${BOT_NAME}-v` + `${botVersion}`} & <span className="yuki-plugin-text-title">yuki-plugin</span>-v
<span className="italic">{`${Config.getLatestVersion()}`}</span>
<span className="italic">{yukiPluginVersion}</span>
</div>
</div>
</>
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import chalk from 'chalk';
import { Application, applicationOptions, EventType, setBotTask, useEvent } from 'yunzai'
import Config from '@/utils/config';
import path from 'path';
import { _paths } from '@/utils/paths';
import * as apps from '@/apps/index';
import { BiliTask } from '@/models/bilibili/bilibili.task';
import { WeiboTask } from '@/models/weibo/weibo.task';
Expand All @@ -10,6 +12,8 @@ type RulesType = {
key: string
}[]

const yukiPluginVersion = Config.getPackageJsonKey('version', path.join(_paths.pluginPath, 'package.json'));

let biliConfigData = Config.getConfigData("config", "bilibili", "config");
let weiboConfigData = Config.getConfigData("config", "weibo", "config");

Expand Down Expand Up @@ -43,7 +47,7 @@ export default () => {
}
}
logger.info(chalk.rgb(0, 190, 255)(`-----------------------------------------`));
logger.info(chalk.rgb(255, 225, 255)(`优纪插件 ${Config.getLatestVersion()} 初始化~`));
logger.info(chalk.rgb(255, 225, 255)(`优纪插件 ${yukiPluginVersion} 初始化~`));
logger.info(chalk.rgb(255, 245, 255)(`作者:snowtafir`));
logger.info(chalk.rgb(255, 225, 255)(`仓库地址:`));
logger.info(chalk.rgb(255, 245, 255)(`https://github.com/snowtafir/yuki-plugin`));
Expand Down
28 changes: 17 additions & 11 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ declare const logger: any;
* Config 类用于管理配置文件的读取和监听
*/
class Config {
readonly packageJsonPath: string;
readonly defaultConfigPath: string;
readonly userConfigPath: string;
defaultConfig: Record<string, any>;
userConfig: Record<string, any>;
watcher: Record<string, chokidar.FSWatcher>;

constructor() {
this.packageJsonPath = path.join(_paths.pluginPath, 'package.json');
/** 默认设置 */
this.defaultConfigPath = path.join(_paths.pluginPath, 'defaultConfig');
this.defaultConfig = {};
Expand Down Expand Up @@ -177,15 +175,23 @@ class Config {
this.saveConfig("config", appDir, functionName, config); // 保存更新后的配置
}

/** 读取package.json文件,获取最新版本号*/
getLatestVersion(): string | null {
const content = fs.readFileSync(this.packageJsonPath, 'utf-8');
const packageJson: { [key: string]: any } = JSON.parse(content);
const match: string | null = packageJson.version;

if (match) {
return match;
} else {
/** 读取package.json文件,获取指定key的值
* @param keyName 要获取的key名称
* @param path package.json文件路径
*/
getPackageJsonKey(keyName: string, path: string): string | null {
try {
const content = fs.readFileSync(path, 'utf-8');
const packageJson: { [key: string]: any } = JSON.parse(content);
const match: string | null = packageJson[keyName];

if (match) {
return match;
} else {
return null;
}
} catch (error) {
logger.error(`getPackageJsonKey error: ${error}`);
return null;
}
}
Expand Down

0 comments on commit 8ea369e

Please sign in to comment.