Skip to content

Commit 1282b6f

Browse files
authored
Merge pull request #2 from codegen-sh/codegen-bot/enhance-dev-setup-and-auth-flow
2 parents 5001350 + 9ae8911 commit 1282b6f

File tree

3 files changed

+158
-12
lines changed

3 files changed

+158
-12
lines changed

README.md

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,27 +78,91 @@ You can customize the extension behavior in VSCode settings:
7878

7979
## Development
8080

81-
### Building from Source
81+
### Local Development Setup
8282

83-
```bash
84-
# Clone the repository
85-
git clone https://github.com/codegen-sh/codegen-ide.git
86-
cd codegen-ide
83+
Most people will use this extension in development mode. Here's how to set it up:
8784

88-
# Install dependencies
89-
npm install
85+
#### Prerequisites
86+
- [Node.js](https://nodejs.org/) (v16 or higher)
87+
- [VSCode](https://code.visualstudio.com/)
88+
- A [Codegen](https://codegen.com) account
89+
90+
#### Setup Steps
91+
92+
1. **Clone the repository**
93+
```bash
94+
git clone https://github.com/codegen-sh/codegen-ide.git
95+
cd codegen-ide
96+
```
97+
98+
2. **Install dependencies**
99+
```bash
100+
npm install
101+
```
102+
103+
3. **Open in VSCode**
104+
```bash
105+
code .
106+
```
90107

91-
# Compile TypeScript
108+
4. **Launch Extension Development Host**
109+
- Press `F5` or go to Run → Start Debugging
110+
- This opens a new VSCode window with the extension loaded
111+
- You can also use `Ctrl+Shift+P` → "Debug: Start Debugging"
112+
113+
5. **Test the extension**
114+
- In the new VSCode window, open the Command Palette (`Ctrl+Shift+P`)
115+
- Run `Codegen: Login` to authenticate
116+
- The Codegen sidebar should appear in the Activity Bar
117+
118+
#### Development Workflow
119+
120+
- **Hot Reload**: The extension automatically reloads when you make changes
121+
- **Debugging**: Set breakpoints in your TypeScript code and they'll work in the Extension Development Host
122+
- **Console Logs**: Check the Debug Console in the main VSCode window for extension logs
123+
- **Restart Extension**: Use `Ctrl+Shift+P` → "Developer: Reload Window" in the Extension Development Host
124+
125+
#### Available Scripts
126+
127+
```bash
128+
# Compile TypeScript (one-time)
92129
npm run compile
93130

94-
# Run in development mode
131+
# Watch mode (auto-compile on changes)
95132
npm run watch
133+
134+
# Run tests
135+
npm test
136+
137+
# Lint code
138+
npm run lint
139+
140+
# Package extension for distribution
141+
npm run package
96142
```
97143

98-
### Testing
144+
#### VSCode Launch Configurations
145+
146+
The project includes pre-configured launch settings in `.vscode/launch.json`:
147+
148+
- **Run Extension**: Launches the extension in a new VSCode window
149+
- **Extension Tests**: Runs the test suite
150+
- **Attach to Extension Host**: For debugging running extensions
151+
152+
#### Debugging Tips
153+
154+
1. **Extension Host Logs**: Check the Output panel → "Log (Extension Host)"
155+
2. **Developer Tools**: Use `Help → Toggle Developer Tools` in the Extension Development Host
156+
3. **Reload Extension**: `Ctrl+R` in the Extension Development Host to reload after changes
157+
158+
### Building from Source
99159

100160
```bash
161+
# Full build process
162+
npm install
163+
npm run compile
101164
npm test
165+
npm run package
102166
```
103167

104168
## Contributing

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@
129129
"type": "number",
130130
"default": 30,
131131
"description": "Auto-refresh interval in seconds"
132+
},
133+
"codegen.webUrl": {
134+
"type": "string",
135+
"default": "https://codegen.com",
136+
"description": "Codegen web URL for browser authentication"
132137
}
133138
}
134139
}

src/auth/AuthManager.ts

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,82 @@ export class AuthManager {
77
constructor(private context: vscode.ExtensionContext) {}
88

99
async login(): Promise<void> {
10+
// Show login options
11+
const loginMethod = await vscode.window.showQuickPick([
12+
{
13+
label: '🌐 Login with Browser',
14+
description: 'Open codegen.com in your browser to authenticate',
15+
detail: 'Recommended - secure OAuth flow'
16+
},
17+
{
18+
label: '🔑 Enter API Token',
19+
description: 'Manually enter your API token',
20+
detail: 'For users who already have a token'
21+
}
22+
], {
23+
placeHolder: 'Choose how you want to authenticate with Codegen',
24+
ignoreFocusOut: true
25+
});
26+
27+
if (!loginMethod) {
28+
return;
29+
}
30+
31+
if (loginMethod.label.includes('Browser')) {
32+
await this.loginWithBrowser();
33+
} else {
34+
await this.loginWithToken();
35+
}
36+
}
37+
38+
private async loginWithBrowser(): Promise<void> {
39+
try {
40+
// Generate a unique state parameter for security
41+
const state = Math.random().toString(36).substring(2, 15);
42+
43+
// Construct the OAuth URL
44+
const config = vscode.workspace.getConfiguration('codegen');
45+
const baseUrl = config.get('webUrl', 'https://codegen.com');
46+
const authUrl = `${baseUrl}/auth/vscode?state=${state}`;
47+
48+
// Open the browser
49+
vscode.env.openExternal(vscode.Uri.parse(authUrl));
50+
51+
// Show a message to the user
52+
const result = await vscode.window.showInformationMessage(
53+
'Opening codegen.com in your browser for authentication...',
54+
{ modal: false },
55+
'I\'ve completed authentication',
56+
'Cancel'
57+
);
58+
59+
if (result === 'Cancel') {
60+
return;
61+
}
62+
63+
if (result === 'I\'ve completed authentication') {
64+
// Prompt for the token that should now be available
65+
const token = await vscode.window.showInputBox({
66+
prompt: 'Paste the API token from your browser',
67+
placeHolder: 'The token should be displayed after authentication',
68+
password: true,
69+
ignoreFocusOut: true
70+
});
71+
72+
if (token) {
73+
await this.storeTokenAndValidate(token);
74+
}
75+
}
76+
} catch (error) {
77+
vscode.window.showErrorMessage(`Browser login failed: ${error}`);
78+
throw error;
79+
}
80+
}
81+
82+
private async loginWithToken(): Promise<void> {
1083
const token = await vscode.window.showInputBox({
1184
prompt: 'Enter your Codegen API token',
12-
placeHolder: 'Your API token from codegen.com',
85+
placeHolder: 'Your API token from codegen.com/settings',
1386
password: true,
1487
ignoreFocusOut: true
1588
});
@@ -18,6 +91,10 @@ export class AuthManager {
1891
return;
1992
}
2093

94+
await this.storeTokenAndValidate(token);
95+
}
96+
97+
private async storeTokenAndValidate(token: string): Promise<void> {
2198
// Store the token securely
2299
await this.context.secrets.store(AuthManager.TOKEN_KEY, token);
23100

@@ -28,7 +105,7 @@ export class AuthManager {
28105
await this.context.globalState.update(AuthManager.ORG_ID_KEY, orgId);
29106
}
30107

31-
vscode.window.showInformationMessage('Successfully logged in to Codegen!');
108+
vscode.window.showInformationMessage('Successfully logged in to Codegen! 🎉');
32109
} catch (error) {
33110
vscode.window.showErrorMessage(`Login failed: ${error}`);
34111
// Clear the token if login failed

0 commit comments

Comments
 (0)