ai-worklog 1.0.1 → 1.0.2
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/bin/index.js +46 -5
- package/package.json +1 -1
package/bin/index.js
CHANGED
|
@@ -132,8 +132,9 @@ async function setupGitlabConfig() {
|
|
|
132
132
|
} catch {}
|
|
133
133
|
}
|
|
134
134
|
|
|
135
|
-
// 尝试从 glab 配置自动读取
|
|
136
135
|
let token = '', username = '';
|
|
136
|
+
|
|
137
|
+
// 来源1:glab 配置文件
|
|
137
138
|
const glabPaths = [
|
|
138
139
|
path.join(os.homedir(), 'Library', 'Application Support', 'glab-cli', 'config.yml'),
|
|
139
140
|
path.join(os.homedir(), '.config', 'glab-cli', 'config.yml'),
|
|
@@ -150,11 +151,51 @@ async function setupGitlabConfig() {
|
|
|
150
151
|
if (token) { console.log(' ✓ 从 glab 配置自动读取了 Token'); break; }
|
|
151
152
|
}
|
|
152
153
|
|
|
153
|
-
//
|
|
154
|
+
// 来源2:git credential helper(适合配置了 HTTPS 凭据的用户)
|
|
154
155
|
if (!token) {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
156
|
+
try {
|
|
157
|
+
const cr = spawnSync('git', ['credential', 'fill'], {
|
|
158
|
+
input: `protocol=https\nhost=${GITLAB_HOST}\n\n`,
|
|
159
|
+
encoding: 'utf8', timeout: 3000,
|
|
160
|
+
});
|
|
161
|
+
const passMatch = cr.stdout.match(/password=(.+)/);
|
|
162
|
+
const userMatch = cr.stdout.match(/username=(.+)/);
|
|
163
|
+
if (passMatch) { token = passMatch[1].trim(); console.log(' ✓ 从 git credential 自动读取了 Token'); }
|
|
164
|
+
if (userMatch && !username) username = userMatch[1].trim();
|
|
165
|
+
} catch {}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// 来源3:环境变量
|
|
169
|
+
if (!token && process.env.GITLAB_TOKEN) {
|
|
170
|
+
token = process.env.GITLAB_TOKEN;
|
|
171
|
+
console.log(' ✓ 从环境变量 GITLAB_TOKEN 读取了 Token');
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// 都没找到:自动打开浏览器到预填好的 Token 创建页,提示粘贴
|
|
175
|
+
if (!token) {
|
|
176
|
+
const tokenUrl = `https://${GITLAB_HOST}/-/user_settings/personal_access_tokens?name=ai-worklog&scopes=api,write_repository`;
|
|
177
|
+
console.log('\n 未检测到 GitLab Token,正在打开浏览器...');
|
|
178
|
+
console.log(` 请在页面中点击「Create personal access token」,复制后粘贴到下方\n`);
|
|
179
|
+
spawnSync('open', [tokenUrl]); // macOS
|
|
180
|
+
spawnSync('xdg-open', [tokenUrl]); // Linux(忽略失败)
|
|
181
|
+
token = await prompt(' 粘贴 Token: ');
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// 用户名:通过 API 自动获取,避免手动输入
|
|
185
|
+
if (!username && token) {
|
|
186
|
+
try {
|
|
187
|
+
const res = await apiRequest({
|
|
188
|
+
hostname: GITLAB_HOST,
|
|
189
|
+
path: '/api/v4/user',
|
|
190
|
+
method: 'GET',
|
|
191
|
+
headers: { 'PRIVATE-TOKEN': token },
|
|
192
|
+
rejectUnauthorized: false,
|
|
193
|
+
});
|
|
194
|
+
if (res.body && res.body.username) {
|
|
195
|
+
username = res.body.username;
|
|
196
|
+
console.log(` ✓ 自动获取用户名: ${username}`);
|
|
197
|
+
}
|
|
198
|
+
} catch {}
|
|
158
199
|
}
|
|
159
200
|
if (!username) {
|
|
160
201
|
username = await prompt(' GitLab 用户名: ');
|