uniky 1.0.20 → 1.0.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -19,6 +19,26 @@ pnpm add uniky
19
19
  yarn add uniky
20
20
  ```
21
21
 
22
+ ## 安装后设置
23
+
24
+ ### 自动安装(推荐)
25
+
26
+ 安装 `uniky` 后,插件文件会自动安装到项目根目录的 `.uniky` 文件夹。
27
+
28
+ 如果自动安装失败或删除了 `.uniky` 文件夹,可以手动触发安装:
29
+
30
+ ```bash
31
+ # 使用 npx
32
+ npx uniky-install
33
+
34
+ # 或者直接运行
35
+ node node_modules/uniky/scripts/postinstall.js
36
+ ```
37
+
38
+ ### 自动检测安装
39
+
40
+ 如果 `.uniky` 文件夹缺失,在首次运行 Vite 时,插件会自动检测并安装所需文件。
41
+
22
42
  ## 使用
23
43
 
24
44
  ### 库功能
@@ -97,11 +117,35 @@ _To.back();
97
117
  在 `main.ts` 中安装全局定义:
98
118
 
99
119
  ```typescript
100
- import { installGlobals } from './autoGen/global.install';
120
+ import { installGlobals } from './_unikey/global.install';
101
121
 
102
122
  installGlobals();
103
123
  ```
104
124
 
125
+ ## 目录结构
126
+
127
+ 安装后,项目根目录会生成 `.uniky` 文件夹:
128
+
129
+ ```
130
+ your-project/
131
+ ├── .uniky/
132
+ │ ├── index.ts
133
+ │ └── plugin/
134
+ │ ├── index.ts
135
+ │ ├── pages.defined.ts
136
+ │ ├── global.defined.ts
137
+ │ ├── http.defined.ts
138
+ │ └── lib.defined.ts
139
+ ├── src/
140
+ │ └── _unikey/ # 插件自动生成的文件
141
+ │ ├── global/
142
+ │ │ ├── pages.ts
143
+ │ │ └── ky.ts
144
+ │ ├── global.d.ts
145
+ │ └── global.install.ts
146
+ └── vite.config.ts
147
+ ```
148
+
105
149
  ## 故障排除
106
150
 
107
151
  ### ESM 相关错误
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "uniky",
3
- "version": "1.0.20",
3
+ "version": "1.0.22",
4
4
  "description": "uni-app 开发工具库,包含 hooks、http 请求和 vite 插件",
5
5
  "type": "module",
6
6
  "main": "./src/lib/index.ts",
@@ -16,6 +16,9 @@
16
16
  "import": "./src/plugin/index.ts"
17
17
  }
18
18
  },
19
+ "bin": {
20
+ "uniky-install": "./scripts/postinstall.js"
21
+ },
19
22
  "sideEffects": false,
20
23
  "files": [
21
24
  "src/lib",
@@ -8,16 +8,24 @@ const __dirname = dirname(__filename);
8
8
 
9
9
  function findProjectRoot() {
10
10
  let currentDir = process.cwd();
11
+
12
+ // 如果当前目录在 node_modules 中,向上查找到项目根目录
13
+ if (currentDir.includes('node_modules')) {
14
+ const parts = currentDir.split('node_modules');
15
+ if (parts.length > 1) {
16
+ // 返回 node_modules 的父目录
17
+ return parts[0].replace(/[\/\\]$/, '');
18
+ }
19
+ }
20
+
21
+ // 否则向上查找包含 package.json 和 node_modules 的目录
11
22
  const maxDepth = 10;
12
23
  let depth = 0;
13
24
 
14
25
  while (depth < maxDepth) {
15
26
  const packageJsonPath = join(currentDir, 'package.json');
16
27
  if (existsSync(packageJsonPath)) {
17
- const nodeModulesPath = join(currentDir, 'node_modules');
18
- if (existsSync(nodeModulesPath)) {
19
- return currentDir;
20
- }
28
+ return currentDir;
21
29
  }
22
30
  const parentDir = dirname(currentDir);
23
31
  if (parentDir === currentDir) break;
@@ -54,26 +62,43 @@ function copyDirectoryRecursive(source, target) {
54
62
 
55
63
  function installPluginFiles() {
56
64
  try {
65
+ console.log('[uniky] 开始安装插件文件...');
66
+
57
67
  const projectRoot = findProjectRoot();
68
+ console.log(`[uniky] 项目根目录: ${projectRoot}`);
69
+
58
70
  const unikyDir = join(projectRoot, '.uniky');
59
71
  const pluginDir = join(unikyDir, 'plugin');
72
+
73
+ console.log(`[uniky] 目标目录: ${unikyDir}`);
60
74
 
61
75
  if (!existsSync(unikyDir)) {
62
76
  mkdirSync(unikyDir, { recursive: true });
77
+ console.log(`[uniky] 创建目录: ${unikyDir}`);
63
78
  }
64
79
 
65
80
  const sourceDir = join(__dirname, '..', 'src', 'plugin');
81
+ console.log(`[uniky] 源目录: ${sourceDir}`);
82
+
83
+ if (!existsSync(sourceDir)) {
84
+ console.error(`[uniky] 错误: 源目录不存在 ${sourceDir}`);
85
+ return;
86
+ }
87
+
66
88
  const copiedCount = copyDirectoryRecursive(sourceDir, pluginDir);
89
+ console.log(`[uniky] 拷贝了 ${copiedCount} 个文件`);
67
90
 
68
91
  const indexContent = `// created by zhuxietong on 2026-01-30 16:37
69
92
  export * from './plugin/index.js';
70
93
  `;
71
94
 
72
95
  writeFileSync(join(unikyDir, 'index.ts'), indexContent, 'utf-8');
96
+ console.log(`[uniky] 创建索引文件: ${join(unikyDir, 'index.ts')}`);
73
97
 
74
- console.log(`[uniky] 插件文件已安装到 ${unikyDir} (${copiedCount} 个文件)`);
98
+ console.log(`[uniky] 插件文件已成功安装到 ${unikyDir} (${copiedCount} 个文件)`);
75
99
  } catch (error) {
76
- console.warn('[uniky] 插件文件安装失败,将在 vite 启动时自动安装:', error.message);
100
+ console.error('[uniky] 插件文件安装失败:', error);
101
+ console.error('[uniky] 错误堆栈:', error.stack);
77
102
  }
78
103
  }
79
104
 
@@ -1,10 +1,86 @@
1
1
  // created by zhuxietong on 2026-01-30 16:37
2
2
  import type { Plugin } from 'vite';
3
+ import { copyFileSync, mkdirSync, existsSync, readdirSync, statSync, writeFileSync } from 'fs';
4
+ import { join, dirname } from 'path';
5
+ import { fileURLToPath } from 'url';
3
6
 
4
7
  import pagesDefinedPlugin from './pages.defined';
5
8
  import globalDefinedPlugin from './global.defined';
6
9
  import httpDefinedPlugin from './http.defined';
7
10
 
11
+ const __filename = fileURLToPath(import.meta.url);
12
+ const __dirname = dirname(__filename);
13
+
14
+ function copyDirectoryRecursive(source: string, target: string): number {
15
+ if (!existsSync(target)) {
16
+ mkdirSync(target, { recursive: true });
17
+ }
18
+
19
+ const files = readdirSync(source);
20
+ let count = 0;
21
+
22
+ files.forEach(file => {
23
+ const sourcePath = join(source, file);
24
+ const targetPath = join(target, file);
25
+ const stat = statSync(sourcePath);
26
+
27
+ if (stat.isDirectory()) {
28
+ count += copyDirectoryRecursive(sourcePath, targetPath);
29
+ } else {
30
+ copyFileSync(sourcePath, targetPath);
31
+ count++;
32
+ }
33
+ });
34
+
35
+ return count;
36
+ }
37
+
38
+ function ensurePluginInstalled(projectRoot: string): void {
39
+ try {
40
+ const unikyDir = join(projectRoot, '.uniky');
41
+ const pluginDir = join(unikyDir, 'plugin');
42
+
43
+ // 检查关键文件是否存在
44
+ const keyFiles = ['index.ts', 'pages.defined.ts', 'global.defined.ts'];
45
+ const needsInstall = !existsSync(pluginDir) ||
46
+ keyFiles.some(file => !existsSync(join(pluginDir, file)));
47
+
48
+ if (!needsInstall) {
49
+ return;
50
+ }
51
+
52
+ console.log('[uniky] 检测到插件文件缺失,正在自动安装...');
53
+
54
+ if (!existsSync(unikyDir)) {
55
+ mkdirSync(unikyDir, { recursive: true });
56
+ }
57
+
58
+ let sourceDir = __dirname;
59
+
60
+ if (sourceDir.includes('node_modules')) {
61
+ const nodeModulesIndex = sourceDir.indexOf('node_modules');
62
+ const nodeModulesPath = sourceDir.substring(0, nodeModulesIndex + 'node_modules'.length);
63
+ sourceDir = join(nodeModulesPath, 'uniky', 'src', 'plugin');
64
+ }
65
+
66
+ if (!existsSync(sourceDir)) {
67
+ console.warn(`[uniky] 源目录不存在: ${sourceDir}`);
68
+ return;
69
+ }
70
+
71
+ const copiedCount = copyDirectoryRecursive(sourceDir, pluginDir);
72
+
73
+ const indexContent = `// created by zhuxietong on 2026-01-30 16:37
74
+ export * from './plugin/index.js';
75
+ `;
76
+ writeFileSync(join(unikyDir, 'index.ts'), indexContent, 'utf-8');
77
+
78
+ console.log(`[uniky] ✅ 插件文件已自动安装到 ${unikyDir} (${copiedCount} 个文件)`);
79
+ } catch (error) {
80
+ console.warn('[uniky] 插件自动安装失败:', error);
81
+ }
82
+ }
83
+
8
84
 
9
85
 
10
86
  export interface UnikyPluginOptions {
@@ -18,6 +94,15 @@ export function unikyPlugin(options: UnikyPluginOptions = {}): Plugin[] {
18
94
 
19
95
  const plugins: Plugin[] = [];
20
96
 
97
+ const setupPlugin: Plugin = {
98
+ name: 'vite-plugin-uniky-setup',
99
+ configResolved(config) {
100
+ ensurePluginInstalled(config.root);
101
+ }
102
+ };
103
+
104
+ plugins.push(setupPlugin);
105
+
21
106
  if (enablePages) {
22
107
  plugins.push(pagesDefinedPlugin());
23
108
  }
@@ -28,7 +113,6 @@ export function unikyPlugin(options: UnikyPluginOptions = {}): Plugin[] {
28
113
  plugins.push(globalDefinedPlugin());
29
114
  }
30
115
 
31
-
32
116
  return plugins;
33
117
  }
34
118