uniky 1.0.20 → 1.0.21

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "uniky",
3
- "version": "1.0.20",
3
+ "version": "1.0.21",
4
4
  "description": "uni-app 开发工具库,包含 hooks、http 请求和 vite 插件",
5
5
  "type": "module",
6
6
  "main": "./src/lib/index.ts",
@@ -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,27 +62,48 @@ 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
 
80
- installPluginFiles();
105
+ if (process.env.npm_config_global !== 'true') {
106
+ installPluginFiles();
107
+ } else {
108
+ console.log('[uniky] 跳过全局安装的 postinstall 脚本');
109
+ }
@@ -1,10 +1,76 @@
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 } 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
+ if (existsSync(pluginDir) && readdirSync(pluginDir).length > 0) {
44
+ return;
45
+ }
46
+
47
+ console.log('[uniky] 检测到 .uniky/plugin 目录不存在,正在自动安装...');
48
+
49
+ if (!existsSync(unikyDir)) {
50
+ mkdirSync(unikyDir, { recursive: true });
51
+ }
52
+
53
+ let sourceDir = __dirname;
54
+
55
+ if (sourceDir.includes('node_modules')) {
56
+ const nodeModulesIndex = sourceDir.indexOf('node_modules');
57
+ const nodeModulesPath = sourceDir.substring(0, nodeModulesIndex + 'node_modules'.length);
58
+ sourceDir = join(nodeModulesPath, 'uniky', 'src', 'plugin');
59
+ }
60
+
61
+ if (!existsSync(sourceDir)) {
62
+ console.warn(`[uniky] 源目录不存在: ${sourceDir}`);
63
+ return;
64
+ }
65
+
66
+ const copiedCount = copyDirectoryRecursive(sourceDir, pluginDir);
67
+
68
+ console.log(`[uniky] ✅ 插件文件已自动安装到 ${unikyDir} (${copiedCount} 个文件)`);
69
+ } catch (error) {
70
+ console.warn('[uniky] 插件自动安装失败:', error);
71
+ }
72
+ }
73
+
8
74
 
9
75
 
10
76
  export interface UnikyPluginOptions {
@@ -18,6 +84,15 @@ export function unikyPlugin(options: UnikyPluginOptions = {}): Plugin[] {
18
84
 
19
85
  const plugins: Plugin[] = [];
20
86
 
87
+ const setupPlugin: Plugin = {
88
+ name: 'vite-plugin-uniky-setup',
89
+ configResolved(config) {
90
+ ensurePluginInstalled(config.root);
91
+ }
92
+ };
93
+
94
+ plugins.push(setupPlugin);
95
+
21
96
  if (enablePages) {
22
97
  plugins.push(pagesDefinedPlugin());
23
98
  }
@@ -28,7 +103,6 @@ export function unikyPlugin(options: UnikyPluginOptions = {}): Plugin[] {
28
103
  plugins.push(globalDefinedPlugin());
29
104
  }
30
105
 
31
-
32
106
  return plugins;
33
107
  }
34
108