@snapcommit/cli 3.7.1 → 3.8.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.
- package/dist/commands/github-connect.js +70 -6
- package/dist/lib/auth.js +29 -1
- package/package.json +1 -1
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* No OAuth complexity, instant setup!
|
|
3
|
+
* GitHub authentication - Auto-fetches OAuth token from SnapCommit account
|
|
4
|
+
* Falls back to manual PAT if OAuth token not available
|
|
6
5
|
*/
|
|
7
6
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
8
7
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
@@ -21,6 +20,7 @@ const path_1 = __importDefault(require("path"));
|
|
|
21
20
|
const os_1 = __importDefault(require("os"));
|
|
22
21
|
const readline_1 = __importDefault(require("readline"));
|
|
23
22
|
const rest_1 = require("@octokit/rest");
|
|
23
|
+
const auth_1 = require("../lib/auth");
|
|
24
24
|
const CONFIG_DIR = path_1.default.join(os_1.default.homedir(), '.snapcommit');
|
|
25
25
|
const GITHUB_TOKEN_FILE = path_1.default.join(CONFIG_DIR, 'github.json');
|
|
26
26
|
/**
|
|
@@ -44,6 +44,50 @@ function askQuestion(query) {
|
|
|
44
44
|
resolve(ans);
|
|
45
45
|
}));
|
|
46
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Try to auto-fetch GitHub OAuth token from SnapCommit account
|
|
49
|
+
*/
|
|
50
|
+
async function autoFetchGitHubToken() {
|
|
51
|
+
try {
|
|
52
|
+
const authConfig = (0, auth_1.getAuthConfig)();
|
|
53
|
+
if (!authConfig || !authConfig.token) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
// Call SnapCommit API to get GitHub OAuth token
|
|
57
|
+
const response = await fetch('https://snapcommit.dev/api/github/token', {
|
|
58
|
+
method: 'GET',
|
|
59
|
+
headers: {
|
|
60
|
+
'Authorization': `Bearer ${authConfig.token}`,
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
if (!response.ok) {
|
|
64
|
+
// OAuth token not available, user needs manual setup
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
const data = await response.json();
|
|
68
|
+
if (!data || typeof data !== 'object' || !('token' in data) || !('username' in data)) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
const githubData = data;
|
|
72
|
+
if (!githubData.token || !githubData.username) {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
// Verify token works by fetching user info
|
|
76
|
+
const octokit = new rest_1.Octokit({ auth: githubData.token });
|
|
77
|
+
const { data: user } = await octokit.users.getAuthenticated();
|
|
78
|
+
const config = {
|
|
79
|
+
token: githubData.token,
|
|
80
|
+
username: user.login,
|
|
81
|
+
userId: user.id,
|
|
82
|
+
connectedAt: new Date().toISOString(),
|
|
83
|
+
};
|
|
84
|
+
return config;
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
// Silent fail - fall back to manual setup
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
47
91
|
/**
|
|
48
92
|
* Verify GitHub token and get user info
|
|
49
93
|
*/
|
|
@@ -117,15 +161,35 @@ function clearGitHubToken() {
|
|
|
117
161
|
*/
|
|
118
162
|
async function githubConnectCommand(tokenArg) {
|
|
119
163
|
console.log(chalk_1.default.bold.cyan('\n╔════════════════════════════════════════╗'));
|
|
120
|
-
console.log(chalk_1.default.bold.cyan('║ 🐙 Connect GitHub CLI
|
|
164
|
+
console.log(chalk_1.default.bold.cyan('║ 🐙 Connect GitHub CLI ║'));
|
|
121
165
|
console.log(chalk_1.default.bold.cyan('╚════════════════════════════════════════╝\n'));
|
|
122
|
-
console.log(chalk_1.default.gray('
|
|
166
|
+
console.log(chalk_1.default.gray('Checking for GitHub OAuth token from your SnapCommit account...\n'));
|
|
167
|
+
// Try to auto-fetch GitHub OAuth token first
|
|
168
|
+
const autoConfig = await autoFetchGitHubToken();
|
|
169
|
+
if (autoConfig) {
|
|
170
|
+
// Success! Auto-connected via OAuth
|
|
171
|
+
saveGitHubToken(autoConfig);
|
|
172
|
+
console.log(chalk_1.default.green.bold('✅ GITHUB AUTO-CONNECTED! 🎉\n'));
|
|
173
|
+
console.log(chalk_1.default.white(` Connected as: ${chalk_1.default.bold('@' + autoConfig.username)}`));
|
|
174
|
+
console.log(chalk_1.default.gray(` Using OAuth token from snapcommit.dev\n`));
|
|
175
|
+
console.log(chalk_1.default.yellow('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n'));
|
|
176
|
+
console.log(chalk_1.default.bold('🚀 You can now use GitHub features!\n'));
|
|
177
|
+
console.log(chalk_1.default.gray(' Try these commands in snap REPL:\n'));
|
|
178
|
+
console.log(chalk_1.default.cyan(' • "create a pull request"'));
|
|
179
|
+
console.log(chalk_1.default.cyan(' • "check CI status"'));
|
|
180
|
+
console.log(chalk_1.default.cyan(' • "list my open PRs"\n'));
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
// Auto-fetch failed, fall back to manual PAT setup
|
|
184
|
+
console.log(chalk_1.default.yellow('⚠️ Could not auto-connect GitHub (OAuth token not available)\n'));
|
|
185
|
+
console.log(chalk_1.default.gray('No problem! You can manually connect with a Personal Access Token:\n'));
|
|
186
|
+
console.log(chalk_1.default.white('This CLI token enables GitHub features in your terminal:\n'));
|
|
123
187
|
console.log(chalk_1.default.white(' • Create pull requests'));
|
|
124
188
|
console.log(chalk_1.default.white(' • Check CI/workflow status'));
|
|
125
189
|
console.log(chalk_1.default.white(' • Manage issues and PRs'));
|
|
126
190
|
console.log(chalk_1.default.white(' • All with natural language!\n'));
|
|
127
191
|
console.log(chalk_1.default.yellow('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n'));
|
|
128
|
-
console.log(chalk_1.default.bold.white('
|
|
192
|
+
console.log(chalk_1.default.bold.white('Manual Setup (2 minutes):\n'));
|
|
129
193
|
console.log(chalk_1.default.gray(' 1. Go to: ') + chalk_1.default.cyan('https://github.com/settings/tokens'));
|
|
130
194
|
console.log(chalk_1.default.gray(' 2. Click "Generate new token (classic)"'));
|
|
131
195
|
console.log(chalk_1.default.gray(' 3. Name: ') + chalk_1.default.white('"SnapCommit CLI"'));
|
package/dist/lib/auth.js
CHANGED
|
@@ -163,9 +163,37 @@ async function promptAuth() {
|
|
|
163
163
|
* Ensure user is authenticated (prompt if not)
|
|
164
164
|
*/
|
|
165
165
|
async function ensureAuth() {
|
|
166
|
+
// Check if we have a locally stored token
|
|
166
167
|
if (isAuthenticated()) {
|
|
167
|
-
|
|
168
|
+
const config = getAuthConfig();
|
|
169
|
+
if (config) {
|
|
170
|
+
// CRITICAL: Verify token with server on every launch
|
|
171
|
+
try {
|
|
172
|
+
const result = await verifyToken(config.token);
|
|
173
|
+
if (result.valid) {
|
|
174
|
+
// Token is still valid!
|
|
175
|
+
return config;
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
// Token is invalid (user deleted, subscription expired, etc.)
|
|
179
|
+
console.log(chalk_1.default.yellow('\n⚠️ Your authentication token is no longer valid.'));
|
|
180
|
+
console.log(chalk_1.default.gray('This could mean:'));
|
|
181
|
+
console.log(chalk_1.default.gray(' • Your subscription has expired'));
|
|
182
|
+
console.log(chalk_1.default.gray(' • Your account was deleted'));
|
|
183
|
+
console.log(chalk_1.default.gray(' • The token was revoked\n'));
|
|
184
|
+
clearAuth();
|
|
185
|
+
// Fall through to re-prompt
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
catch (error) {
|
|
189
|
+
// Network error - allow offline usage with cached token
|
|
190
|
+
console.log(chalk_1.default.yellow('\n⚠️ Could not verify token (offline mode)'));
|
|
191
|
+
console.log(chalk_1.default.gray('Using cached credentials. Some features may be limited.\n'));
|
|
192
|
+
return config;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
168
195
|
}
|
|
196
|
+
// No valid token - prompt for authentication
|
|
169
197
|
const success = await promptAuth();
|
|
170
198
|
if (success) {
|
|
171
199
|
return getAuthConfig();
|