Skip to content

Commit

Permalink
feat(projects): add version update notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
mufeng889 committed Sep 1, 2024
1 parent 04577cd commit 3f25b19
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 7 deletions.
13 changes: 13 additions & 0 deletions build/plugins/html.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Plugin } from 'vite';

export function setupHtmlPlugin(buildTime: string) {
const plugin: Plugin = {
name: 'html-plugin',
apply: 'build',
transformIndexHtml(html) {
return html.replace('<head>', `<head>\n <meta name="buildTime" content="${buildTime}">`);
}
};

return plugin;
}
6 changes: 4 additions & 2 deletions build/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ import { setupAutoImport } from './auto-import';
import { setupUnocss } from './unocss';
import { setupUnPluginIcon } from './unplugin-icon';
import { setupElegantRouter } from './router';
import { setupHtmlPlugin } from './html';

export function setupVitePlugins(viteEnv: Env.ImportMeta) {
export function setupVitePlugins(viteEnv: Env.ImportMeta, buildTime: string) {
const plugins: PluginOption = [
react(),
setupAutoImport(viteEnv),
setupUnocss(viteEnv),
...setupUnPluginIcon(viteEnv),
setupElegantRouter(),
Inspect(),
removeConsole()
removeConsole(),
setupHtmlPlugin(buildTime)
];
return plugins;
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"cleanup": "sa cleanup",
"commit": "sa git-commit",
"commit:zh": "sa git-commit -l=zh-cn",
"czh": "sa git-commit -l=zh-cn",
"dev": "vite --mode test",
"dev:prod": "vite --mode prod",
"gen-route": "sa gen-route",
Expand Down
12 changes: 10 additions & 2 deletions packages/scripts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { blue, lightGreen } from 'kolorist';
import { version } from '../package.json';
import { cleanup, genChangelog, generateRoute, gitCommit, gitCommitVerify, release, updatePkg } from './commands';
import { loadCliOptions } from './config';
import type { Lang } from './locales';

type Command = 'cleanup' | 'update-pkg' | 'git-commit' | 'git-commit-verify' | 'changelog' | 'release' | 'gen-route';

Expand All @@ -25,6 +26,12 @@ interface CommandArg {
* Multiple values use "," to separate them
*/
cleanupDir?: string;
/**
* display lang of cli
*
* @default 'en-us'
*/
lang?: Lang;
}

export async function setupCli() {
Expand All @@ -44,6 +51,7 @@ export async function setupCli() {
'-c, --cleanupDir <dir>',
'The glob pattern of dirs to cleanup, If not set, it will use the default value, Multiple values use "," to separate them'
)
.option('-l, --lang <lang>', 'display lang of cli', { default: 'en-us', type: [String] })
.help();

const commands: CommandWithAction<CommandArg> = {
Expand All @@ -61,8 +69,8 @@ export async function setupCli() {
},
'git-commit': {
desc: 'git commit, generate commit message which match Conventional Commits standard',
action: async () => {
await gitCommit(cliOptions.gitCommitTypes, cliOptions.gitCommitScopes);
action: async args => {
await gitCommit(args?.lang);
}
},
'git-commit-verify': {
Expand Down
4 changes: 3 additions & 1 deletion src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import FallbackRender from '../ErrorBoundary.tsx';
import App from './App.tsx';
import './plugins/assets';
import { setupI18n } from './locales';
import { setupDayjs, setupIconifyOffline, setupLoading, setupNProgress } from './plugins';
import { setupAppVersionNotification, setupDayjs, setupIconifyOffline, setupLoading, setupNProgress } from './plugins';

function setupApp() {
setupI18n();
Expand All @@ -19,6 +19,8 @@ function setupApp() {

setupDayjs();

setupAppVersionNotification();

const container = document.getElementById('root');
if (!container) return;
const root = createRoot(container);
Expand Down
68 changes: 68 additions & 0 deletions src/plugins/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { createElement } from 'react';
import { Button } from 'antd';
import { $t } from '../locales';

export function setupAppVersionNotification() {
let isShow = false;

document.addEventListener('visibilitychange', async () => {
const preConditions = [!isShow, document.visibilityState === 'visible', !import.meta.env.DEV];

if (!preConditions.every(Boolean)) return;

const buildTime = await getHtmlBuildTime();

if (buildTime === BUILD_TIME) {
return;
}

isShow = true;

window.$notification?.open({
message: $t('system.updateTitle'),
description: $t('system.updateContent'),
btn: (() => {
return createElement(
'div',
{ style: { display: 'flex', justifyContent: 'end', gap: '12px', width: '325px' } },
[
createElement(
Button,
{
onClick() {
window.$notification?.destroy();
}
},
$t('system.updateCancel')
),
createElement(
Button,
{
type: 'primary',
onClick() {
location.reload();
}
},
$t('system.updateConfirm')
)
]
);
})(),
onClose() {
isShow = false;
}
});
});
}

async function getHtmlBuildTime() {
const res = await fetch(`/index.html?time=${Date.now()}`);

const html = await res.text();

const match = html.match(/<meta name="buildTime" content="(.*)">/);

const buildTime = match?.[1] || '';

return buildTime;
}
1 change: 1 addition & 0 deletions src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './nprogress';
export * from './loading';
export * from './iconify';
export * from './dayjs';
export * from './app';
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default defineConfig(configEnv => {
}
}
},
plugins: setupVitePlugins(viteEnv),
plugins: setupVitePlugins(viteEnv, buildTime),
define: {
BUILD_TIME: JSON.stringify(buildTime)
},
Expand Down

0 comments on commit 3f25b19

Please sign in to comment.