@zrhsh/wukong-cli 0.3.0 → 0.4.0

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": "@zrhsh/wukong-cli",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Wukong CLI - TypeScript with auto token refresh",
5
5
  "private": false,
6
6
  "type": "module",
@@ -1,9 +1,9 @@
1
1
  /**
2
- * 安装脚本 - 创建默认配置文件
3
- * 在 npm install 时自动执行
2
+ * 安装脚本 - 配置文件迁移和创建
3
+ * 在 npm install/update 时自动执行
4
4
  */
5
5
 
6
- import { writeFileSync, existsSync, mkdirSync, readFileSync } from 'fs';
6
+ import { writeFileSync, existsSync, mkdirSync, readFileSync, unlinkSync } from 'fs';
7
7
  import { join, dirname } from 'path';
8
8
  import { fileURLToPath } from 'url';
9
9
  import { homedir } from 'os';
@@ -12,38 +12,72 @@ const __filename = fileURLToPath(import.meta.url);
12
12
  const __dirname = dirname(__filename);
13
13
 
14
14
  const TEMPLATE_FILE = join(__dirname, '../wukong-cli.json.template');
15
- const USER_CONFIG_PATH = join(homedir(), 'wukong-cli.json');
15
+ const LEGACY_CONFIG_PATH = join(homedir(), 'wukong-cli.json');
16
+ const CONFIG_DIR = join(homedir(), '.wukong-cli');
17
+ const CONFIG_FILE_PATH = join(CONFIG_DIR, 'wukong-cli.json');
16
18
 
19
+ /**
20
+ * 确保配置目录存在
21
+ */
22
+ function ensureConfigDir() {
23
+ if (!existsSync(CONFIG_DIR)) {
24
+ mkdirSync(CONFIG_DIR, { recursive: true });
25
+ }
26
+ }
27
+
28
+ /**
29
+ * 迁移旧配置(针对老用户)
30
+ */
31
+ function migrateLegacyConfig() {
32
+ // 旧配置存在 + 新配置不存在 → 迁移
33
+ if (existsSync(LEGACY_CONFIG_PATH) && !existsSync(CONFIG_FILE_PATH)) {
34
+ try {
35
+ ensureConfigDir();
36
+ const oldConfig = readFileSync(LEGACY_CONFIG_PATH, 'utf-8');
37
+ writeFileSync(CONFIG_FILE_PATH, oldConfig, 'utf-8');
38
+ unlinkSync(LEGACY_CONFIG_PATH);
39
+
40
+ console.log('✓ Migrated configuration to new location');
41
+ console.log('✓ Old config removed:', LEGACY_CONFIG_PATH);
42
+ console.log('✓ New location:', CONFIG_FILE_PATH);
43
+ return true;
44
+ } catch (error) {
45
+ console.warn('⚠ Migration failed:', error.message);
46
+ return false;
47
+ }
48
+ }
49
+ return false;
50
+ }
51
+
52
+ /**
53
+ * 创建默认配置(针对新用户)
54
+ */
17
55
  function createDefaultConfig() {
18
- // 如果配置文件已存在,不覆盖
19
- if (existsSync(USER_CONFIG_PATH)) {
20
- console.log('✓ Config file already exists:', USER_CONFIG_PATH);
56
+ // 新配置已存在 → 不覆盖
57
+ if (existsSync(CONFIG_FILE_PATH)) {
21
58
  return;
22
59
  }
23
60
 
61
+ // 新配置不存在 → 在新位置创建
24
62
  try {
25
- // 检查模板文件是否存在
26
63
  if (!existsSync(TEMPLATE_FILE)) {
27
- console.warn('⚠ Template file not found:', TEMPLATE_FILE);
28
- console.warn('⚠ Please create config manually at:', USER_CONFIG_PATH);
64
+ console.warn('⚠ Template not found:', TEMPLATE_FILE);
65
+ console.warn('⚠ Create config manually at:', CONFIG_FILE_PATH);
29
66
  return;
30
67
  }
31
68
 
32
- // 读取模板文件
69
+ ensureConfigDir();
33
70
  const templateContent = readFileSync(TEMPLATE_FILE, 'utf-8');
71
+ writeFileSync(CONFIG_FILE_PATH, templateContent, 'utf-8');
34
72
 
35
- // 确保目录存在
36
- const dir = dirname(USER_CONFIG_PATH);
37
- if (!existsSync(dir)) {
38
- mkdirSync(dir, { recursive: true });
39
- }
40
-
41
- // 写入配置文件
42
- writeFileSync(USER_CONFIG_PATH, templateContent, 'utf-8');
43
- console.log('✓ Created default config:', USER_CONFIG_PATH);
73
+ console.log('✓ Created default config:', CONFIG_FILE_PATH);
44
74
  } catch (error) {
45
- console.warn('⚠ Failed to create default config:', error.message);
75
+ console.warn('⚠ Failed to create config:', error.message);
46
76
  }
47
77
  }
48
78
 
49
- createDefaultConfig();
79
+ // 主逻辑:先尝试迁移,无需迁移时创建默认配置
80
+ const migrated = migrateLegacyConfig();
81
+ if (!migrated) {
82
+ createDefaultConfig();
83
+ }