@hecom/codearts 0.1.0 → 0.2.1

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.
@@ -1,56 +1,220 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
5
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.getGlobalConfigPath = getGlobalConfigPath;
37
+ exports.globalConfigExists = globalConfigExists;
38
+ exports.readGlobalConfig = readGlobalConfig;
39
+ exports.writeGlobalConfig = writeGlobalConfig;
40
+ exports.deleteGlobalConfig = deleteGlobalConfig;
6
41
  exports.loadConfig = loadConfig;
7
- const dotenv_1 = __importDefault(require("dotenv"));
8
- const global_config_1 = require("./global-config");
9
- // 加载当前目录的 .env 文件(如果存在)
10
- dotenv_1.default.config();
42
+ exports.getConfig = getConfig;
43
+ const fs = __importStar(require("fs"));
44
+ const os = __importStar(require("os"));
45
+ const path = __importStar(require("path"));
46
+ const types_1 = require("../types");
47
+ /**
48
+ * 全局配置管理工具
49
+ * 配置文件存储在用户主目录下的 .hecom-codearts 目录
50
+ */
51
+ const CONFIG_DIR = path.join(os.homedir(), '.hecom-codearts');
52
+ const CONFIG_FILE = path.join(CONFIG_DIR, 'config.env');
53
+ /**
54
+ * 确保配置目录存在
55
+ */
56
+ function ensureConfigDir() {
57
+ if (!fs.existsSync(CONFIG_DIR)) {
58
+ fs.mkdirSync(CONFIG_DIR, { recursive: true });
59
+ }
60
+ }
61
+ /**
62
+ * 获取全局配置文件路径
63
+ */
64
+ function getGlobalConfigPath() {
65
+ return CONFIG_FILE;
66
+ }
67
+ /**
68
+ * 检查全局配置文件是否存在
69
+ */
70
+ function globalConfigExists() {
71
+ return fs.existsSync(CONFIG_FILE);
72
+ }
73
+ /**
74
+ * 读取全局配置
75
+ */
76
+ function readGlobalConfig() {
77
+ if (!globalConfigExists()) {
78
+ return {};
79
+ }
80
+ const config = {};
81
+ try {
82
+ const content = fs.readFileSync(CONFIG_FILE, 'utf-8');
83
+ const lines = content.split('\n');
84
+ for (const line of lines) {
85
+ const trimmedLine = line.trim();
86
+ if (!trimmedLine || trimmedLine.startsWith('#')) {
87
+ continue;
88
+ }
89
+ const equalIndex = trimmedLine.indexOf('=');
90
+ if (equalIndex > 0) {
91
+ const key = trimmedLine.substring(0, equalIndex).trim();
92
+ const value = trimmedLine.substring(equalIndex + 1).trim();
93
+ // 只保存合法的配置键
94
+ if (Object.values(types_1.ConfigKey).includes(key)) {
95
+ config[key] = value;
96
+ }
97
+ }
98
+ }
99
+ }
100
+ catch (error) {
101
+ console.error('读取全局配置文件失败:', error);
102
+ }
103
+ return config;
104
+ }
105
+ /**
106
+ * 配置项分组和顺序定义
107
+ */
108
+ const CONFIG_GROUPS = [
109
+ {
110
+ title: '华为云IAM认证端点(根据区域调整)',
111
+ keys: [types_1.ConfigKey.HUAWEI_CLOUD_IAM_ENDPOINT, types_1.ConfigKey.HUAWEI_CLOUD_REGION],
112
+ },
113
+ {
114
+ title: 'IAM用户凭证',
115
+ keys: [
116
+ types_1.ConfigKey.HUAWEI_CLOUD_USERNAME,
117
+ types_1.ConfigKey.HUAWEI_CLOUD_PASSWORD,
118
+ types_1.ConfigKey.HUAWEI_CLOUD_DOMAIN,
119
+ ],
120
+ },
121
+ {
122
+ title: '项目配置',
123
+ keys: [types_1.ConfigKey.CODEARTS_BASE_URL, types_1.ConfigKey.PROJECT_ID, types_1.ConfigKey.ROLE_ID],
124
+ },
125
+ ];
126
+ /**
127
+ * 写入全局配置
128
+ * 支持动态配置项,自动按分组组织配置文件
129
+ */
130
+ function writeGlobalConfig(config) {
131
+ ensureConfigDir();
132
+ // 构建配置文件头部
133
+ let content = `# Hecom CodeArts 全局配置文件`;
134
+ // 记录已写入的配置项
135
+ const writtenKeys = new Set();
136
+ // 按分组写入配置
137
+ for (const group of CONFIG_GROUPS) {
138
+ content += `\n# ${group.title}\n`;
139
+ for (const key of group.keys) {
140
+ const value = config[key] || '';
141
+ content += `${key}=${value}\n`;
142
+ writtenKeys.add(key);
143
+ }
144
+ }
145
+ // 写入未分组的其他配置项(支持未来扩展)
146
+ const otherKeys = Object.keys(config).filter((key) => !writtenKeys.has(key));
147
+ if (otherKeys.length > 0) {
148
+ content += `\n# 其他配置\n`;
149
+ for (const key of otherKeys) {
150
+ const value = config[key] || '';
151
+ content += `${key}=${value}\n`;
152
+ }
153
+ }
154
+ try {
155
+ fs.writeFileSync(CONFIG_FILE, content, 'utf-8');
156
+ }
157
+ catch (error) {
158
+ throw new Error(`写入全局配置文件失败: ${error}`);
159
+ }
160
+ }
161
+ /**
162
+ * 删除全局配置
163
+ */
164
+ function deleteGlobalConfig() {
165
+ if (globalConfigExists()) {
166
+ try {
167
+ fs.unlinkSync(CONFIG_FILE);
168
+ }
169
+ catch (error) {
170
+ throw new Error(`删除全局配置文件失败: ${error}`);
171
+ }
172
+ }
173
+ }
11
174
  // 加载全局配置
12
- const globalConfig = (0, global_config_1.globalConfigExists)() ? (0, global_config_1.readGlobalConfig)() : {};
175
+ const globalConfig = globalConfigExists() ? readGlobalConfig() : {};
13
176
  /**
14
- * 加载配置,优先级:命令行参数 > 当前目录 .env > 全局配置 > 默认值
177
+ * 加载配置,优先级:命令行参数 > 全局配置
15
178
  * @param cliOptions 命令行选项
16
179
  * @returns 加载的配置
17
180
  */
18
181
  function loadConfig(cliOptions = {}) {
19
- // 命令行参数 > 当前目录 .env > 全局配置
20
- const projectId = cliOptions.projectId || process.env.PROJECT_ID || globalConfig.PROJECT_ID;
21
- const roleIdStr = cliOptions.roleId || process.env.ROLE_ID || globalConfig.ROLE_ID;
182
+ // 命令行参数 > 全局配置
183
+ const projectId = globalConfig[types_1.ConfigKey.PROJECT_ID];
184
+ const roleIdStr = cliOptions.roleId || globalConfig[types_1.ConfigKey.ROLE_ID];
22
185
  if (!projectId) {
23
- throw new Error('缺少必需参数: --project-id 或环境变量 PROJECT_ID\n提示:运行 codearts config 创建配置');
186
+ throw new Error('缺少项目 ID');
24
187
  }
25
188
  if (!roleIdStr) {
26
- throw new Error('缺少必需参数: --role-id 或环境变量 ROLE_ID\n提示:运行 codearts config 创建配置');
189
+ throw new Error('缺少角色 ID');
27
190
  }
28
191
  const roleIds = roleIdStr.split(',').map((id) => parseInt(id.trim()));
29
192
  if (roleIds.some((id) => isNaN(id))) {
30
193
  throw new Error('ROLE_ID 格式不正确,应为数字或逗号分隔的数字列表');
31
194
  }
32
- const username = cliOptions.username || process.env.HUAWEI_CLOUD_USERNAME || globalConfig.HUAWEI_CLOUD_USERNAME;
33
- const password = cliOptions.password || process.env.HUAWEI_CLOUD_PASSWORD || globalConfig.HUAWEI_CLOUD_PASSWORD;
34
- const domain = cliOptions.domain || process.env.HUAWEI_CLOUD_DOMAIN || globalConfig.HUAWEI_CLOUD_DOMAIN;
35
- if (!username || !password || !domain) {
36
- throw new Error('缺少华为云认证信息: --username, --password, --domain 或对应的环境变量\n提示:运行 codearts config 创建配置');
195
+ const username = globalConfig[types_1.ConfigKey.HUAWEI_CLOUD_USERNAME];
196
+ const password = globalConfig[types_1.ConfigKey.HUAWEI_CLOUD_PASSWORD];
197
+ const domain = globalConfig[types_1.ConfigKey.HUAWEI_CLOUD_DOMAIN];
198
+ const iamEndpoint = globalConfig[types_1.ConfigKey.HUAWEI_CLOUD_IAM_ENDPOINT];
199
+ const region = globalConfig[types_1.ConfigKey.HUAWEI_CLOUD_REGION];
200
+ const endpoint = globalConfig[types_1.ConfigKey.CODEARTS_BASE_URL];
201
+ if (!username || !password || !domain || !iamEndpoint || !region || !endpoint) {
202
+ throw new Error('缺少华为云认证信息,请先运行 `npx @hecom/codearts config` 创建配置');
37
203
  }
38
204
  const config = {
39
- iamEndpoint: cliOptions.iamEndpoint ||
40
- process.env.HUAWEI_CLOUD_IAM_ENDPOINT ||
41
- globalConfig.HUAWEI_CLOUD_IAM_ENDPOINT ||
42
- 'https://iam.cn-north-4.myhuaweicloud.com',
43
- region: cliOptions.region ||
44
- process.env.HUAWEI_CLOUD_REGION ||
45
- globalConfig.HUAWEI_CLOUD_REGION ||
46
- 'cn-north-4',
47
- endpoint: cliOptions.codeartsUrl ||
48
- process.env.CODEARTS_BASE_URL ||
49
- globalConfig.CODEARTS_BASE_URL ||
50
- 'https://projectman-ext.cn-north-4.myhuaweicloud.cn',
205
+ iamEndpoint,
206
+ region,
207
+ endpoint,
51
208
  username,
52
209
  password,
53
210
  domainName: domain,
54
211
  };
55
212
  return { projectId, roleIds, config };
56
213
  }
214
+ /**
215
+ * 获取最终合并后的配置(用于显示)
216
+ * @returns 合并后的配置映射
217
+ */
218
+ function getConfig() {
219
+ return globalConfig;
220
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hecom/codearts",
3
- "version": "0.1.0",
4
- "description": "CodeArts获取日报信息",
3
+ "version": "0.2.1",
4
+ "description": "华为云 CodeArts 统计分析工具",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "bin": {
@@ -10,14 +10,14 @@
10
10
  "files": [
11
11
  "dist",
12
12
  "bin",
13
- ".env.example",
14
13
  "README.md"
15
14
  ],
16
15
  "scripts": {
17
16
  "build": "rm -rf dist && tsc && chmod +x bin/codearts",
18
17
  "test": "jest --passWithNoTests",
19
18
  "test:coverage": "jest --coverage --passWithNoTests",
20
- "prepublishOnly": "npm run build"
19
+ "prepublishOnly": "npm run build",
20
+ "dev": "ts-node src/bin/cli.ts"
21
21
  },
22
22
  "keywords": [
23
23
  "huawei-cloud",
@@ -40,13 +40,12 @@
40
40
  "url": "https://github.com/hecom-rn/hecom-codearts/issues"
41
41
  },
42
42
  "engines": {
43
- "node": ">=16.0.0",
43
+ "node": ">=23.0.0",
44
44
  "npm": ">=7.0.0"
45
45
  },
46
46
  "dependencies": {
47
47
  "axios": "^1.5.0",
48
48
  "commander": "^12.1.0",
49
- "dotenv": "^16.3.1",
50
49
  "inquirer": "^9.3.8"
51
50
  },
52
51
  "devDependencies": {
package/.env.example DELETED
@@ -1,20 +0,0 @@
1
- # 华为云CodeArts API 环境变量配置
2
- # 复制此文件为 .env 并填入真实的配置值
3
-
4
- # 华为云IAM认证端点(根据区域调整)
5
- HUAWEI_CLOUD_IAM_ENDPOINT=https://iam.cn-north-4.myhuaweicloud.com
6
- HUAWEI_CLOUD_REGION=cn-north-4
7
-
8
- # IAM用户凭证
9
- HUAWEI_CLOUD_USERNAME=your-iam-username
10
- HUAWEI_CLOUD_PASSWORD=your-iam-password
11
- HUAWEI_CLOUD_DOMAIN=your-domain-name
12
-
13
- # CodeArts API基础URL(可选,默认根据区域自动生成)
14
- CODEARTS_BASE_URL=https://projectman-ext.cn-north-4.myhuaweicloud.cn
15
-
16
- PROJECT_ID=your-project-id
17
-
18
- # 角色ID(支持多个,逗号分隔)
19
- ROLE_ID=your-role-id
20
- # 示例:ROLE_ID=1,2,3
@@ -1,24 +0,0 @@
1
- /**
2
- * 获取全局配置文件路径
3
- */
4
- export declare function getGlobalConfigPath(): string;
5
- /**
6
- * 检查全局配置文件是否存在
7
- */
8
- export declare function globalConfigExists(): boolean;
9
- /**
10
- * 读取全局配置
11
- */
12
- export declare function readGlobalConfig(): Record<string, string>;
13
- /**
14
- * 写入全局配置
15
- */
16
- export declare function writeGlobalConfig(config: Record<string, string>): void;
17
- /**
18
- * 删除全局配置
19
- */
20
- export declare function deleteGlobalConfig(): void;
21
- /**
22
- * 获取配置信息(用于显示)
23
- */
24
- export declare function getConfigInfo(): string;
@@ -1,153 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.getGlobalConfigPath = getGlobalConfigPath;
37
- exports.globalConfigExists = globalConfigExists;
38
- exports.readGlobalConfig = readGlobalConfig;
39
- exports.writeGlobalConfig = writeGlobalConfig;
40
- exports.deleteGlobalConfig = deleteGlobalConfig;
41
- exports.getConfigInfo = getConfigInfo;
42
- const fs = __importStar(require("fs"));
43
- const path = __importStar(require("path"));
44
- const os = __importStar(require("os"));
45
- /**
46
- * 全局配置管理工具
47
- * 配置文件存储在用户主目录下的 .hecom-codearts 目录
48
- */
49
- const CONFIG_DIR = path.join(os.homedir(), '.hecom-codearts');
50
- const CONFIG_FILE = path.join(CONFIG_DIR, 'config.env');
51
- /**
52
- * 确保配置目录存在
53
- */
54
- function ensureConfigDir() {
55
- if (!fs.existsSync(CONFIG_DIR)) {
56
- fs.mkdirSync(CONFIG_DIR, { recursive: true });
57
- }
58
- }
59
- /**
60
- * 获取全局配置文件路径
61
- */
62
- function getGlobalConfigPath() {
63
- return CONFIG_FILE;
64
- }
65
- /**
66
- * 检查全局配置文件是否存在
67
- */
68
- function globalConfigExists() {
69
- return fs.existsSync(CONFIG_FILE);
70
- }
71
- /**
72
- * 读取全局配置
73
- */
74
- function readGlobalConfig() {
75
- if (!globalConfigExists()) {
76
- return {};
77
- }
78
- const config = {};
79
- try {
80
- const content = fs.readFileSync(CONFIG_FILE, 'utf-8');
81
- const lines = content.split('\n');
82
- for (const line of lines) {
83
- const trimmedLine = line.trim();
84
- if (!trimmedLine || trimmedLine.startsWith('#')) {
85
- continue;
86
- }
87
- const equalIndex = trimmedLine.indexOf('=');
88
- if (equalIndex > 0) {
89
- const key = trimmedLine.substring(0, equalIndex).trim();
90
- const value = trimmedLine.substring(equalIndex + 1).trim();
91
- config[key] = value;
92
- }
93
- }
94
- }
95
- catch (error) {
96
- console.error('读取全局配置文件失败:', error);
97
- }
98
- return config;
99
- }
100
- /**
101
- * 写入全局配置
102
- */
103
- function writeGlobalConfig(config) {
104
- ensureConfigDir();
105
- const content = `# Hecom CodeArts 全局配置文件
106
- # 此文件由 codearts config 命令自动生成
107
- # 位置: ${CONFIG_FILE}
108
-
109
- # 华为云IAM认证端点(根据区域调整)
110
- HUAWEI_CLOUD_IAM_ENDPOINT=${config.iamEndpoint || ''}
111
- HUAWEI_CLOUD_REGION=${config.region || ''}
112
-
113
- # IAM用户凭证
114
- HUAWEI_CLOUD_USERNAME=${config.username || ''}
115
- HUAWEI_CLOUD_PASSWORD=${config.password || ''}
116
- HUAWEI_CLOUD_DOMAIN=${config.domain || ''}
117
-
118
- # 项目配置
119
- CODEARTS_BASE_URL=${config.codeartsUrl || ''}
120
- PROJECT_ID=${config.projectId || ''}
121
- ROLE_ID=${config.roleId || ''}
122
- `;
123
- try {
124
- fs.writeFileSync(CONFIG_FILE, content, 'utf-8');
125
- }
126
- catch (error) {
127
- throw new Error(`写入全局配置文件失败: ${error}`);
128
- }
129
- }
130
- /**
131
- * 删除全局配置
132
- */
133
- function deleteGlobalConfig() {
134
- if (globalConfigExists()) {
135
- try {
136
- fs.unlinkSync(CONFIG_FILE);
137
- }
138
- catch (error) {
139
- throw new Error(`删除全局配置文件失败: ${error}`);
140
- }
141
- }
142
- }
143
- /**
144
- * 获取配置信息(用于显示)
145
- */
146
- function getConfigInfo() {
147
- if (globalConfigExists()) {
148
- return `全局配置文件: ${CONFIG_FILE}`;
149
- }
150
- else {
151
- return `全局配置文件不存在\n建议运行: codearts config`;
152
- }
153
- }