td-web-cli 0.1.6 → 0.1.7

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/src/api/index.ts DELETED
@@ -1,188 +0,0 @@
1
- import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
2
- import { normalizeError } from '../utils/index.js';
3
-
4
- // GET 请求封装
5
- export const getData = async <T>(
6
- url: string,
7
- params: Record<string, unknown> = {},
8
- headers: Record<string, string> = {}
9
- ): Promise<T> => {
10
- const res = await axios.get<T>(url, { params, headers });
11
- return res.data;
12
- };
13
-
14
- // POST 请求封装
15
- export const postData = async <T, R>(
16
- url: string,
17
- params: T,
18
- headers: Record<string, string> = {}
19
- ): Promise<R> => {
20
- const res = await axios.post<T, AxiosResponse<R>>(url, params, {
21
- headers,
22
- });
23
- return res.data;
24
- };
25
-
26
- // 流式传输 POST 请求(带取消功能)
27
- export const postStream = async <T>(
28
- url: string,
29
- params: T,
30
- headers: Record<string, string> = {},
31
- onData: (chunk: string) => void,
32
- onError?: (error: Error) => void,
33
- onComplete?: () => void,
34
- signal?: AbortSignal
35
- ): Promise<void> => {
36
- const config: AxiosRequestConfig = {
37
- headers,
38
- responseType: 'stream',
39
- signal, // 添加 AbortSignal 支持
40
- };
41
-
42
- try {
43
- const response = await axios.post(url, params, config);
44
-
45
- // 处理流数据
46
- const stream = response.data;
47
- let buffer = '';
48
-
49
- // 监听 abort 事件
50
- const onAbort = (): void => {
51
- stream.destroy(new Error('请求已中止'));
52
- };
53
-
54
- signal?.addEventListener('abort', onAbort, { once: true });
55
-
56
- stream.on('data', (chunk: Buffer) => {
57
- buffer += chunk.toString();
58
-
59
- // 处理可能的多个消息在一个chunk中
60
- const parts = buffer.split('\n');
61
- buffer = parts.pop() || ''; // 保留未完成的部分
62
-
63
- parts.forEach((part) => {
64
- if (part.trim()) {
65
- onData(part.trim());
66
- }
67
- });
68
- });
69
-
70
- stream.on('end', () => {
71
- signal?.removeEventListener('abort', onAbort);
72
- if (buffer.trim()) {
73
- onData(buffer.trim());
74
- }
75
- onComplete?.();
76
- });
77
-
78
- stream.on('error', (err: Error) => {
79
- signal?.removeEventListener('abort', onAbort);
80
- // 如果是主动取消的请求,不触发 onError
81
- if (err.message !== '请求已中止') {
82
- onError?.(err);
83
- }
84
- });
85
- } catch (error) {
86
- // 如果是取消的请求,不触发 onError
87
- if (!axios.isCancel(error)) {
88
- onError?.(normalizeError(error));
89
- }
90
- }
91
- };
92
-
93
- // SSE 传输 POST 请求(带取消功能)
94
- export const postSSE = async <T>(
95
- url: string,
96
- params: T,
97
- headers: Record<string, string> = {},
98
- onMessage: (event: string, data: string) => void,
99
- onError?: (error: Error) => void,
100
- signal?: AbortSignal
101
- ): Promise<void> => {
102
- const config: AxiosRequestConfig = {
103
- headers: {
104
- ...headers,
105
- Accept: 'text/event-stream',
106
- },
107
- responseType: 'stream',
108
- signal, // 添加 AbortSignal 支持
109
- };
110
-
111
- try {
112
- const response = await axios.post(url, params, config);
113
- const stream = response.data;
114
- let buffer = '';
115
-
116
- // 监听 abort 事件
117
- const onAbort = (): void => {
118
- stream.destroy(new Error('请求已中止'));
119
- };
120
-
121
- signal?.addEventListener('abort', onAbort, { once: true });
122
-
123
- stream.on('data', (chunk: Buffer) => {
124
- buffer += chunk.toString();
125
-
126
- // 处理SSE格式(以\n\n分隔的多个事件)
127
- const events = buffer.split('\n\n');
128
- buffer = events.pop() || '';
129
-
130
- events.forEach((eventStr) => {
131
- if (eventStr.trim()) {
132
- const event: Record<string, string> = {};
133
- eventStr.split('\n').forEach((line) => {
134
- const sepIndex = line.indexOf(':');
135
- if (sepIndex !== -1) {
136
- const key = line.slice(0, sepIndex).trim();
137
- // 注意 trimStart 保留 value 中的空格
138
- event[key] = line.slice(sepIndex + 1).trimStart();
139
- }
140
- });
141
-
142
- if (event.event || event.data) {
143
- onMessage(event.event || 'message', event.data || '');
144
- }
145
- }
146
- });
147
- });
148
-
149
- stream.on('error', (err: Error) => {
150
- signal?.removeEventListener('abort', onAbort);
151
- // 如果是主动取消的请求,不触发 onError
152
- if (err.message !== '请求已中止') {
153
- onError?.(err);
154
- }
155
- });
156
-
157
- stream.on('end', () => {
158
- signal?.removeEventListener('abort', onAbort);
159
- });
160
- } catch (error) {
161
- // 如果是取消的请求,不触发 onError
162
- if (!axios.isCancel(error)) {
163
- onError?.(normalizeError(error));
164
- }
165
- }
166
- };
167
-
168
- // PUT 请求封装
169
- export const putData = async <T, R>(
170
- url: string,
171
- params: T,
172
- headers: Record<string, string> = {}
173
- ): Promise<R> => {
174
- const res = await axios.put<T, AxiosResponse<R>>(url, params, {
175
- headers,
176
- });
177
- return res.data;
178
- };
179
-
180
- // DELETE 请求封装
181
- export const deleteData = async <T>(
182
- url: string,
183
- params: Record<string, unknown> = {},
184
- headers: Record<string, string> = {}
185
- ): Promise<T> => {
186
- const res = await axios.delete<T>(url, { params, headers });
187
- return res.data;
188
- };
@@ -1,6 +0,0 @@
1
- const api = {
2
- LANGUAGE_TOOL_V2_LANGUAGES: 'https://api.languagetool.org/v2/languages',
3
- LANGUAGE_TOOL_V2_CHECK: 'https://api.languagetool.org/v2/check',
4
- };
5
-
6
- export default api;
package/src/index.ts DELETED
@@ -1,78 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * CLI入口脚本
5
- * 通过交互式选择执行不同模块功能
6
- */
7
-
8
- import { Command } from 'commander';
9
- import { select, Separator } from '@inquirer/prompts';
10
- import { i18n } from './modules/i18n/index.js';
11
- import { logger, loggerError } from './utils/index.js';
12
-
13
- const program = new Command();
14
-
15
- /**
16
- * 主程序入口函数
17
- * 解析命令行参数,交互式选择模块并执行对应功能
18
- */
19
- async function main() {
20
- try {
21
- logger.info('td-web-cli程序启动');
22
-
23
- // 解析命令行参数
24
- program.parse(process.argv);
25
- logger.info(`命令行参数解析完成:${process.argv.slice(2).join(' ')}`);
26
-
27
- // 定义可用模块选项
28
- const moduleChoices = [
29
- {
30
- name: '国际化',
31
- value: 'i18n',
32
- description: '国际化相关功能',
33
- },
34
- ];
35
-
36
- // 交互式选择模块
37
- const answer = await select({
38
- message: '请选择要执行的模块:',
39
- choices: [
40
- ...moduleChoices,
41
- new Separator(), // 分割线,便于未来扩展更多模块
42
- ],
43
- default: 'i18n', // 默认选项
44
- pageSize: 10, // 最大显示选项数
45
- loop: true, // 选项循环滚动
46
- });
47
-
48
- // 查找选择模块的名称,方便日志输出
49
- const selectedModule = moduleChoices.find((item) => item.value === answer);
50
-
51
- if (!selectedModule) {
52
- logger.warn('未选择有效模块,程序已退出');
53
- process.exit(0);
54
- }
55
-
56
- logger.info(`用户选择模块:${selectedModule.name}`);
57
-
58
- // 根据选择执行对应模块
59
- switch (answer) {
60
- case 'i18n':
61
- logger.info(`${selectedModule.name}模块开始执行`);
62
- await i18n(program);
63
- logger.info(`${selectedModule.name}模块执行完成`);
64
- break;
65
- default:
66
- logger.warn(`${selectedModule.name}模块暂未实现,程序已退出`);
67
- process.exit(0);
68
- }
69
- } catch (error: unknown) {
70
- // 记录错误日志,方便排查
71
- loggerError(error, logger);
72
- console.error('程序执行时发生异常,已记录日志,程序已退出');
73
- process.exit(1);
74
- }
75
- }
76
-
77
- // 启动主程序
78
- main();