ops-toolkit 1.1.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.
Files changed (43) hide show
  1. package/.commitlintrc.js +25 -0
  2. package/.env.example +25 -0
  3. package/.husky/commit-msg +4 -0
  4. package/.husky/pre-commit +4 -0
  5. package/.opencode/README.md +320 -0
  6. package/.opencode/command/add-cmd.md +38 -0
  7. package/.opencode/command/add-pkg.md +28 -0
  8. package/.opencode/command/build.md +27 -0
  9. package/.opencode/command/debug.md +36 -0
  10. package/.opencode/command/fix.md +23 -0
  11. package/.opencode/command/release.md +28 -0
  12. package/.opencode/command/review.md +36 -0
  13. package/.opencode/command/test.md +25 -0
  14. package/.prettierrc +16 -0
  15. package/.release-it.json +29 -0
  16. package/.versionrc.js +18 -0
  17. package/.vscode/extensions.json +14 -0
  18. package/.vscode/launch.json +33 -0
  19. package/.vscode/typescript.code-snippets +61 -0
  20. package/AGENTS.md +277 -0
  21. package/CHANGELOG.md +24 -0
  22. package/QUICKSTART.md +136 -0
  23. package/README.md +143 -0
  24. package/bin/ops-toolkit.ts +92 -0
  25. package/bun.lock +1921 -0
  26. package/dist/index.js +3726 -0
  27. package/docs/DEBUGGING.md +255 -0
  28. package/docs/DEVELOPMENT_GUIDE.md +538 -0
  29. package/eslint.config.js +64 -0
  30. package/package.json +90 -0
  31. package/src/commands/deploy/index.ts +97 -0
  32. package/src/commands/monitor/index.ts +60 -0
  33. package/src/commands/system/index.ts +120 -0
  34. package/src/index.ts +82 -0
  35. package/src/types/commands.ts +41 -0
  36. package/src/types/index.ts +3 -0
  37. package/src/types/system.ts +65 -0
  38. package/src/types/ui.ts +61 -0
  39. package/src/utils/config.ts +146 -0
  40. package/src/utils/index.ts +3 -0
  41. package/src/utils/logger.ts +62 -0
  42. package/src/utils/system.ts +183 -0
  43. package/tsconfig.json +48 -0
@@ -0,0 +1,62 @@
1
+ import chalk from 'chalk';
2
+ import figlet from 'figlet';
3
+ import boxen from 'boxen';
4
+
5
+ // 日志工具类
6
+ export class Logger {
7
+ private static formatMessage(level: string, message: string, color: any): string {
8
+ const timestamp = new Date().toISOString();
9
+ return `${color(`[${timestamp}] [${level}]`)} ${message}`;
10
+ }
11
+
12
+ static info(message: string): void {
13
+ console.log(this.formatMessage('INFO', message, chalk.blue));
14
+ }
15
+
16
+ static success(message: string): void {
17
+ console.log(this.formatMessage('SUCCESS', message, chalk.green));
18
+ }
19
+
20
+ static warning(message: string): void {
21
+ console.log(this.formatMessage('WARNING', message, chalk.yellow));
22
+ }
23
+
24
+ static error(message: string): void {
25
+ console.error(this.formatMessage('ERROR', message, chalk.red));
26
+ }
27
+
28
+ static debug(message: string): void {
29
+ if (process.env.NODE_ENV === 'development' || process.env.DEBUG) {
30
+ console.log(this.formatMessage('DEBUG', message, chalk.gray));
31
+ }
32
+ }
33
+
34
+ // 显示标题
35
+ static showTitle(text: string, font: string = 'Standard'): void {
36
+ console.log(chalk.cyan(figlet.textSync(text, { font })));
37
+ }
38
+
39
+ // 显示框
40
+ static showBox(content: string, options?: any): void {
41
+ const boxOptions = {
42
+ padding: 1,
43
+ margin: 1,
44
+ borderStyle: 'round',
45
+ borderColor: 'cyan',
46
+ ...options,
47
+ };
48
+
49
+ console.log(boxen(content, boxOptions));
50
+ }
51
+
52
+ // 显示分隔线
53
+ static separator(char: string = '-', length: number = 50): void {
54
+ console.log(chalk.gray(char.repeat(length)));
55
+ }
56
+
57
+ // 显示加载动画
58
+ static spinner(message: string): any {
59
+ const ora = require('ora');
60
+ return ora(message).start();
61
+ }
62
+ }
@@ -0,0 +1,183 @@
1
+ import { exec } from 'child_process';
2
+ import { promisify } from 'util';
3
+ import os from 'os';
4
+ import fs from 'fs';
5
+ import chalk from 'chalk';
6
+
7
+ const execAsync = promisify(exec);
8
+
9
+ // 系统工具函数
10
+ export class SystemUtils {
11
+ // 获取系统信息
12
+ static async getSystemInfo() {
13
+ const info = {
14
+ hostname: os.hostname(),
15
+ platform: os.platform(),
16
+ arch: os.arch(),
17
+ uptime: os.uptime(),
18
+ loadAverage: os.loadavg(),
19
+ totalMemory: os.totalmem(),
20
+ freeMemory: os.freemem(),
21
+ cpus: os.cpus(),
22
+ };
23
+
24
+ return info;
25
+ }
26
+
27
+ // 获取内存使用情况
28
+ static getMemoryUsage() {
29
+ const total = os.totalmem();
30
+ const free = os.freemem();
31
+ const used = total - free;
32
+ const percentage = (used / total) * 100;
33
+
34
+ return {
35
+ total,
36
+ free,
37
+ used,
38
+ percentage: Math.round(percentage * 100) / 100,
39
+ };
40
+ }
41
+
42
+ // 获取CPU使用率
43
+ static getCpuUsage(): Promise<number> {
44
+ return new Promise(resolve => {
45
+ const startMeasure = this.cpuAverage();
46
+
47
+ setTimeout(() => {
48
+ const endMeasure = this.cpuAverage();
49
+ const idleDifference = endMeasure.idle - startMeasure.idle;
50
+ const totalDifference = endMeasure.total - startMeasure.total;
51
+ const percentageCPU = 100 - ~~((100 * idleDifference) / totalDifference);
52
+
53
+ resolve(percentageCPU);
54
+ }, 1000);
55
+ });
56
+ }
57
+
58
+ private static cpuAverage() {
59
+ const cpus = os.cpus();
60
+ let totalIdle = 0;
61
+ let totalTick = 0;
62
+
63
+ for (const cpu of cpus) {
64
+ for (const type in cpu.times) {
65
+ totalTick += cpu.times[type as keyof typeof cpu.times];
66
+ }
67
+ totalIdle += cpu.times.idle;
68
+ }
69
+
70
+ return {
71
+ idle: totalIdle / cpus.length,
72
+ total: totalTick / cpus.length,
73
+ };
74
+ }
75
+
76
+ // 执行系统命令
77
+ static async execCommand(command: string): Promise<string> {
78
+ try {
79
+ const { stdout } = await execAsync(command);
80
+ return stdout.trim();
81
+ } catch (error) {
82
+ throw new Error(`Command execution failed: ${command}`);
83
+ }
84
+ }
85
+
86
+ // 检查文件是否存在
87
+ static fileExists(path: string): boolean {
88
+ try {
89
+ return fs.existsSync(path);
90
+ } catch {
91
+ return false;
92
+ }
93
+ }
94
+
95
+ // 读取文件内容
96
+ static readFile(path: string): string {
97
+ try {
98
+ return fs.readFileSync(path, 'utf8');
99
+ } catch (error) {
100
+ throw new Error(`Failed to read file: ${path}`);
101
+ }
102
+ }
103
+
104
+ // 写入文件内容
105
+ static writeFile(path: string, content: string): void {
106
+ try {
107
+ fs.writeFileSync(path, content, 'utf8');
108
+ } catch (error) {
109
+ throw new Error(`Failed to write file: ${path}`);
110
+ }
111
+ }
112
+
113
+ // 格式化字节大小
114
+ static formatBytes(bytes: number): string {
115
+ const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
116
+ if (bytes === 0) return '0 Bytes';
117
+
118
+ const i = Math.floor(Math.log(bytes) / Math.log(1024));
119
+ return Math.round((bytes / Math.pow(1024, i)) * 100) / 100 + ' ' + sizes[i];
120
+ }
121
+
122
+ // 格式化运行时间
123
+ static formatUptime(seconds: number): string {
124
+ const days = Math.floor(seconds / 86400);
125
+ const hours = Math.floor((seconds % 86400) / 3600);
126
+ const minutes = Math.floor((seconds % 3600) / 60);
127
+ const secs = Math.floor(seconds % 60);
128
+
129
+ const parts = [];
130
+ if (days > 0) parts.push(`${days}d`);
131
+ if (hours > 0) parts.push(`${hours}h`);
132
+ if (minutes > 0) parts.push(`${minutes}m`);
133
+ if (secs > 0) parts.push(`${secs}s`);
134
+
135
+ return parts.join(' ') || '0s';
136
+ }
137
+
138
+ // 获取进程列表
139
+ static async getProcessList(): Promise<any[]> {
140
+ try {
141
+ const platform = os.platform();
142
+ let command = '';
143
+
144
+ if (platform === 'darwin') {
145
+ command = 'ps aux';
146
+ } else if (platform === 'linux') {
147
+ command = 'ps aux';
148
+ } else if (platform === 'win32') {
149
+ command = 'tasklist';
150
+ } else {
151
+ command = 'ps aux';
152
+ }
153
+
154
+ const output = await this.execCommand(command);
155
+ return this.parseProcessList(output);
156
+ } catch (error) {
157
+ console.error(chalk.red('Failed to get process list:'), error);
158
+ return [];
159
+ }
160
+ }
161
+
162
+ private static parseProcessList(output: string): any[] {
163
+ const lines = output.split('\n').slice(1); // 跳过标题行
164
+ const processes = [];
165
+
166
+ for (const line of lines) {
167
+ if (line.trim()) {
168
+ const parts = line.trim().split(/\s+/);
169
+ if (parts.length >= 11) {
170
+ processes.push({
171
+ pid: parseInt(parts[1] || '0'),
172
+ user: parts[0] || '',
173
+ cpu: parseFloat(parts[2] || '0'),
174
+ memory: parseFloat(parts[3] || '0'),
175
+ command: parts.slice(10).join(' ') || '',
176
+ });
177
+ }
178
+ }
179
+ }
180
+
181
+ return processes;
182
+ }
183
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "compilerOptions": {
3
+ // Environment setup & latest features
4
+ "lib": ["ESNext"],
5
+ "target": "ESNext",
6
+ "module": "Preserve",
7
+ "moduleDetection": "force",
8
+ "jsx": "react-jsx",
9
+ "allowJs": true,
10
+
11
+ // Bundler mode
12
+ "moduleResolution": "bundler",
13
+ "allowImportingTsExtensions": true,
14
+ "verbatimModuleSyntax": false,
15
+ "noEmit": true,
16
+
17
+ // Best practices
18
+ "strict": true,
19
+ "skipLibCheck": true,
20
+ "noFallthroughCasesInSwitch": true,
21
+ "noUncheckedIndexedAccess": true,
22
+ "noImplicitOverride": true,
23
+
24
+ // Some stricter flags (disabled by default)
25
+ "noUnusedLocals": false,
26
+ "noUnusedParameters": false,
27
+ "noPropertyAccessFromIndexSignature": false,
28
+
29
+ // Path mapping
30
+ "baseUrl": ".",
31
+ "paths": {
32
+ "@/*": ["src/*"],
33
+ "@/commands/*": ["src/commands/*"],
34
+ "@/components/*": ["src/components/*"],
35
+ "@/utils/*": ["src/utils/*"],
36
+ "@/types/*": ["src/types/*"]
37
+ },
38
+
39
+ // Additional options
40
+ "esModuleInterop": true,
41
+ "allowSyntheticDefaultImports": true,
42
+ "forceConsistentCasingInFileNames": true,
43
+ "resolveJsonModule": true,
44
+ "isolatedModules": true
45
+ },
46
+ "include": ["src/**/*", "bin/**/*", "scripts/**/*"],
47
+ "exclude": ["node_modules", "dist", "build", "coverage"]
48
+ }