open-notepad 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.
Files changed (2) hide show
  1. package/bin/note.js +86 -0
  2. package/package.json +1 -1
package/bin/note.js CHANGED
@@ -147,6 +147,92 @@ async function handleLogin() {
147
147
  const current = await loadConfig();
148
148
 
149
149
  const apiUrl = await ask('Enter Server API URL', current.apiUrl);
150
+
151
+ // Generate a random session code
152
+ const sessionCode = 'cli_' + Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
153
+
154
+ // Try browser-based login
155
+ info('Registering CLI session...');
156
+ try {
157
+ const regRes = await fetch(`${apiUrl.replace(/\/$/, '')}/api/cli/session`, {
158
+ method: 'POST',
159
+ headers: { 'Content-Type': 'application/json' },
160
+ body: JSON.stringify({ code: sessionCode })
161
+ });
162
+
163
+ if (!regRes.ok) {
164
+ throw new Error(`Server returned ${regRes.status}`);
165
+ }
166
+
167
+ // Open browser
168
+ const authPageUrl = `${apiUrl.replace(/\/$/, '')}/auth-cli-20260628dontuseforyounote?session=${sessionCode}`;
169
+ info(`Opening browser for authentication...`);
170
+ log(`\n${colors.dim}If browser does not open automatically, visit:${colors.reset}`);
171
+ log(`${colors.bold}${colors.cyan}${authPageUrl}${colors.reset}\n`);
172
+
173
+ // Platform-specific browser open
174
+ const openCmd = process.platform === 'win32' ? 'start'
175
+ : process.platform === 'darwin' ? 'open'
176
+ : 'xdg-open';
177
+ const child = spawn(openCmd, [authPageUrl], { shell: true, stdio: 'ignore', detached: true });
178
+ child.unref();
179
+
180
+ // Poll for completion
181
+ info('Waiting for browser authorization...');
182
+ const maxAttempts = 120; // 2 minutes (polling every 1s)
183
+ let attempts = 0;
184
+
185
+ while (attempts < maxAttempts) {
186
+ await new Promise(r => setTimeout(r, 1500));
187
+ attempts++;
188
+
189
+ try {
190
+ const pollRes = await fetch(`${apiUrl.replace(/\/$/, '')}/api/cli/session/${sessionCode}`);
191
+
192
+ if (pollRes.status === 404) {
193
+ // Session expired or not found
194
+ if (attempts > 10) {
195
+ error('Session expired. Please try again.');
196
+ return;
197
+ }
198
+ continue;
199
+ }
200
+
201
+ if (pollRes.ok) {
202
+ const data = await pollRes.json();
203
+
204
+ if (data.status === 'completed' && data.room_id && data.api_key) {
205
+ clearScreen();
206
+ log(`${colors.bgBlue}${colors.white}${colors.bold} CLI Connected Successfully! ${colors.reset}\n`);
207
+ await saveConfig({
208
+ apiUrl,
209
+ roomId: data.room_id,
210
+ apiKey: data.api_key
211
+ });
212
+ success(`Linked to room: ${colors.bold}${data.room_id}.notepad.web.id${colors.reset}`);
213
+ info('You can now use all CLI commands. Try: note list');
214
+ return;
215
+ }
216
+ }
217
+ } catch (pollErr) {
218
+ // Silently continue polling
219
+ }
220
+
221
+ // Progress indicator
222
+ if (attempts % 4 === 0) {
223
+ process.stdout.write(`\r${colors.dim} Polling... (${attempts}s elapsed, waiting for browser auth)${colors.reset} `);
224
+ }
225
+ }
226
+
227
+ process.stdout.write('\n');
228
+ warning('Browser authorization timed out.');
229
+ info('Falling back to manual configuration...\n');
230
+ } catch (e) {
231
+ warning(`Browser login unavailable: ${e.message}`);
232
+ info('Using manual configuration instead...\n');
233
+ }
234
+
235
+ // Fallback: manual configuration
150
236
  const roomId = await ask('Enter Subdomain/Room ID (e.g. "saya")', current.roomId);
151
237
  const apiKey = await ask('Enter Room API Token/Key', current.apiKey);
152
238
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "open-notepad",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "CLI tool for notepad.web.id to access, edit, create, and list room notes interactively.",
5
5
  "type": "module",
6
6
  "bin": {