wjx-cli 0.1.3 → 0.1.4
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/dist/commands/init.js +36 -41
- package/dist/commands/init.js.map +1 -1
- package/package.json +1 -1
package/dist/commands/init.js
CHANGED
|
@@ -2,72 +2,67 @@ import { createInterface } from "node:readline/promises";
|
|
|
2
2
|
import { stdin, stderr } from "node:process";
|
|
3
3
|
import { listSurveys } from "wjx-api-sdk";
|
|
4
4
|
import { loadConfig, saveConfig, CONFIG_PATH } from "../lib/config.js";
|
|
5
|
+
function mask(value) {
|
|
6
|
+
if (value.length <= 4)
|
|
7
|
+
return "****";
|
|
8
|
+
return value.slice(0, 4) + "****" + value.slice(-4);
|
|
9
|
+
}
|
|
5
10
|
export function registerInitCommands(program) {
|
|
6
11
|
program
|
|
7
12
|
.command("init")
|
|
8
13
|
.description("初始化配置(交互式设置 API Key、Base URL、Corp ID)")
|
|
9
14
|
.action(async () => {
|
|
15
|
+
const config = loadConfig();
|
|
16
|
+
const currentApiKey = process.env.WJX_API_KEY || config?.apiKey || "";
|
|
17
|
+
const currentBaseUrl = process.env.WJX_BASE_URL || config?.baseUrl || "";
|
|
18
|
+
const currentCorpId = process.env.WJX_CORP_ID || config?.corpId || "";
|
|
19
|
+
stderr.write("问卷星 CLI 配置向导\n");
|
|
20
|
+
stderr.write(`配置文件: ${CONFIG_PATH}\n\n`);
|
|
10
21
|
const rl = createInterface({ input: stdin, output: stderr });
|
|
11
22
|
try {
|
|
12
|
-
//
|
|
13
|
-
const existing = loadConfig();
|
|
14
|
-
if (existing) {
|
|
15
|
-
const masked = existing.apiKey.length > 8
|
|
16
|
-
? existing.apiKey.slice(0, 8) + "..."
|
|
17
|
-
: "***";
|
|
18
|
-
stderr.write(`已存在配置文件: ${CONFIG_PATH}\n`);
|
|
19
|
-
stderr.write(` apiKey: ${masked}\n`);
|
|
20
|
-
if (existing.baseUrl)
|
|
21
|
-
stderr.write(` baseUrl: ${existing.baseUrl}\n`);
|
|
22
|
-
if (existing.corpId)
|
|
23
|
-
stderr.write(` corpId: ${existing.corpId}\n`);
|
|
24
|
-
const overwrite = await rl.question("\n覆盖现有配置?(y/N) ");
|
|
25
|
-
if (overwrite.trim().toLowerCase() !== "y") {
|
|
26
|
-
stderr.write("已取消。\n");
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
stderr.write("\n");
|
|
30
|
-
}
|
|
31
|
-
// Prompt API Key (required)
|
|
23
|
+
// 1. API Key (required *)
|
|
32
24
|
let apiKey = "";
|
|
33
25
|
while (!apiKey) {
|
|
34
|
-
const
|
|
35
|
-
|
|
26
|
+
const hint = currentApiKey ? ` [${mask(currentApiKey)}]` : "";
|
|
27
|
+
const input = await rl.question(`* WJX_API_KEY${hint}: `);
|
|
28
|
+
apiKey = input.trim() || currentApiKey;
|
|
36
29
|
if (!apiKey) {
|
|
37
|
-
stderr.write("API Key
|
|
30
|
+
stderr.write(" API Key 不能为空,请输入。\n");
|
|
38
31
|
}
|
|
39
32
|
}
|
|
40
|
-
//
|
|
41
|
-
const
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const
|
|
33
|
+
// 2. Base URL (optional)
|
|
34
|
+
const defaultUrl = currentBaseUrl || "https://www.wjx.cn";
|
|
35
|
+
const baseUrlInput = await rl.question(` WJX_BASE_URL [${defaultUrl}]: `);
|
|
36
|
+
const baseUrl = baseUrlInput.trim() || defaultUrl;
|
|
37
|
+
// 3. Corp ID (optional)
|
|
38
|
+
const corpHint = currentCorpId ? ` [${currentCorpId}]` : "";
|
|
39
|
+
const corpIdInput = await rl.question(` WJX_CORP_ID${corpHint}: `);
|
|
40
|
+
const corpId = corpIdInput.trim() || currentCorpId || undefined;
|
|
46
41
|
// Validate API Key
|
|
47
|
-
stderr.write("\n
|
|
42
|
+
stderr.write("\n验证 API Key...");
|
|
48
43
|
try {
|
|
49
44
|
const result = await listSurveys({ page_index: 1, page_size: 1 }, { apiKey });
|
|
50
45
|
if (result.result === false) {
|
|
51
|
-
stderr.write(
|
|
46
|
+
stderr.write(` 失败 (${result.errormsg})\n`);
|
|
52
47
|
stderr.write(" 配置仍将保存,请检查 Key 是否正确。\n");
|
|
53
48
|
}
|
|
54
49
|
else {
|
|
55
|
-
stderr.write("OK
|
|
50
|
+
stderr.write(" OK\n");
|
|
56
51
|
}
|
|
57
52
|
}
|
|
58
53
|
catch (err) {
|
|
59
|
-
stderr.write(
|
|
54
|
+
stderr.write(` 失败 (${err instanceof Error ? err.message : String(err)})\n`);
|
|
60
55
|
stderr.write(" 配置仍将保存。\n");
|
|
61
56
|
}
|
|
62
|
-
// Save
|
|
63
|
-
const
|
|
64
|
-
if (baseUrl)
|
|
65
|
-
|
|
57
|
+
// Save
|
|
58
|
+
const newConfig = { apiKey };
|
|
59
|
+
if (baseUrl !== "https://www.wjx.cn")
|
|
60
|
+
newConfig.baseUrl = baseUrl;
|
|
66
61
|
if (corpId)
|
|
67
|
-
|
|
68
|
-
saveConfig(
|
|
69
|
-
stderr.write(`\
|
|
70
|
-
stderr.write("提示:
|
|
62
|
+
newConfig.corpId = corpId;
|
|
63
|
+
saveConfig(newConfig);
|
|
64
|
+
stderr.write(`\n已保存到 ${CONFIG_PATH}\n`);
|
|
65
|
+
stderr.write("提示: 也可以直接编辑该文件修改配置。\n");
|
|
71
66
|
}
|
|
72
67
|
finally {
|
|
73
68
|
rl.close();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"init.js","sourceRoot":"","sources":["../../src/commands/init.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,KAAK,
|
|
1
|
+
{"version":3,"file":"init.js","sourceRoot":"","sources":["../../src/commands/init.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,cAA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