@zrhsh/wukong-cli 0.2.2 → 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/README.md +97 -147
- package/dist/cli.js +61 -46
- package/dist/cli.js.map +1 -1
- package/package.json +1 -1
- package/scripts/install.js +56 -22
package/package.json
CHANGED
package/scripts/install.js
CHANGED
|
@@ -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
|
|
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(
|
|
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
|
|
28
|
-
console.warn('⚠
|
|
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
|
|
75
|
+
console.warn('⚠ Failed to create config:', error.message);
|
|
46
76
|
}
|
|
47
77
|
}
|
|
48
78
|
|
|
49
|
-
|
|
79
|
+
// 主逻辑:先尝试迁移,无需迁移时创建默认配置
|
|
80
|
+
const migrated = migrateLegacyConfig();
|
|
81
|
+
if (!migrated) {
|
|
82
|
+
createDefaultConfig();
|
|
83
|
+
}
|