Skip to content

Commit 7bbdff9

Browse files
author
kinfuy
committed
feat: low version support prompt (#1)
1 parent 428ab33 commit 7bbdff9

File tree

5 files changed

+51
-11
lines changed

5 files changed

+51
-11
lines changed

package/index.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,58 @@
1+
import { readFileSync } from 'fs';
2+
import { join } from 'path';
3+
import colors from 'picocolors';
14
import { bindShortcuts } from './shortcuts';
25
import type { Plugin, ViteDevServer } from 'vite';
36
import type { CLIShortcut } from './shortcuts';
4-
57
export interface ShortcutsOptions {
68
/**
79
* @description default `Plugin Shortcuts`
810
*/
911
outputName?: string;
1012
shortcuts: CLIShortcut[];
1113
}
14+
/**
15+
* 查询版本号
16+
* @param path
17+
* @returns
18+
*/
19+
const queryViteVersion = (path: string) => {
20+
const pkg = readFileSync(path, 'utf-8');
21+
let version = 4;
22+
if (pkg) {
23+
try {
24+
version =
25+
JSON.parse(pkg)?.devDependencies?.vite?.match(
26+
/^[^\d]*(?<version>[0-9])/
27+
).groups?.version || 4;
28+
} catch (error) {}
29+
}
30+
return Number(version);
31+
};
1232

1333
export function shortcutsPlugin(shortcutsOptions?: ShortcutsOptions): Plugin {
1434
return {
1535
name: 'shortcuts',
1636
apply: 'serve',
1737
configureServer: (server: ViteDevServer) => {
38+
const path = join(server.config.root, 'package.json');
39+
const version = queryViteVersion(path);
40+
const _printUrls = server.printUrls;
41+
42+
// 改写printUrls 方法
43+
// https://github.com/kinfuy/vite-plugin-shortcuts/issues/1
44+
server.printUrls = () => {
45+
_printUrls();
46+
if (version < 4) {
47+
server.config.logger.info(
48+
colors.dim(colors.green(' ➜')) +
49+
colors.dim(' press ') +
50+
colors.bold('h') +
51+
colors.dim(' to show help')
52+
);
53+
}
54+
};
55+
1856
return () => {
1957
bindShortcuts(server, shortcutsOptions);
2058
};

package/shortcuts.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ export function bindShortcuts(
2424
.filter(isDefined)
2525
.concat(BASE_SHORTCUTS);
2626

27+
if (shortcuts.length === 0) {
28+
server.config.logger.warn(
29+
colors.yellow('No additional shortcut keys configured')
30+
);
31+
return;
32+
}
33+
2734
let actionRunning = false;
2835

2936
const onInput = async (input: string) => {
@@ -42,7 +49,7 @@ export function bindShortcuts(
4249
server.config.logger.info(
4350
[
4451
'',
45-
colors.bold(` ${outputName}`),
52+
colors.bold(` ${outputName}`),
4653
...shortcuts.map(
4754
(shortcut) =>
4855
colors.dim(' press ') +

playground/vue/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"vue": "^3.2.41"
1414
},
1515
"devDependencies": {
16-
"vite-plugin-shortcuts": "^0.0.5",
16+
"vite-plugin-shortcuts": "workspace:*",
1717
"vite": "^3.0.0",
1818
"@vitejs/plugin-vue": "^3.0.0",
1919
"js-yaml": "^4.1.0",

playground/vue/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default defineConfig({
1212
},
1313
plugins: [
1414
shortcutsPlugin({
15+
outputName: '自定义快捷键',
1516
shortcuts: [
1617
{
1718
key: 'c',

pnpm-lock.yaml

Lines changed: 2 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)